屬性值3)@Action中還可以指定一個params屬性,該屬性是一個字符串數組,用于該Acion指定的參數名和參數值。params屬性應遵守如下格式:{“name1”,”value1”,”name2”,”value2”}4)@Actions也" />

黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

struts2-convention-plugin Annotation(零配置

系統 2054 0

一、Convention的Annotation

1) 與Action相關的兩個Annotation是@Action 和@Actions
2) @Action中可指定一個value屬性。類似于指定<action name=””/>屬性值
3) @Action中還可以指定一個params屬性,該屬性是一個字符串數組,用于該Acion指定的參數名和參數值。params屬性應遵守如下格式:{“name1”,”value1”,”name2”,”value2”}
4) @Actions 也用于修飾Action類里的方法,用于將該方法映射到多個URL.@Actions用于組織多個@Action.因此它可將一個方法映射成多個邏輯Action。

通過@Action注釋
對如下例子:

Java代碼 收藏代碼
  1. package com.example.web;
  2. import com.opensymphony.xwork2.Action;
  3. import com.opensymphony.xwork2.ActionSupport;
  4. public class HelloAction extends ActionSupport{
  5. @Action ( "action1" )
  6. public Stringmethod1(){
  7. return SUCCESS;
  8. }
  9. @Action ( "/user/action2" )
  10. public Stringmethod2(){
  11. return SUCCESS;
  12. }
  13. }
方法名 默認調用路徑 默認映射路徑
method1 /hello!method1.action . /WEB-INF/content/hello.jsp
method2 /hello!method2.action. /WEB-INF/content/hello.jsp

通過@Action注釋后

方法名 @Action注釋后調用路徑 @Action注釋后映射路徑
method1 /action1!method1.action. 或簡寫 /action1 /WEB-INF/content/action1.jsp
method1 /user/action2!method2.action或 /user/action2 /WEB-INF/content/user/action2.jsp

注:

@Action( "/user" )則是 /user,對應的映射路徑是 /WEB-INF/content/action3.jsp, 該注釋覆蓋了默認的namespace(“/”)
通過@Actions注釋

Java代碼 收藏代碼
  1. package com.example.web;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. public class HelloAction extends ActionSupport{
  6. @Actions ({
  7. @Action ( "/different/url" ),
  8. @Action ( "/another/url" )
  9. })
  10. public Stringmethod1(){
  11. return “error”;
  12. }

我們可以通過: /different/url!method1.action( /different/url ) /another/url!method1.action 來調用 method1 方法。
對應的映射路徑分別是 /WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp

可能誤導了大家,一個方法被@Action注釋后,只是多了一種調用方式,而不是說覆蓋了原來的調用方式。比如對于如下例子:

Java代碼 收藏代碼
  1. package com.example.web;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. public class HelloAction extends ActionSupport{
  6. @Action ( "/another/url" )
  7. public Stringmethod1(){
  8. return “error”;
  9. }


我們調用method1方法可以通過兩種方式:
1 /hello!method1.action 映射 url: /WEB-INF/content/hello-error.jsp
2
/another/url!method1.action 映射 url: /WEB-INF/content/another/url-error.jsp
可見,兩種方式均可對method1方法進行調用,唯一的區別就是,兩種調用的映射是不一樣的,所以,想跳轉到不同的界面,這是一個非常好的選擇。

通過@Namespace 注釋

Java代碼 收藏代碼
  1. package com.example.web;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. @Namespace ( "/other" )
  6. public class HelloWorld extends ActionSupport{
  7. public Stringmethod1(){
  8. return “error”;
  9. }
  10. @Action ( "url" )
  11. public Stringmethod2(){
  12. return “error”;
  13. }
  14. @Action ( "/different/url" )
  15. public Stringmethod3(){
  16. return “error”;
  17. }
  18. }

通過 /other/hello-world!method1.action 訪問 method1 方法。
通過
/other/url!method2.action 訪問 method2 方法 映射 url: /WEB-INF/content/ /other/ url/ hello-error.jsp
通過
/different /url!method3.action 訪問 method3 方法
與@Action 注釋不同的是,該注釋覆蓋了默認的namespace(這里是’/’),此時再用 hello!method1.action 已經不能訪問 method1 了.


@Results和@Result
1 全局的(global)。

全局results可以被action類中所有的action分享,這種results在action類上使用注解進行聲明。

Java代碼 收藏代碼
  1. package com.example.actions;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. import org.apache.struts2.convention.annotation.Result;
  6. import org.apache.struts2.convention.annotation.Results;
  7. @Results ({
  8. @Result (name= "failure" ,location= "/WEB-INF/fail.jsp" )
  9. })
  10. public class HelloWorld extends ActionSupport{
  11. public Stringmethod1(){
  12. return “failure”;
  13. }
  14. @Action ( "/different/url" )
  15. public Stringmethod2(){
  16. return “failure”;
  17. }
  18. }


當我們訪問 /hello -world !method1.action 時,返回 /WEB-INF/fail.jsp
當我們訪問 /hello
-world !method2.action 時,返回 /WEB-INF/fail.jsp
當我們訪問 /different/url!method2.action 時,返回 /WEB-INF/fail.jsp


2 本地的(local)。
本地results只能在action方法上進行聲明。

Java代碼 收藏代碼
  1. package com.example.actions;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. import org.apache.struts2.convention.annotation.Result;
  6. import org.apache.struts2.convention.annotation.Results;
  7. public class HelloWorld extends ActionSupport{
  8. @Action (value= "/other/bar" ,results={ @Result (name= "error" ,location= "www.baidu.com" ,type= "redirect" )})
  9. public Stringmethod1(){
  10. return “error”;
  11. }
  12. }


當我們調用 /hello -world !method1.action 時,返回 /WEB-INF/content/hello-error.jsp
當我們調用 /other/bar!method1.action 時,返回 www.baidu.com


struts2-convention-plugin Annotation(零配置中的注解)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論