隨筆 - 72  文章 - 28  trackbacks - 0
          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(4)

          隨筆分類(66)

          隨筆檔案(72)

          文章檔案(19)

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          使用注解來配置Action的最大好處就是可以實現零配置,但是事務都是有利有弊的,使用方便,維護起來就沒那么方便了。

           

          要使用注解方式,我們必須添加一個額外包:struts2-convention-plugin-2.x.x.jar。

           

          雖說是零配置的,但struts.xml還是少不了的,配置如下:

           

          <?xml version="1.0" encoding="UTF-8" ?> 

          <!DOCTYPE struts PUBLIC 

              "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" 

              "http://struts.apache.org/dtds/struts-2.1.7.dtd"> 

             

          <struts>   

              <!-- 請求參數的編碼方式--> 

              <constant name="struts.i18n.encoding" value="UTF-8"/> 

              <!-- 指定被struts2處理的請求后綴類型。多個用逗號隔開--> 

              <constant name="struts.action.extension" value="action,do,htm"/> 

              <!-- 當struts.xml改動后,是否重新加載。默認值為false(生產環境下使用),開發階段最好打開  --> 

              <constant name="struts.configuration.xml.reload" value="true"/> 

              <!-- 是否使用struts的開發模式。開發模式會有更多的調試信息。默認值為false(生產環境下使用),開發階段最好打開  --> 

              <constant name="struts.devMode" value="false"/>   

              <!-- 設置瀏覽器是否緩存靜態內容。默認值為true(生產環境下使用),開發階段最好關閉  --> 

              <constant name="struts.serve.static.browserCache" value="false" /> 

              <!-- 指定由spring負責action對象的創建    

              <constant name="struts.objectFactory" value="spring" /> 

              --> 

              <!-- 是否開啟動態方法調用--> 

              <constant name="struts.enable.DynamicMethodInvocation" value="false"/> 

          </struts> 

          action類的注解:

           

           

          package com.tjcyjd.web.action;   

             

          import org.apache.struts2.convention.annotation.Action;   

          import org.apache.struts2.convention.annotation.ExceptionMapping;   

          import org.apache.struts2.convention.annotation.ExceptionMappings;   

          import org.apache.struts2.convention.annotation.Namespace;   

          import org.apache.struts2.convention.annotation.ParentPackage;   

          import org.apache.struts2.convention.annotation.Result;   

          import org.apache.struts2.convention.annotation.Results;   

             

          import com.opensymphony.xwork2.ActionSupport;   

             

          /** 

           * Struts2基于注解的Action配置

           *  

           */   

          @ParentPackage("struts-default") 

          @Namespace("/annotation_test") 

          @Results( { @Result(name = "success", location = "/main.jsp"), 

                  @Result(name = "error", location = "/error.jsp") }) 

          @ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") }) 

          public class LoginAction extends ActionSupport { 

              private static final long serialVersionUID = 2730268055700929183L;  

              private String loginName;   

              private String password;   

             

              @Action("login") //或者寫成  @Action(value = "login")  

              public String login() throws Exception {   

             

                  if ("yjd".equals(loginName) && "yjd".equals(password)) {   

                      return SUCCESS;   

                  } else {   

                      return ERROR;   

                  }   

              }   

              @Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") })   

              public String add() throws Exception {   

                  return SUCCESS;   

              }   

              public String getLoginName() {   

                  return loginName;   

              }   

              public void setLoginName(String loginName) {   

                  this.loginName = loginName;   

              }   

              public String getPassword() {   

                  return password;   

              }   

              public void setPassword(String password) {   

                  this.password= password;   

              }   

          這樣就完成了一個基于注解的action配置。

           

          總結常用的注解如下:

           

          Namespace:指定命名空間。

          ParentPackage:指定父包。

          Result:提供了Action結果的映射。(一個結果的映射)

          Results:“Result”注解列表

          ResultPath:指定結果頁面的基路徑。

          Action:指定Action的訪問URL。

          Actions:“Action”注解列表。

          ExceptionMapping:指定異常映射。(映射一個聲明異常)

          ExceptionMappings:一級聲明異常的數組。

          InterceptorRef:攔截器引用。

          InterceptorRefs:攔截器引用組。

          posted on 2014-01-09 16:20 kelly 閱讀(203) 評論(0)  編輯  收藏 所屬分類: java 框架
          主站蜘蛛池模板: 常宁市| 永仁县| 云浮市| 即墨市| 通渭县| 伊通| 东光县| 沛县| 奈曼旗| 永和县| 肃北| 堆龙德庆县| 呈贡县| 福清市| 凉城县| 上林县| 博罗县| 武城县| 同心县| 瑞安市| 河池市| 保定市| 古田县| 尼木县| 青铜峡市| 洪泽县| 团风县| 华阴市| 昌平区| 辽中县| 凤山县| 荥阳市| 西充县| 宜章县| 三亚市| 阜南县| 准格尔旗| 新宾| 绿春县| 龙海市| 香港 |