Struts2可以在忽略struts.xml配置下的方法,而根据url指定的action方法进行调用,这个功能在框架中默认关闭!
1.先在配置文件中开启该功能
<struts>
<!-- 配置 Struts 可以受理的请求的扩展名 -->
<constant name="struts.action.extension" value="action,do,"></constant>
<!-- 打开允许动态方法调用的开关, 默认是 false -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="default" extends="struts-default">
<action name="testDynamicMethodInvocation"
class="com.atguigu.struts2.action.TestDynamicMethodInvocationActoin"
method="save">
<result>/success.jsp</result>
</action>
</package>
</struts>
2.对应TestDynamicMethodInvocationActoin类中有两个action方法,默认xml配置方法为save,
public class TestDynamicMethodInvocationActoin {
public String save(){
System.out.println("save...");
return "success";
}
public String update(){
System.out.println("update...");
return "success";
}
}
那么通过在浏览器上输入
http://localhost:8080/struts2-3/testDynamicMethodInvocation!update.do
即可动态调用对应Action中的update方法。