隨筆-126  評論-247  文章-5  trackbacks-0

          convention-plugin 可以用來實(shí)現(xiàn) struts2 的零配置。

          零配置的意思并不是說沒有配置,而是通過約定大于配置的方式,大量通過約定來調(diào)度頁面的跳轉(zhuǎn)而使得配置大大減少。

          考慮到某種因素,這里采用 myeclipse 作為示例 IDE,環(huán)境 :

          JDK 1.6
          myeclipse 8.6.1
          struts 2.1.8

          web.xml
            <filter>
                
          <filter-name>struts2</filter-name>
                
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
            
          </filter>
            
          <filter-mapping>
                
          <filter-name>struts2</filter-name>
                
          <url-pattern>/*</url-pattern>
            
          </filter-mapping>

          struts.xml
          <struts>

            
          <constant name="struts.devMode" value="true"/>                                                                     <!-- 開發(fā)模式 -->
            
          <constant name="struts.i18n.encoding" value="UTF-8"/>                                                       <!-- Web運(yùn)用編碼 -->
            
          <constant name="struts.convention.result.path" value="/view/" />                                  <!-- 搜索視圖資源的路徑 -->
            
          <constant name="struts.convention.action.name.separator" value="_" />                     <!-- Action類名分隔符 -->
            
          <constant name="struts.convention.classes.reload" value="true" />                                 <!-- convention類重加載 -->
            
          <constant name="struts.convention.action.suffix" value="Action" />                               <!-- Action后綴名 -->
            
          <constant name="struts.action.extension" value="action,do,html,htm,php,aspx" /> <!-- Action擴(kuò)展名 -->
            
          <constant name="struts.convention.package.locators" value="action,actions" />       <!-- 搜索Action資源的包路徑 -->
            
          </struts>

          convention 幾項(xiàng)重要配置 :

          Action 類后綴名 : <constant name="struts.convention.action.suffix" value="Action" />

          繼承了 struts2 的 ActionSupport 類的類不是一個普通的類,它具有能夠處理請求和對請求作出響應(yīng)的能力,

          通常,開發(fā)者常常為這種特殊的類起一個后綴名,以區(qū)分一般普通的類。例如,xxAction,xxController,

          這里的類名的后綴 Action,Controller 就是對應(yīng)上面配置中的 value 的值,這個值會在發(fā)起 URL 請求的時候被截去。例如 : TestAction ---> test

          Action類擴(kuò)展名 [多項(xiàng)以逗號隔開]    <constant name="struts.action.extension" value="action,do,html,htm,php,aspx" />

          與文件的擴(kuò)展名相似的,例如 : TestAction ---> test.action 或 test.do 或 test.html 或 test.php 或 test.aspx 或 ...

          注意 : 若該項(xiàng)被配置,則,訪問所有的 Action 都需帶上 Action 的擴(kuò)展名,否則客戶端將拋出 404 ERROR

          Action類名分隔符 : <constant name="struts.convention.action.name.separator" value="_" />

          若 Action 類名中出現(xiàn)駝峰標(biāo)識的串,則用分隔符來切割串,如 HelloWorldAction ---> hello_world

          搜索 Action 資源的包路徑 [多項(xiàng)以逗號隔開] : <constant name="struts.convention.package.locators" value="action,actions" />

          只有當(dāng)包名含有以上配置中 value 值中至少一項(xiàng)時,convention plugin 才能搜索找得到該 Action,

          否則就算訪問的 Action 是存在的,convention plugin 也無法找得到該 Action

          注意 : 若包名不是以上列出現(xiàn)過的串結(jié)束,則后面的串相當(dāng)于該包下所有 Action 訪問的命名空間 ( 以下面的 LoginAction 作為示例 )。

          搜索視圖資源(JSP,freemarker,等)的路徑 : <constant name="struts.convention.result.path" value="/view/" />

          所有的視圖資源都需放在配置指定的文件夾路徑下,否則,就算結(jié)果視圖是真實(shí)存在的,convention plugin 也無法找得到該視圖。默認(rèn)值是 /WEB-INF/content/


          demo 結(jié)構(gòu)圖 :



          convention 約定 :

          1. [ Action 不存在的情況 ] 若 convention plugin 在包搜索路徑中搜索不到與請求的 URL 相匹配的 Action,則會到視圖的搜索路徑下搜索

          與請求的 URL 相匹配的視圖資源,若還搜索不到,則拋出 no Action mapped Exception

          示例 :

          view/hello_world.jsp [ 沒有與之匹配的 Action 類 ]
          <html>
           
          <body>
            
          <h2>Hello World!!</h2>
           
          </body>
          </html>



          2. [ Action 的 execute 方法或動態(tài)方法調(diào)用的情況 ] 如請求 name!method.action,若 NameAction 存在,且 NameAction 中存在 method 這樣一個方法,

          則根據(jù) method 方法返回的字符串,結(jié)果的視圖資源名稱規(guī)則 ( 視圖以 JSP 文件為例 ) :

          ① success  ----->  name.jsp 或 name_success.jsp ; 若這兩個視圖同時存在,則 convention plugin 會選擇 name_success.jsp 作為視圖來展示

          ② 非 success 的任意串 string  ----->  name_string.jsp

          示例 :

          package net.yeah.fancydeepin.action;

          import com.opensymphony.xwork2.ActionSupport;
          /**
           * -----------------------------------------
           * @描述  TODO
           * @作者  fancy
           * @郵箱  fancydeepin@yeah.net
           * @日期  2012-10-26 <BR>
           * -----------------------------------------
           
          */
          public class SayHelloAction extends ActionSupport{

              
          private static final long serialVersionUID = 1L;

              
          private String message;
              
              
          public String execute(){
                  
                  message 
          = "Hello struts2 convention plugin!";
                  
          return SUCCESS;
              }
              
              
          public String say(){
                  
                  message 
          = "SayHelloAction, say";
                  
          return "world";
              }
              
              
          public String sayHello(){
                  
                  message 
          = "SayHelloAction, sayHello";
                  
          return "conventionPlugin";
              }

              
          public String getMessage() {
                  
          return message;
              }
              
          }

          view/say_hello.jsp
          <html>
           
          <body>
            
          <h2>say_hello.jsp &rarr; Message : ${message}</h2>
           
          </body>
          </html>

          view/say_hello_world.jsp
          <html>
           
          <body>
            
          <h2>say_hello_world.jsp &rarr; Message : ${message}</h2>
           
          </body>
          </html>

          view/say_hello_conventionPlugin.jsp
          <html>
           
          <body>
            
          <h2>say_hello_conventionPlugin.jsp &rarr; Message : ${message}</h2>
           
          </body>
          </html>










          convention 注解 :

          @ParentPackage : 對應(yīng)于 struts.xml 常量配置項(xiàng)的 <constant name="struts.convention.default.parent.package" value="xx"/> 默認(rèn)值是 convention-default

          @ResultPath : 對應(yīng)于 struts.xml 常量配置項(xiàng)的 <constant name="struts.convention.result.path" value="xx" /> 默認(rèn)值是 /WEB-INF/content/

          @Namespace : Action 訪問的命名空間,該注解一旦聲明,結(jié)果的視圖資源需放在 : 視圖搜索目錄/命名空間 ( 如 : view/simple/demo.jsp )


          package net.yeah.fancydeepin.action;

          import org.apache.struts2.convention.annotation.Namespace;
          import org.apache.struts2.convention.annotation.ParentPackage;
          import org.apache.struts2.convention.annotation.ResultPath;
          import com.opensymphony.xwork2.ActionSupport;
          /**
           * -----------------------------------------
           * @描述  TODO
           * @作者  fancy
           * @郵箱  fancydeepin@yeah.net
           * @日期  2012-10-26 <BR>
           * -----------------------------------------
           
          */
          @ParentPackage(
          "convention-default")
          @Namespace(
          "/simple")
          @ResultPath(
          "/view/")
          public class DemoAction extends ActionSupport{

              
          private static final long serialVersionUID = 1L;
              
          private String message;
              
              
          public String execute(){
                  
                  message 
          = "DemoAction";
                  
          return SUCCESS;
              }

              
          public String getMessage() {
                  
          return message;
              }
          }

          view/simple/demo.jsp
          <html>
           
          <body>
            
          <h2>demo.jsp &rarr; Hello World ${message}!</h2>
           
          </body>
          </html>



          @Action

          package net.yeah.fancydeepin.action;

          import org.apache.struts2.convention.annotation.Action;
          import com.opensymphony.xwork2.ActionSupport;
          /**
           * -----------------------------------------
           * @描述  TODO
           * @作者  fancy
           * @郵箱  fancydeepin@yeah.net
           * @日期  2012-10-26 <BR>
           * -----------------------------------------
           
          */
          public class ActionannotationAction extends ActionSupport{

              
          private static final long serialVersionUID = 1L;
              
          private String message;
              
              @Action(
          "invoke")
              
          public String invokeMethod(){
                  
                  message 
          = "ActionannotationAction, invokeMethod";
                  
          return SUCCESS;
              }

              
          public String getMessage() {
                  
          return message;
              }
          }

          view/invoke.jsp
          <html>
           
          <body>
            
          <h2>invoke.jsp &rarr; Message : ${message}</h2>
           
          </body>
          </html>




          @Result,@Results

          package net.yeah.fancydeepin.action;

          import java.util.Random;
          import org.apache.struts2.convention.annotation.Action;
          import org.apache.struts2.convention.annotation.Result;
          import org.apache.struts2.convention.annotation.Results;
          import com.opensymphony.xwork2.ActionSupport;
          /**
           * -----------------------------------------
           * @描述  TODO
           * @作者  fancy
           * @郵箱  fancydeepin@yeah.net
           * @日期  2012-10-26 <BR>
           * -----------------------------------------
           
          */
          @Results({@Result(name 
          = "success", location = "result.jsp")})
          public class ResultAction extends ActionSupport{

              
          private static final long serialVersionUID = 1L;
              
          private String message;
              
              
          public String execute(){
                  
                  message 
          = "ResultAction, execute";
                  
          return SUCCESS;
              }
              
              @Action(value 
          = "doIt"
                  results 
          = {
                      @Result(name 
          = "isTrue", location = "result_true.jsp"),
                      @Result(name 
          = "isFalse", location = "result_false.jsp")
                  }
              )
              
          public String doExecute(){

                  message 
          = "doExecute isFalse.";
                  
          if(new Random().nextBoolean()){
                      message 
          = "doExecute isTrue.";
                      
          return "isTrue";
                  }
                  
          return "isFalse";
              }

              
          public String getMessage() {
                  
          return message;
              }
          }

          view/result.jsp
          <html>
           
          <body>
            
          <h2>result.jsp &rarr; Message : ${message}</h2>
           
          </body>
          </html>

          view/result_true.jsp
          <html>
           
          <body>
            
          <h2>result_true.jsp &rarr; Message : ${message}</h2>
           
          </body>
          </html>

          view/result_false.jsp
          <html>
           
          <body>
            
          <h2>result_false.jsp &rarr; Message : ${message}</h2>
           
          </body>
          </html>







          The last example


          package net.yeah.fancydeepin.action.admin;

          import com.opensymphony.xwork2.ActionSupport;
          /**
           * -----------------------------------------
           * @描述  TODO
           * @作者  fancy
           * @郵箱  fancydeepin@yeah.net
           * @日期  2012-10-26 <BR>
           * -----------------------------------------
           
          */
          public class LoginAction extends ActionSupport{

              
          private static final long serialVersionUID = 1L;
              
          private String username;
              
          private String password;

              
          public String log(){
                  
                  username 
          = username.intern();
                  password 
          = password.intern();
                  
          if(username == "admin" && password == "fancy"){
                      
          return SUCCESS;
                  }
                  
          return ERROR;
              }

              
          public void setUsername(String username) {
                  
          this.username = username;
              }

              
          public void setPassword(String password) {
                  
          this.password = password;
              }
          }

          view/admin/login_success.jsp
          <html>
           
          <body>
            
          <h2>Welcome!!</h2>
           
          </body>
          </html>

          view/admin/login_error.jsp
          <html>
           
          <body>
            
          <h2>Login Failed!!</h2>
           
          </body>
          </html>









            
          posted on 2012-10-26 11:16 fancydeepin 閱讀(12021) 評論(6)  編輯  收藏

          評論:
          # re: Struts2 Convention Plugin ( struts2 零配置 ) 2012-10-26 13:04 | BlogJava_Net_332
          好強(qiáng)大,啊!111  回復(fù)  更多評論
            
          # re: Struts2 Convention Plugin ( struts2 零配置 ) 2013-01-26 14:45 | tomwills
          看了幾遍才慢慢參透,其實(shí)還是自己去實(shí)踐下比較好,真的很不錯!  回復(fù)  更多評論
            
          # re: Struts2 Convention Plugin ( struts2 零配置 )[未登錄] 2013-01-27 23:34 | 1
          不愧是大神,我終于會convension了。  回復(fù)  更多評論
            
          # re: Struts2 Convention Plugin ( struts2 零配置 )[未登錄] 2013-09-24 17:36 | 小菜
          能發(fā)一份源碼么?  回復(fù)  更多評論
            
          # re: Struts2 Convention Plugin ( struts2 零配置 )[未登錄] 2013-09-24 17:37 | 小菜
          1240927198@qq.com  回復(fù)  更多評論
            
          # re: Struts2 Convention Plugin ( struts2 零配置 ) 2015-03-23 19:29 | yqysrc
          求源碼 393434042@qq.com   回復(fù)  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 绵阳市| 兰西县| 沽源县| 邯郸县| 华宁县| 桂东县| 安宁市| 伊宁市| 平泉县| 辰溪县| 桂阳县| 靖安县| 扬州市| 呼和浩特市| 子洲县| 陇川县| 乐平市| 博野县| 甘泉县| 仙居县| 三江| 高阳县| 青神县| 陈巴尔虎旗| 土默特左旗| 米脂县| 长海县| 新乡县| 剑川县| 政和县| 德庆县| 泊头市| 马鞍山市| 澳门| 五峰| 台湾省| 肇州县| 龙岩市| 缙云县| 方正县| 凤阳县|