如果之前沒有用struts2 的convention,在開發中你會發現有大量的action需要去配置,而且每個action又有多個result需要配置。
用了struts的convention後將極大的減少這些操作。開發速度大大加快。
1.所需jar包 struts2-convention-plugin-2.2.1.1.jar
還要與之相當的xwork-core-2.1.6.jar的jar包,如果xwork版本較低時1.x.x會出現l類似異常
嚴重: Exception starting filter struts2
java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finder.ClassFinder.<init>(Lcom/opensymphony/xwork2/util/finder/ClassLoaderInterface;Ljava/util/Collection;ZLjava/util/Set;Lcom/opensymphony/xwork2/util/finder/Test;)V
所以最好將xwork的jar包升級到2.x的
2.struts.xml中的一些常量配置
2.1默認所有result對應的結果頁面都放在WEB-INF/content目錄下,你可以通過設置struts.convention.result.path這個屬性的值來改變到其他路徑。
例如:
<constant name="struts.convention.result.path" value="/WEB-INF/page" />
2.2默認路徑包含action,actions,struts,struts2的所有包都會被struts作爲含有Action類的路徑來搜索。你可以通過設置struts.convention.package.locators屬性修改這個設置。
例如:
<constant name="struts.convention.package.locators" value="web,action" />
則定義了在項目中,包路徑包含web和action的將被視爲Action存在的路徑來進行搜索。
3.尋找Action類的規則,從前一步找到的package以及其子package中尋找(直接或間接)實現了com.opensymphony.xwork2.Action接口的實現類以及以Action結尾的類
例如:
com.example.actions.MainAction
com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)
com.example.struts.company.details.ShowCompanyDetailsAction
4.命名空間。從定義的.package.locators標示開始到包結束的部分,就是命名空間。舉個例子:
com.ustb.web.user.userAction的命名空間是:”/user”。
com.ustb.web.user.detail.UserAction的命名空間是:”/user/detail”
如果有返回的result值,這與返回的result值之間用‘-’分割開,這個符號可以設置struts.convention.action.name.separator如
<constant name="struts.convention.action.name.separator" value="-" />
例如:UserAction->user UserDetailAction ->userDetail或者user-detail。
當申請的action爲userDetail.action是對應的url頁面就是userDetail.jsp
當申請的action爲user-detail.action是對應的url頁面就是user-detail.jsp
我發現這完全取決於申請提交的action格式
結合上面的。
如果有result例如list 則 user-list.jsp 和 userDetail-list.jsp或user-detail-list.jsp
對於com.ustb.web.user.detail.UserDetailAction,映射的url路徑就是/WEB-INF/content/user/detail/userDetail.jsp
或者/WEB-INF/content/user/detail/user-detail.jsp
如果有返回值result首先映射/WEB-INF/content/user/detail/userDetail-list.jsp,或者/WEB-INF/content/user/detail/user-detail-list.jsp,
如果未找到,其次映射/WEB-INF/content/user/detail/userDetail.jsp,/WEB-INF/content/user/detail/user-detail.jsp,
你可以自己去試一下
通過以上配置基本就可以搞定大部分的要求了,但是爲了滿足實際要求更靈活的配置。
struts-convention還提供了註解去滿足實際中存在的變態要求。(世界)
1.@Action
package com.example.web;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
@Action("action1")
public String method1() {
return SUCCESS;
}
@Action("/user/action2")
public String method2() {
return SUCCESS;
}
}
方法名 默認調用路徑 默認映射路徑
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
2.@Actions
package com.example.web;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
public class HelloAction extends ActionSupport {
@Actions({
@Action("/different/url"),
@Action("/another/url")
})
public String method1() {
return “error”;
}
我們可以通過:/different/url!method1.action 或 /another/url!method1.action 來調用method1 方法。
對應的映射路徑分別是
/WEB-INF/content/different/url-error.jsp;
/WEB-INF/content/another/url-error.jsp
可能誤導了大家,一個方法被@Action註釋後,只是多了一種調用方式,而不是說覆蓋了原來的調用方式。
兩種方式均可對method1方法進行調用,唯一的區別就是,兩種調用的映射是不一樣的,所以,想跳轉到不同的界面,這是一個非常好的選擇。
這相當於在調用方法的時候就已經指定好了返回的路徑了。
3.@Namespace
package com.example.web;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
@Namespace("/other")
public class HelloWorld extends ActionSupport {
public String method1() {
return “error”;
}
@Action("url")
public String method2() {
return “error”;
}
@Action("/different/url")
public String method3() {
return “error”;
}
}
通過 /other/hello-world!method1.action 訪問method1 方法。
通過 /other/url!method2.action 訪問method2 方法
通過 /different /url!method3.action 訪問method3 方法
與@Action 註釋不同的是,該註釋覆蓋了默認的namespace(這裏是’/’),此時再用hello!method1.action 已經不能訪問method1 了.
注意:這裏是覆蓋而不是並存。
4.@Results和@Result
1 全局的(global)。
全局results可以被action類中所有的action分享,這種results在action類上使用註解進行聲明。
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
@Results({
@Result(name="failure", location="/WEB-INF/fail.jsp")
})
public class HelloWorld extends ActionSupport {
public String method1() {
return “failure”;
}
@Action("/different/url")
public String method2() {
return “failure”;
}
}
當我們訪問 /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方法上進行聲明。
Java代碼
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.convention.annotation.Result;
import org.apache.convention.annotation.Results;
public class HelloWorld extends ActionSupport {
@Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})
public String method1() {
return “error”;
}
}
當我們調用 /hello -world !method1.action 時,返回 /WEB-INF/content/hello-error.jsp
當我們調用 /other/bar!method1.action 時,返回 www.baidu.com
參考:Struts2_Convention_Plugin中文文檔