歡迎您光臨本站 註冊首頁

Struts2教程3:struts.xml常用配置解析

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

使用<include>標籤重用配置文件

在Struts2中提供了一個默認的struts.xml文件,但如果package、action、interceptors等配置比較多時,都放到一個struts.xml文件不太容易維護.因此,就需要將struts.xml文件分成多個配置文件,然後在struts.xml文件中使用<include>標籤引用這些配置文件.這樣做的優點如下:

結構更清晰,更容易維護配置信息.

配置文件可以復用.如果在多個Web程序中都使用類似或相同的配置文件,那麼可以使用<include>標籤來引用這些配置文件,這樣可以減少工作量.

假設有一個配置文件,文件名為newstruts.xml,代碼如下:

<?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">
<actionname="submit" class="action.MoreSubmitAction">
<resultname="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>
<includefile="newstruts.xml"/>
<packagename="test"extends="struts-default">
……
</package>
</struts>

大家要注意一下,用<include>引用的xml文件也必須是完成的struts2的配置.實際上<include>在引用時是單獨解析的xml文件,而不是將被引用的文件插入到struts.xml文件中.

action的別名

在默認情況下,Struts2會調用動作類的execute方法.但有些時候,我們需要在一個動作類中處理不同的動作.也就是用戶請求不同的動作時,執行動作類中的不同的方法.為了達到這個目的,可以在<action>標籤中通過method方法指定要指行的動作類的方法名,並且需要為不同的動作起不同的名子(也稱為別名).如下面代碼所示:

<?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">
<actionname="test" class="action.MyAction">
……
</action>
<actionname="my" class="action.MyAction"method="my">
……
</action>
</package>
</struts>

上面代碼的兩個動作的class屬性都指向同一個類,name為這個類起了兩個動作別名:test和my.在動作my中,使用了method屬性指定要要運行的方法名為my.

在MyAction類中必須要有my方法,代碼如下:

packageaction;
importcom.opensymphony.xwork2.ActionSupport;
publicclassMyActionextendsActionSupport
{
……
publicStringexecute()throwsException
{
//處理test動作的代碼
}
publicStringmy()throwsException
{
 //處理my動作的代碼
}
……
}

除了在struts.xml中配置別名,還可以通過請求參數來描述指定動作(並不需要在struts.xml中配置).請求參數的格式如下:

http://localhost:8080/contextPath/actionName!method.action

關於通過請求指定動作的詳細內容,請參閱筆者寫的《Struts2教程2:處理一個form多個submit》.

為action指定參數

在struts2中還可以為action指定一個或多個參數.大家還記著struts1.x是如何設置的action參數不? 在struts1.x中可以使用<action>標籤的parameter屬性為其指定一個action參數,如果要指定多個,就只能通過逗號(,)或其他的分隔符將不同的參數隔開.而在struts2中可以通過<param>標籤指定任意多個參數.代碼如下:

<actionname="submit" class="action.MyAction">
<paramname="param1">value1</param>
<paramname="param2">value2</param>
<resultname="save">
/result.jsp
</result>
 ……
</action>

當然,在action中讀這些參數也非常簡單,只需要象獲取請求參數一樣在action類中定義相應的setter方法即可(一般不用定義getter方法).如下面的代碼將讀取param1和param2參數的值:

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;
}
……
}

當struts2在調用execute之前,param1和param2的值就已經是相應參數的值了,因此,在execute方法中可以直接使用param1和param2.

選擇result類型

在默認時,標籤的type屬性值是「dispatcher」(實際上就是轉發,forward).開發人員可以根據自己的需要指定不同的類型,如redirect、stream等.如下面代碼所示:

<result name="save"type="redirect">
  /result.jsp
</result>

這此result-type可以在struts2-core-2.0.11.1.jar包或struts2源代碼中的struts-default.xml文件中找到,在這個文件中找到<result-types>標籤,所有的result-type都在裡面定義了.代碼如下:

<result-types>
 <result-typename="chain"class="com.opensymphony.xwork2.ActionChainResult"/>
 <result-typename="dispatcher"class="org.apache.struts2.dispatcher.ServletDis
patcherResult"default="true"/>
 <result-typename="freemarker"class="org.apache.struts2.views.freemarker.Free
markerResult"/>
 <result-typename="httpheader"class="org.apache.struts2.dispatcher.HttpHeader
Result"/>
 <result-typename="redirect"class="org.apache.struts2.dispatcher.ServletRedir
ectResult"/>
 <result-typename="redirectAction"class="org.apache.struts2.dispatcher.Servle
tActionRedirectResult"/>
 <result-typename="stream"class="org.apache.struts2.dispatcher.StreamResult"/

 <result-typename="velocity"class="org.apache.struts2.dispatcher.VelocityResu
lt"/>
 <result-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-->
 <result-typename="redirect-action"class="org.apache.struts2.dispatcher.Servl
etActionRedirectResult"/>
 <result-typename="plaintext"class="org.apache.struts2.dispatcher.PlainTextRe
sult"/>
</result-types>

全局result

有很多時候一個<result>初很多<action>使用,這時可以使用<global-results>標籤來定義全局的<result>,代碼如下:

<struts>
<packagename="demo"extends="struts-default">
<global-results>
<resultname="print">/result.jsp</result>
</global-results>
<actionname="submit"class="action.MoreSubmitAction">
 ……
</action>
<actionname="my"class="action.MoreSubmitAction"method="my">
……
</action>
</package>
</struts>

如果<action>中沒有相應的<result>,Struts2就會使用全局的<result>.


[火星人 ] Struts2教程3:struts.xml常用配置解析已經有648次圍觀

http://coctec.com/docs/java/show-post-61859.html