Struts是Apache基金會(huì)Jakarta項(xiàng)目組的一個(gè)Open Source項(xiàng)目,它采用MVC模式,能夠很好地幫助Java開發(fā)者利用J2EE開發(fā)Web應(yīng)用。和其他的Java架構(gòu)一樣,Struts也是面向?qū)ο笤O(shè)計(jì),將MVC模式"分離顯示邏輯和業(yè)務(wù)邏輯"的能力發(fā)揮得淋漓盡致。Struts的目的是為了減少在運(yùn)用MVC設(shè)計(jì)模型來開發(fā)Web應(yīng)用的時(shí)間。你仍然需要學(xué)習(xí)和應(yīng)用該架構(gòu),不過它將可以完成其中一些繁重的工作。在本系列教程中我們將學(xué)習(xí)到Struts2的各種技術(shù)。
【相關(guān)文章】
- Struts2教程1:***個(gè)Struts2程序
- Struts2教程2:處理一個(gè)form多個(gè)submit
- Struts2教程4:使用validate方法驗(yàn)證數(shù)據(jù)
- Struts2教程5:使用Validation框架驗(yàn)證數(shù)據(jù)
- Struts2教程6:在Action類中獲得HttpServletResponse對(duì)象
- Struts2教程7:上傳任意多個(gè)文件
- Struts2教程8:攔截器概述
- Struts2教程9:實(shí)現(xiàn)自已的攔截器
- Struts2教程10:國際化
使用<include>標(biāo)簽重用配置文件
在Struts2中提供了一個(gè)默認(rèn)的struts.xml文件,但如果package、action、interceptors等配置比較多時(shí),都放到一個(gè)struts.xml文件不太容易維護(hù)。因此,就需要將struts.xml文件分成多個(gè)配置文件,然后在struts.xml文件中使用<include>標(biāo)簽引用這些配置文件。這樣做的優(yōu)點(diǎn)如下:
結(jié)構(gòu)更清晰,更容易維護(hù)配置信息。
配置文件可以復(fù)用。如果在多個(gè)Web程序中都使用類似或相同的配置文件,那么可以使用<include>標(biāo)簽來引用這些配置文件,這樣可以減少工作量。
假設(shè)有一個(gè)配置文件,文件名為newstruts.xml,代碼如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
?。紁ackagename="demo"extends="struts-default">
?。糰ctionname="submit" class="action.MoreSubmitAction">
?。紃esultname="save">
/result.jsp
?。?result>
<resultname="print">
/result.jsp
?。?result>
?。?action>
?。?package>
</struts>
|
則struts.xml引用newstruts.xml文件的代碼如下:
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEstrutsPUBLIC "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> ?。糹ncludefile="newstruts.xml"/> ?。紁ackagename="test"extends="struts-default"> …… ?。?package> </struts> |
大家要注意一下,用<include>引用的xml文件也必須是完成的struts2的配置。實(shí)際上<include>在引用時(shí)是單獨(dú)解析的xml文件,而不是將被引用的文件插入到struts.xml文件中。
action的別名
在默認(rèn)情況下,Struts2會(huì)調(diào)用動(dòng)作類的execute方法。但有些時(shí)候,我們需要在一個(gè)動(dòng)作類中處理不同的動(dòng)作。也就是用戶請(qǐng)求不同的動(dòng)作時(shí),執(zhí)行動(dòng)作類中的不同的方法。為了達(dá)到這個(gè)目的,可以在<action>標(biāo)簽中通過method方法指定要指行的動(dòng)作類的方法名,并且需要為不同的動(dòng)作起不同的名子(也稱為別名)。如下面代碼所示:
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEstrutsPUBLIC "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <packagename="demo"extends="struts-default"> ?。糰ctionname="test" class="action.MyAction"> …… ?。?action> <actionname="my" class="action.MyAction"method="my"> …… ?。?action> </package> </struts> |
上面代碼的兩個(gè)動(dòng)作的class屬性都指向同一個(gè)類,name為這個(gè)類起了兩個(gè)動(dòng)作別名:test和my。在動(dòng)作my中,使用了method屬性指定要要運(yùn)行的方法名為my。
在MyAction類中必須要有my方法,代碼如下:
packageaction; importcom.opensymphony.xwork2.ActionSupport; publicclassMyActionextendsActionSupport { …… publicStringexecute()throwsException { //處理test動(dòng)作的代碼 } publicStringmy()throwsException { //處理my動(dòng)作的代碼 } …… } |
除了在struts.xml中配置別名,還可以通過請(qǐng)求參數(shù)來描述指定動(dòng)作(并不需要在struts.xml中配置)。請(qǐng)求參數(shù)的格式如下:
http://localhost:8080/contextPath/actionName!method.action
關(guān)于通過請(qǐng)求指定動(dòng)作的詳細(xì)內(nèi)容,請(qǐng)參閱筆者寫的《Struts2教程2:處理一個(gè)form多個(gè)submit》。
#p#
為action指定參數(shù)
在struts2中還可以為action指定一個(gè)或多個(gè)參數(shù)。大家還記著struts1.x是如何設(shè)置的action參數(shù)不? 在struts1.x中可以使用<action>標(biāo)簽的parameter屬性為其指定一個(gè)action參數(shù),如果要指定多個(gè),就只能通過逗號(hào)(,)或其他的分隔符將不同的參數(shù)隔開。而在struts2中可以通過<param>標(biāo)簽指定任意多個(gè)參數(shù)。代碼如下:
<actionname="submit" class="action.MyAction"> <paramname="param1">value1</param> <paramname="param2">value2</param> ?。紃esultname="save"> /result.jsp ?。?result> …… </action> |
當(dāng)然,在action中讀這些參數(shù)也非常簡單,只需要象獲取請(qǐng)求參數(shù)一樣在action類中定義相應(yīng)的setter方法即可(一般不用定義getter方法)。如下面的代碼將讀取param1和param2參數(shù)的值:
packageaction; importcom.opensymphony.xwork2.ActionSupport; publicclassMyActionextendsActionSupport { privateStringparam1; privateStringparam2; publicStringexecute()throwsException { System.out.println(param1+param2); } publicvoidsetParam1(Stringparam1) { this.param1=param1; } publicvoidsetParam2(Stringparam2) { this.param2=param2; } …… } |
當(dāng)struts2在調(diào)用execute之前,param1和param2的值就已經(jīng)是相應(yīng)參數(shù)的值了,因此,在execute方法中可以直接使用param1和param2。
選擇result類型
在默認(rèn)時(shí),標(biāo)簽的type屬性值是“dispatcher”(實(shí)際上就是轉(zhuǎn)發(fā),forward)。開發(fā)人員可以根據(jù)自己的需要指定不同的類型,如redirect、stream等。如下面代碼所示:
<result name="save"type="redirect"> /result.jsp </result> |
這此result-type可以在struts2-core-2.0.11.1.jar包或struts2源代碼中的struts-default.xml文件中找到,在這個(gè)文件中找到<result-types>標(biāo)簽,所有的result-type都在里面定義了。代碼如下:
<result-types> ?。紃esult-typename="chain"class="com.opensymphony.xwork2.ActionChainResult"/> <result-typename="dispatcher"class="org.apache.struts2.dispatcher.ServletDis patcherResult"default="true"/> ?。紃esult-typename="freemarker"class="org.apache.struts2.views.freemarker.Free markerResult"/> ?。紃esult-typename="httpheader"class="org.apache.struts2.dispatcher.HttpHeader Result"/> <result-typename="redirect"class="org.apache.struts2.dispatcher.ServletRedir ectResult"/> ?。紃esult-typename="redirectAction"class="org.apache.struts2.dispatcher.Servle tActionRedirectResult"/> <result-typename="stream"class="org.apache.struts2.dispatcher.StreamResult"/ > ?。紃esult-typename="velocity"class="org.apache.struts2.dispatcher.VelocityResu lt"/> ?。紃esult-typename="xslt"class="org.apache.struts2.views.xslt.XSLTResult"/> <result-typename="plainText"class="org.apache.struts2.dispatcher.PlainTextRe sult"/> ?。?--DeprecatednameformscheduledforremovalinStruts2.1.0.ThecamelCaseversionsa repreferred.Seeww-1707--> ?。紃esult-typename="redirect-action"class="org.apache.struts2.dispatcher.Servl etActionRedirectResult"/> ?。紃esult-typename="plaintext"class="org.apache.struts2.dispatcher.PlainTextRe sult"/> </result-types> |
全局result
有很多時(shí)候一個(gè)<result>初很多<action>使用,這時(shí)可以使用<global-results>標(biāo)簽來定義全局的<result>,代碼如下:
<struts> <packagename="demo"extends="struts-default"> ?。糶lobal-results> <resultname="print">/result.jsp</result> ?。?global-results> ?。糰ctionname="submit"class="action.MoreSubmitAction"> …… ?。?action> ?。糰ctionname="my"class="action.MoreSubmitAction"method="my"> …… </action> ?。?package> </struts> |
如果<action>中沒有相應(yīng)的<result>,Struts2就會(huì)使用全局的<result>。
【編輯推薦】
- Struts2教程1:***個(gè)Struts2程序
- Struts2教程2:處理一個(gè)form多個(gè)submit
- Struts2教程4:使用validate方法驗(yàn)證數(shù)據(jù)
- Struts2教程5:使用Validation框架驗(yàn)證數(shù)據(jù)
- Struts2教程6:在Action類中獲得HttpServletResponse對(duì)象
- Struts2教程7:上傳任意多個(gè)文件
- Struts2教程8:攔截器概述
- Struts2教程9:實(shí)現(xiàn)自已的攔截器
- Struts2教程10:國際化