原文 http://blog.csdn.net/woshixuye/article/details/7734482
首先我們有一個Action——UserAction
public class UserAction extends ActionSupport
{
public String add()
{
return "add";
}
public String modify()
{
return "modify";
}
}
1 指定method
<package name="user" namespace="/userPath" extends="struts-default">
<action name=" userAdd " class="com.xy.UserAction" method="add">
<result name="add">add.jsp</result>
</action>
<action name=" userModify " class="com.xy.UserAction" method="modify">
<result name="modify">modify.jsp</result>
</action>
</package>
路徑:
userPath/userAdd
userPath/userModify
特點:
不靈活,CRUD四個操作就要配4個action。
2 動態方法調用DMI(Dynamic Method Invocation)
<package name="user" namespace="/userPath" extends="struts-default">
<action name="user" class="com.xy.UserAction">
<result name="add">add.jsp</result>
<result name="modify">modify.jsp</result>
</action>
</package>
路徑:
userPath/user!add
userPath/user!modify
特點:
靈活。只要指定不同的方法就可以做不同的操作。
3 通配符
<package name="all" namespace="/" extends="struts-default">
<action name="*_*" class="com.xy.{1}Action" method="{2}">
<result name="add">{1}_add.jsp</result>
<result name="modify">{1}_modify.jsp</result>
</action>
</package>
路徑:
User_add
User_modify
特點:
更加靈活。整個項目甚至只要配一個總的action。是指定方法的一個特殊的用法。不過我覺得用DMI可以將每個模塊分的清楚。
推薦的方法是動態調用,也就是DMI.
注意的問題:
1.比如在地址欄中輸入URL:http://localhost:8080/struts2/front/helloword!add
但是:如果這樣輸入的話,會報錯(There is no Action mapped for namespace [/front] and action name [helloword!add()] associated with context path [/Struts2_10003].)
因為:struts2中默認不允許使用DMI
所以:需要在配置文件中打開: <constant name="struts.enable.DynamicMethodInvocation" value="true"/>這樣大家在地址欄動態輸入就可以得到預期的頁面
2.如果想要利用DMI的方式傳遞參數的話:userPath/user!add?flag=true
