Struts2 Convention Plugin ( struts2 零配置 )
convention-plugin 可以用來(lái)實(shí)現(xiàn) struts2 的零配置。
零配置的意思并不是說(shuō)沒(méi)有配置,而是通過(guò)約定大于配置的方式,大量通過(guò)約定來(lái)調(diào)度頁(yè)面的跳轉(zhuǎn)而使得配置大大減少。
考慮到某種因素,這里采用 myeclipse 作為示例 IDE,環(huán)境 :
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"/>????????????????????????????????????????????????????????????? ???????<!--?開(kāi)發(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類(lèi)名分隔符?--> ??<constant?name="struts.convention.classes.reload"?value="true"?/>?????????????????????????? ??????<!--?convention類(lèi)重加載?--> ??<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 類(lèi)后綴名 :?<constant?name="struts.convention.action.suffix"?value="Action"?/>
繼承了 struts2 的 ActionSupport 類(lèi)的類(lèi)不是一個(gè)普通的類(lèi),它具有能夠處理請(qǐng)求和對(duì)請(qǐng)求作出響應(yīng)的能力,
通常,開(kāi)發(fā)者常常為這種特殊的類(lèi)起一個(gè)后綴名,以區(qū)分一般普通的類(lèi)。例如,xxAction,xxController,
這里的類(lèi)名的后綴 Action,Controller 就是對(duì)應(yīng)上面配置中的 value 的值,這個(gè)值會(huì)在發(fā)起 URL 請(qǐng)求的時(shí)候被截去。例如 : TestAction ---> test
Action類(lèi)擴(kuò)展名 [多項(xiàng)以逗號(hào)隔開(kāi)]??:???<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)被配置,則,訪(fǎng)問(wèn)所有的 Action 都需帶上 Action 的擴(kuò)展名,否則客戶(hù)端將拋出 404 ERROR
Action類(lèi)名分隔符 :?<constant?name="struts.convention.action.name.separator"?value="_"?/>
若 Action 類(lèi)名中出現(xiàn)駝峰標(biāo)識(shí)的串,則用分隔符來(lái)切割串,如 HelloWorldAction ---> hello_world
搜索 Action 資源的包路徑 [多項(xiàng)以逗號(hào)隔開(kāi)] :?<constant?name="struts.convention.package.locators"?value="action,actions"?/> 只有當(dāng)包名含有以上配置中 value 值中至少一項(xiàng)時(shí),convention plugin 才能搜索找得到該 Action,
否則就算訪(fǎng)問(wèn)的 Action 是存在的,convention plugin 也無(wú)法找得到該 Action
注意 :?若包名不是以上列出現(xiàn)過(guò)的串結(jié)束,則后面的串相當(dāng)于該包下所有 Action 訪(fǎng)問(wèn)的命名空間 (?以下面的 LoginAction?作為示例?)。
搜索視圖資源(JSP,freemarker,等)的路徑 :?<constant?name="struts.convention.result.path"?value="/view/"?/>
所有的視圖資源都需放在配置指定的文件夾路徑下,否則,就算結(jié)果視圖是真實(shí)存在的,convention plugin 也無(wú)法找得到該視圖。默認(rèn)值是 /WEB-INF/content/
demo 結(jié)構(gòu)圖 :
convention?約定 :
1. [?Action 不存在的情況?] 若 convention plugin 在包搜索路徑中搜索不到與請(qǐng)求的 URL 相匹配的 Action,則會(huì)到視圖的搜索路徑下搜索
與請(qǐng)求的 URL 相匹配的視圖資源,若還搜索不到,則拋出 no Action mapped Exception?
示例 :?
view/hello_world.jsp [?沒(méi)有與之匹配的 Action 類(lèi)?]
2. [?Action 的 execute 方法或動(dòng)態(tài)方法調(diào)用的情況?] 如請(qǐng)求 name!method.action,若 NameAction 存在,且 NameAction 中存在 method 這樣一個(gè)方法,
則根據(jù) method 方法返回的字符串,結(jié)果的視圖資源名稱(chēng)規(guī)則 (?視圖以 JSP 文件為例?) :
① success? ----->? name.jsp 或 name_success.jsp ; 若這兩個(gè)視圖同時(shí)存在,則 convention plugin 會(huì)選擇 name_success.jsp 作為視圖來(lái)展示
② 非 success 的任意串 string? ----->? name_string.jsp
示例 :?
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?→?Message?:?${message}</h2> ?</body> </html>view/say_hello_world.jsp
<html> ?<body> ??<h2>say_hello_world.jsp?→?Message?:?${message}</h2> ?</body> </html>view/say_hello_conventionPlugin.jsp
<html> ?<body> ??<h2>say_hello_conventionPlugin.jsp?→?Message?:?${message}</h2> ?</body> </html>
convention?注解 :
@ParentPackage?: 對(duì)應(yīng)于 struts.xml 常量配置項(xiàng)的?<constant?name="struts.convention.default.parent.package"?value="xx"/>?默認(rèn)值是 convention-default
@ResultPath?: 對(duì)應(yīng)于 struts.xml 常量配置項(xiàng)的?<constant?name="struts.convention.result.path"?value="xx"?/>?默認(rèn)值是 /WEB-INF/content/
@Namespace?: Action 訪(fǎng)問(wèn)的命名空間,該注解一旦聲明,結(jié)果的視圖資源需放在 : 視圖搜索目錄/命名空間 (?如 : view/simple/demo.jsp?)
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?→?Hello?World?${message}!</h2> ?</body> </html>
@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?→?Message?:?${message}</h2> ?</body> </html>
@Result,@Results
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?→?Message?:?${message}</h2> ?</body> </html>view/result_true.jsp
<html> ?<body> ??<h2>result_true.jsp?→?Message?:?${message}</h2> ?</body> </html>view/result_false.jsp
<html> ?<body> ??<h2>result_false.jsp?→?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>
?
?
文章轉(zhuǎn)自:http://www.blogjava.net/fancydeepin/archive/2012/10/26/struts2_convention_plugin.html
轉(zhuǎn)載于:https://www.cnblogs.com/Neil223/p/5587535.html
總結(jié)
以上是生活随笔為你收集整理的Struts2 Convention Plugin ( struts2 零配置 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASP.NET Core 源码阅读笔记(
- 下一篇: 原创:犹太人那么聪明,为何却在世界备受“