1. 默認所有的結果頁面都存儲在WEB-INF/content下,你可以通過設置struts.convention.result.path這個屬性的值來改變到其他路徑。如:
則將路徑配置到了WEB-INF/page 下。
2. 默認包路徑包含action,actions,struts,struts2的所有包都會被struts作爲含有Action類的路徑來搜索。你可以通過設置struts.convention.package.locators屬性來修改這個配置。如:
Com.ustb.web.*/com.ustb.action.*都將被視爲含有Action的包路徑而被搜索。
2. com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)
3. com.example.struts.company.details.ShowCompanyDetailsAction
Com.ustb.web.user.userAction的命名空間是:”/user”。Com.ustb.web.user.detail.UserAction的命名空間是:”/user/detail”
5. Convention通過如下規則確定URL的具體資源部分:去掉類名的Action部分。然後將將每個分部的首字母轉爲小寫,用’-’分割,你可以設置struts.convention.action.name.separator 如:
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格式的文件。
下面是actiong和結果模版的映射關係:
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 |
以上的內容來自struts2的文檔http://struts.apache.org/2.1.6/docs/convention-plugin.html
當然,簡單的通過默認的方式來進行配置不能完全滿足實際項目的需要。所幸,convention的零配置是非常靈活的。
通過@Action註釋
對如下例子:
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. }
方法名 | 默認調用路徑 | 默認映射路徑 |
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註釋
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. }
對應的映射路徑分別是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp
可能誤導了大家,一個方法被@Action註釋後,只是多了一種調用方式,而不是說覆蓋了原來的調用方式。比如對於如下例子:
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. }
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 註釋
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/url!method2.action 訪問method2 方法
通過 /different /url!method3.action 訪問method3 方法
與@Action 註釋不同的是,該註釋覆蓋了默認的namespace(這裏是’/’),此時再用hello!method1.action 已經不能訪問method1 了.
@Results和@Result
1 全局的(global)。
全局results可以被action類中所有的action分享,這種results在action類上使用註解進行聲明。
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. }
當我們訪問 /hello -world !method2.action 時,返回 /WEB-INF/fail.jsp
當我們訪問 /different/url!method2.action 時,返回 /WEB-INF/fail.jsp
2 本地的(local)。
本地results只能在action方法上進行聲明。
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. }
當我們調用 /other/bar!method1.action 時,返回 www.baidu.com