详解Struts2动态方法调用
动态方法就是一个Action对应多个请求,减少Action的数量
1、指定method属性
<action name="addAction" method="add" class="com.venn.action.HelloWorldAction"> <result>/jsp/add.jsp</result> </action>
2、感叹号(!)方式(不推荐使用)
<action name="HelloWorld" class="com.venn.action.HelloWorldAction"> <result>/jsp/test.jsp</result> <result name="add">/jsp/add.jsp</result> <result name="update">/jsp/update.jsp</result> </action>
需要在struts.xml中加入如下常量:
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>(加在package标签外面)
调用不同方法使用:
访问execute方法: http://localhost:8080/TestStruts2/HelloWorld.action
访问update方法: http://localhost:8080/TestStruts2/HelloWorld!update.action
访问add方法 http://localhost:8080/TestStruts2/HelloWorld!add.action
3、通配符方式
Action配置:
<action name="HelloWorld_*" method="{1}" class="com.venn.action.HelloWorldAction"> <result>/jsp/test.jsp</result> <result name="add">/jsp/add.jsp</result> <result name="update">/jsp/update.jsp</result> </action>
访问execute方法: http://localhost:8080/TestStruts2/HelloWorld.action 或http://localhost:8080/TestStruts2/HelloWorld_execute.action
访问add方法 http://localhost:8080/TestStruts2/HelloWorld_add.action
注:为简化struts.xml配置,可以将action配置为:
<action name="*_*_*" method="{2}" class="com.venn.{3}.{1}Action"> <result>/jsp/test.jsp</result> <result name="add">/jsp/{2}.jsp</result> <result name="update">/jsp/{2}.jsp</result> </action>
第一个*对应action,第二个*对应method
注意result标签的name属性不可以使用通配符
java类
public class HelloWorldAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println("execute method"); return "success"; } public String add(){ System.err.println("add method"); return "add"; } public String update(){ System.out.println("update method"); return "update"; } }
总结
以上就是本文关于详解Struts2动态方法调用的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅:struts2开发流程及详细配置 Struts2修改上传文件大小限制方法解析等,有什么问题可以随时留言,小编会尽快回复大家。
您可能感兴趣的文章:
- Struts2中动态方法调用
- Struts2动态方法调用
- Struts2动态方法调用
- Struts2使用!动态方法调用无效
- struts2动态方法调用
- struts2——动态方法调用(dmi)!!!
- struts2动态方法调用
- struts2自主学习之动态方法调用,使用method属性,配置Result,结果类型
- struts2 动态调用action中方法
- Struts2动态方法调用
- struts2动态方法调用 ActionName!methodName.action
- struts2 动态方法调用 感叹号 !
- struts2-通配符和动态方法调用
- 第三章Struts2 Action中动态方法调用、通配符的使用
- Struts2的动态方法调用DMI
- 第三篇——Struts2的动态方法调用
- Struts2 Action中动态方法调用、通配符的使用
- 关于Struts2 namespace 的学习笔记(DMI动态方法调用)
- Struts2中DMI(动态方法调用)的一些问题
- struts2——(8)(①3种情况通配符的使用方法②动态方法调用 )