想飛就別怕摔

          大爺的并TM罵人

          struts2學習筆記(二)--Conversion-plugin實現零配飾

          首先感謝http://www.cnblogs.com/MoShin/archive/2011/04/06/2006591.html給的這么詳細的講解。
          如何使用Convention(約定)將struts-Convention-plugin-2.1.6.jar文件復制到WEB-INF/lib路徑下

          零配置并不是沒有配置,而是通過約定大于配置的方式,大量通過約定來調度頁面的跳轉而使得配置大大減少。所以,首先應該了解下convention-plugin的約定:
          1. 默認所有的結果頁面都存儲在WEB-INF/content下,你可以通過設置struts.convention.result.path這個屬性的值來改變到其他路徑。如:
           
          1   <constant name="struts.convention.result.path" value="/WEB-INF/page" /> 

           則將路徑配置到了WEB-INF/page 下。

           

          2. 默認包路徑包含action,actions,struts,struts2的所有包都會被struts作為含有Action類的路徑來搜索。

          (A)Convention插件會把如下兩種java類當成Action處理:
              1) 所有實現了com.opensymphony.xwork2.Action的java類。
              2) 所有類名以Action結尾的java類

          (B)你也可以通過設置struts.convention.package.locators屬性來修改這個配置。如:

          1 <constant name="struts.convention.package.locators" value="web,action" />

          則定義了在項目中,包路徑包含web和action的將被視為Action存在的路徑來進行搜索。
          Com.ustb.web.*/com.ustb.action.*都將被視為含有Action的包路徑而被搜索。
          struts.Convention.exclude.packges:指定不掃描哪些包下的java類,位于這些包結構下的java類將不會自動映射成Action;
          (C)struts.convention.action.packages:Convention插件以該常量指定包作為根包來搜索Action類。Convention插件除了掃描action,actions,struts,struts2四個包的類以外,還會掃描該常量指定的一個  或多個包,Convention會試圖從中發現Action類。
          (D)注意:struts.convention.package.locators和struts.convention.action.packages兩個常量的作用比較微妙,開發者在利用這兩個常量時務必小心。
              如:下面Action所在包被映射的命名空間如下:
              com.fun.actions.LoginAction 映射到 /
              com.fun.actions.myoffice.CarInfoAction 映射到 /myoffice
              com.fun.struts.myoffice.EntINfoAction 映射到 /myofiice
          3. 接著,Convention從前一步找到的package以及其子package中尋找 com.opensymphony.xwork2.Action 的實現以及以Action結尾的類:
             1. com.example.actions.MainAction  
             
          2. com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)  
             
          3. com.example.struts.company.details.ShowCompanyDetailsAction 

           

          4. 命名空間。從定義的.package.locators標示開始到包結束的部分,就是命名空間。舉個例子:
              Com.ustb.web.user.userAction的命名空間是:”/user”。Com.ustb.web.user.detail.UserAction的命名空間是:”/user/detail”

          5. Convention通過如下規則確定URL的具體資源部分:去掉類名的Action部分。然后將將每個分部的首字母轉為小寫,用’-’分割,你可以設置struts.convention.action.name.separator 如:
          1     <constant name="struts.convention.action.name.separator" value="-" />
              還是舉個例子:
              UserAction->user  UserDetailAction ->user-detail。結合上面的。對于com.ustb.web.user.detail.UserDetailAction,映射的 url就是/WEB-INF/content/user/detail/user-detail.jsp

          6. struts支持.jsp .html .htm .vm格式的文件。
          下面是action和結果模版的映射關系:
          URL Result
          File that could match Result Type
          /hello success /WEB-INF/content/hello.jsp Dispatcher
          /hello success /WEB-INF/content/hello-success.htm Dispatcher
          /hello success /WEB-INF/content/hello.ftl FreeMarker
          /hello-world input /WEB-INF/content/hello-world-input.vm Velocity
          /test1/test2/hello error /WEB-INF/content/test/test2/hello-error.html Dispatcher

           

           

           

           

           

           

           

           

           

           

          當然,簡單的通過默認的方式來進行配置不能完全滿足實際項目的需要。所幸,convention的零配置是非常靈活的。
          通過@Action注釋
          對如下例子:

           1 import com.opensymphony.xwork2.Action;
           2 import com.opensymphony.xwork2.ActionSupport;
           3 
           4 public class HelloAction extends ActionSupport {
           5     @Action("action1")
           6     public String method1() {
           7         return SUCCESS;
           8     }
           9 
          10     @Action("/user/action2")
          11     public String method2() {
          12         return SUCCESS;
          13     }
          14 }

          方法名 默認調用路徑 默認映射路徑
          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. /WEB-INF/content/action1.jsp
          method1 /user/action2!method2.action /WEB-INF/content/user/action2.jsp


          通過@Actions注釋

           1 import com.opensymphony.xwork2.ActionSupport;   
           2 import org.apache.struts2.convention.annotation.Action;  
           3 import org.apache.struts2.convention.annotation.Actions;  
           4  
           5 public class HelloAction extends ActionSupport {  
           6     @Actions({  
           7      @Action("/different/url"),  
           8      @Action("/another/url")  
           9    })  
          10 public String method1() {  
          11    return “error”;  
          12 }
           
          我們可以通過:/different/url!method1.action /another/url!method1.action 來調用method1 方法。
          對應的映射路徑分別是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp

          可能誤導了大家,一個方法被@Action注釋后,只是多了一種調用方式,而不是說覆蓋了原來的調用方式。比如對于如下例子:
           1 import com.opensymphony.xwork2.ActionSupport;   
           2 import org.apache.struts2.convention.annotation.Action;  
           3 import org.apache.struts2.convention.annotation.Actions;  
           4    
           5 public class HelloAction extends ActionSupport {  
           6    @Action("/another/url")  
           7    public String method1() {  
           8      return “error”;  
           9    }  
          10 }
          我們調用method1方法可以通過兩種方式:
          /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 注釋

           1 import com.opensymphony.xwork2.ActionSupport;
           2 import org.apache.struts2.convention.annotation.Action;
           3 import org.apache.struts2.convention.annotation.Actions;
           4 
           5 @Namespace("/other")
           6 public class HelloWorld extends ActionSupport {
           7 
           8     public String method1() {  
           9         return “error”;  
          10     }    
          11     @Action("url")  
          12     public String method2() {  
          13         return “error”;  
          14     }    
          15     @Action("/different/url")  
          16     public String method3() {  
          17          return “error”;  
          18     }    
          19 }
          通過 /other/hello-world!method1.action 訪問method1 方法。
          通過
          /other/url!method2.action 訪問method2 方法
          通過
          /different /url!method3.action 訪問method3 方法
          與@Action 注釋不同的是,該注釋覆蓋了默認的namespace(這里是’/’),此時再用hello!method1.action 已經不能訪問method1 了.
          @Results和@Result
          1 全局的(global)。
          全局results可以被action類中所有的action分享,這種results在action類上使用注解進行聲明。
            
           1 import com.opensymphony.xwork2.ActionSupport;   
           2 import org.apache.struts2.convention.annotation.Action;  
           3 import org.apache.struts2.convention.annotation.Actions;  
           4 import org.apache.struts2.convention.annotation.Result;  
           5 import org.apache.struts2.convention.annotation.Results;  
           6      
           7  @Results({  
           8    @Result(name="failure", location="/WEB-INF/fail.jsp")  
           9  })  
          10  public class HelloWorld extends ActionSupport {  
          11    public String method1() {  
          12      return “failure”;  
          13    }  
          14      @Action("/different/url")  
          15    public String method2() {  
          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方法上進行聲明。
           1    
           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   
           8 public class HelloWorld extends ActionSupport {  
           9    @Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})  
          10    public String method1() {  
          11      return “error”;  
          12    }  
          13 }
           
          當我們調用 /hello -world !method1.action 時,返回 /WEB-INF/content/hello-error.jsp
          當我們調用 /other/bar!method1.action 時,返回 www.baidu.com

          posted on 2011-12-10 12:52 生命的綻放 閱讀(1418) 評論(0)  編輯  收藏 所屬分類: Struts2.0

          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(5)

          隨筆分類(94)

          隨筆檔案(93)

          文章分類(5)

          文章檔案(5)

          相冊

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 洛川县| 安顺市| 平阴县| 东安县| 子洲县| 萍乡市| 纳雍县| 临西县| 雅安市| 新沂市| 获嘉县| 会东县| 莎车县| 永昌县| 桃园市| 文山县| 南投市| 麻江县| 宁南县| 湘阴县| 伊川县| 兴宁市| 富裕县| 贡觉县| 兴义市| 濉溪县| 澄城县| 南靖县| 阿城市| 海南省| 滁州市| 绵阳市| 额尔古纳市| 台安县| 聂拉木县| 秦皇岛市| 格尔木市| 平陆县| 宜阳县| 大埔县| 昭觉县|