日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

struts2采用convention

 KILLKISS 2013-04-25

使用Convention插件,你需要將其JAR文件放到你應(yīng)用的WEB-INF/lib目錄中,你也可以在你Maven項目的POM文件中添加下面包依賴

Xml代碼  收藏代碼
  1. <dependency>  
  2.   <groupId>org.apache.struts</groupId>  
  3.   <artifactId>struts2-convention-plugin</artifactId>  
  4.   <version>2.1.6</version>  
  5. </dependency>  



零配置并不是沒有配置,而是通過約定大于配置的方式,大量通過約定來調(diào)度頁面的跳轉(zhuǎn)而使得配置大大減少。所以,首先應(yīng)該了解下convention-plugin的約定:
1. 默認(rèn)所有的結(jié)果頁面都存儲在WEB-INF/content下,你可以通過設(shè)置struts.convention.result.path這個屬性的值來改變到其他路徑。如:

Xml代碼  收藏代碼
  1. <constant name="struts.convention.result.path" value="/WEB-INF/page" />  


則將路徑配置到了WEB-INF/page 下。
2. 默認(rèn)包路徑包含action,actions,struts,struts2的所有包都會被struts作為含有Action類的路徑來搜索。你可以通過設(shè)置struts.convention.package.locators屬性來修改這個配置。如:

Xml代碼  收藏代碼
  1. <constant name="struts.convention.package.locators" value="web,action" />  


則定義了在項目中,包路徑包含web和action的將被視為Action存在的路徑來進(jìn)行搜索。
Com.ustb.web.*/com.ustb.action.*都將被視為含有Action的包路徑而被搜索。
3. 接著,Convention從前一步找到的package以及其子package中尋找 com.opensymphony.xwork2.Action 的實現(xiàn)以及以Action結(jié)尾的類:

Java代碼  收藏代碼
  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標(biāo)示開始到包結(jié)束的部分,就是命名空間。舉個例子:
Com.ustb.web.user.userAction的命名空間是:”/user”。Com.ustb.web.user.detail.UserAction的命名空間是:”/user/detail”
5. Convention通過如下規(guī)則確定URL的具體資源部分:去掉類名的Action部分。然后將將每個分部的首字母轉(zhuǎn)為小寫,用’-’分割,你可以設(shè)置struts.convention.action.name.separator 如

Xml代碼  收藏代碼
  1. <constant name="struts.convention.action.name.separator" value="-" />  


還是舉個例子:
UserAction->user  UserDetailAction ->user-detail。結(jié)合上面的。對于com.ustb.web.user.detail.UserDetailAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp
6. struts支持.jsp .html .htm .vm格式的文件。
下面是actiong和結(jié)果模版的映射關(guān)系:

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

 

 

 

 

 

 

 

 

 

 

以上的內(nèi)容來自struts2的文檔http://struts./2.1.6/docs/convention-plugin.html

 


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

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

 

方法名 默認(rèn)調(diào)用路徑 默認(rèn)映射路徑
method1 /hello!method1.action . /WEB-INF/content/hello.jsp
method2 /hello!method2.action. /WEB-INF/content/hello.jsp

通過@Action注釋后

方法名 @Action注釋后調(diào)用路徑 @Action注釋 后映射路徑
method1 /action1!method1.action. /WEB-INF/content/action1.jsp
method1 /user/action2!method2.action /WEB-INF/content/user/action2.jsp


通過@Actions注釋

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


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

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

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


我們調(diào)用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方法進(jìn)行調(diào)用,唯一的區(qū)別就是,兩種調(diào)用的映射是不一樣的,所以,想跳轉(zhuǎn)到不同的界面,這是一個非常好的選擇。


通過@Namespace 注釋

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



通過 /other/hello-world!method1.action 訪問method1 方法。
通過
/other/url!method2.action 訪問method2 方法
通過
/different /url!method3.action 訪問method3 方法
與@Action 注釋不同的是,該注釋覆蓋了默認(rèn)的namespace(這里是’/’),此時再用hello!method1.action 已經(jīng)不能訪問method1 了.
@Results和@Result
1 全局的(global)。
全局results可以被action類中所有的action分享,這種results在action類上使用注解進(jìn)行聲明。

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


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


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

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


當(dāng)我們調(diào)用 /hello -world !method1.action 時,返回 /WEB-INF/content/hello-error.jsp
當(dāng)我們調(diào)用 /other/bar!method1.action 時,返回 www.baidu.com

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多