動態(tài)方法調(diào)用
在Struts2中動態(tài)方法調(diào)用有三種方式,動態(tài)方法調(diào)用就是為了解決一個Action對應(yīng)多個請求的處理,以免Action太多
第一種方式:指定method屬性
這種方式我們前面已經(jīng)用到過,類似下面的配置就可以實現(xiàn)
<action name="chainAction" class="chapter2.action.Chapter2Action"
method="chainAction">
<result name="chainAction" type="chain">redirect</result>
</action>
<action name="plainText" class="chapter2.action.Chapter2Action"method="plainText">
<result name="plainText" type="plainText">/WEB-INF/JspPage/chapter2/plaintext.jsp</result>
</action>
第二種方式:感嘆號方式(需要開啟),官網(wǎng)不推薦使用這種方式,建議大家不要使用.
用這種方式需要先開啟一個開關(guān)
將此常量設(shè)置為true,這種方式才能使用,使用見示例
Action
package chapter3.action;
public class Chapter3Action {
public String result1(){
return "result1";
}public String result2(){
return "result2";
}
}
Jsp中訪問方式
<body><a href="basePath/chapter3/chapter3Action!result1">result1</a><br><ahref="basePath/chapter3/chapter3Action!result1">result1
result2</a><br>
</body>
如果配置了后綴,必須這樣寫:
/chapter4/chapter4Action!create.action
XML中配置方式
<package name="chapter3" namespace="/chapter3" extends="struts-default">
<action name="chapter3Action" class="chapter3.action.Chapter3Action">
<result name="result1">/WEB-INF/JspPage/chapter3/result1.jsp</result>
<result name="result2">/WEB-INF/JspPage/chapter3/result2.jsp</result>
<result name="chapter3">/WEB-INF/JspPage/chapter3/chapter3.jsp</result>
</action>
</package>
第三種方式:通配符方式(官網(wǎng)推薦使用)
首先得關(guān)閉開關(guān)
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
這一種方式是由第一種轉(zhuǎn)變過來的,我們可以看到,第一種方式有很多重復(fù)的代碼,那么我們可以進行變形,看下面的代碼
<action name="chapter3_*" class="chapter3.action.Chapter3Action"
method="{1}">