详解Struts2动态方法调用
作者:春江花夜月 时间:2022-10-18 11:19:25
动态方法就是一个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";
}
}
来源:http://www.cnblogs.com/Springmoon-venn/p/5578965.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
idea设置JVM运行参数的几种方式
![](https://img.aspxhome.com/file/2023/3/63803_0s.png)
如何用java程序(JSch)运行远程linux主机上的shell脚本
MyBatis逆向工程的创建和使用
![](https://img.aspxhome.com/file/2023/3/87213_0s.jpg)
Android悬浮按钮点击返回顶部FloatingActionButton
![](https://img.aspxhome.com/file/2023/8/117138_0s.gif)
Java反射机制(Reflection)浅析
详解在springboot中使用Mybatis Generator的两种方式
2022 最新 IntelliJ IDEA 详细配置步骤演示(推荐)
![](https://img.aspxhome.com/file/2023/3/77483_0s.jpg)
SpringMvc+Angularjs 实现多文件批量上传
Trie树(字典树)的介绍及Java实现
![](https://img.aspxhome.com/file/2023/5/79325_0s.png)
C#基于Socket实现多人聊天功能
![](https://img.aspxhome.com/file/2023/9/104359_0s.jpg)
SpringBoot JdbcTemplate批量操作的示例代码
SpringBoot如何返回Json数据格式
![](https://img.aspxhome.com/file/2023/0/61390_0s.png)
Java 数据结构与算法系列精讲之哈希算法实现
![](https://img.aspxhome.com/file/2023/3/71683_0s.gif)
SpringBoot整合Log4j2及配置步骤
![](https://img.aspxhome.com/file/2023/8/103808_0s.jpg)
java base64编码、解码的三种方式总结
实现分布式WebSocket集群的方法
![](https://img.aspxhome.com/file/2023/3/82433_0s.png)
Spring中统一异常处理示例详解
Android的Launcher启动器中添加快捷方式及小部件实例
![](https://img.aspxhome.com/file/2023/4/137144_0s.jpg)
sigsetjmp的用法总结
java中使用zxing批量生成二维码立牌
![](https://img.aspxhome.com/file/2023/1/64371_0s.png)