struts2动态调用action的方法

struts2的配置文件中代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="test" namespace="/user" extends="struts-default"> <action name="hello" class="com.dbj.action.TestAction"> <result>/hello.jsp</result> </action> </package> </struts>
Java后台代码:
 1 public class TestAction {
 2 
 3     public String test() {
 4         System.out.println("test");
 5         return "success";
 6     }
 7     
 8     public String add() {
 9         System.out.println("add");
10         return "success";
11     }
12     
13     public String execute() {
14         return "success";
15     }
16 }

浏览器动态调用action方法的URL:

http://localhost:8080/01Struts2/user/hello!test
其中的/user是namespace="/user",hello是action的name,test是TestAction类中的test()方法,配置中的action名称和类中的具体方法名称用“!”连接。

  

原文地址:https://www.cnblogs.com/dbj66/p/8340484.html