Action Mappings Action映射可以指定一組result類型,一組異常處理,一個攔截器堆。但是只有name屬性是必須的。其他的屬性都可以通過package范圍提供。
一個登錄action
<action name="Logon" class="tutorial.Logon">
<result type="redirect-action">Menu</result>
<result name="input">/tutorial/Logon.jsp</result>
</action>
Action Names 在web應用中,name屬性是用以匹配瀏覽器請求路徑的一部分(或其他HTTP客戶端)。framework會丟掉主機名、應用名和擴展名,并匹配中間部分。所以,一個
http://www.planetstruts.org/struts2-mailreader/Welcome.do
會map到
Welcome action。
在應用中,這個到action的鏈接一般是由struts tag來自動產生的。這個tag通過名字來指定action,并且這個framework會加上默認擴展名和其他一些必須 的東西。
<s:form action="Hello">
<s:textfield label="Please enter your name" name="name"/>
<s:submit/>
</s:form>
Wildcard Method 多數情況,一組action mapping會有一個共享模式。比如,你所有的 edit actions都一個“edit”打頭, 并調用這個action 類的edit方法。delete actions也是這個模式,但是調用的是delete方法。
這種情況,你就可以通過通配符的方式來定義一次action mapping,遠勝于定義每一個action mapping.
<action name="*Crud" class="example.Crud" method="{1}">
這里,一個"editCrud"的action會調用Crud Action類實例的edit方法。同樣,一個"deleteCrud"的action會調用delete方法。
另一種常用的方法就是后綴方法名,通過設置"!", "_", 或者其他一些特殊字符。
- "action=Crud_input"
- "action=Crud_delete"
To use a postfix wildcard, just move the asterisk and add an underscore.
<action name="Crud_*" class="example.Crud" method="{1}">