动态方法调用
动态方法调用就是为了解决一个Action对应多个请求的处理,以免Action太多。
例子(调用Action中的add()和update()方法,默认调用execute方法):
import com.opensymphony.xwork2.ActionSupport;
public class HelloWroldAction extends ActionSupport {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行Action");
return SUCCESS;
}
//add方法
public String add(){
return SUCCESS;
}
//update方法
public String update(){
return SUCCESS;
}
}
一、设置method属性
二、感叹号方式(不推荐使用)
三、通配符方式(官方推荐使用)
Struts2的配置可以参考 http://blog.csdn.net/qq_34966272/article/details/53213193
一、设置method属性
(1)修改struts.xml,添加Action属性
<?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>
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld" class="HelloWroldAction">
<result>/result.jsp</result>
</action>
<!--当页面返回 http://localhost:8080/当前项目/addAction.action时候,调用add方法 -->
<action name="addAction" method="add" class="HelloWroldAction">
<result>/add.jsp</result>
</action>
<!--当页面返回 http://localhost:8080/当前项目/updateAction.action时候,调用update方法 -->
<action name="updateAction" method="update" class="HelloWroldAction">
<result>/update.jsp</result>
</action>
</package>
</struts>
二、感叹号方式(不推荐使用)
(1)修改方法的返回值
import com.opensymphony.xwork2.ActionSupport;
public class HelloWroldAction extends ActionSupport {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行Action");
return SUCCESS;
}
//add方法
public String add(){
return "add";
}
//update方法
public String update(){
return "update";
}
}
(2)修改struts.xml文件
1.添加constant常量属性
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
2.修改action属性
<?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>
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld" class="HelloWroldAction">
<result>/result.jsp</result>
<!--当页面返回 http://localhost:8080/当前项目/addAction!add.action时候,调用add方法 -->
<result name="add">/add.jsp</result> <!--其中 name对应为方法的返回值 -->
<!--当页面返回 http://localhost:8080/当前项目/addAction!update.action时候,调用add方法 -->
<result name="update">/update.jsp</result>
</action>
</package>
<!-- 避免写入多个Action属性 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
(3)注意:
1.访问 http://localhost:8080/当前项目/addAction!update.action时候,调用add方法
三、通配符方式
(1)修改struts.xml文件
<?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>
<package name="default" namespace="/" extends="struts-default">
<!-- 用下划线 “_”隔开,用“*” 代表传入的参数{1},{2}...... -->
<action name="helloworld_*" method="{1}" class="HelloWroldAction">
<result>/result.jsp</result>
<!--当页面返回 http://localhost:8080/当前项目/addAction_add.action时候,调用add方法 -->
<result name="add">/{1}.jsp</result>
<!--当页面返回 http://localhost:8080/当前项目/addAction_update.action时候,调用add方法 -->
<result name="update">/{1}.jsp</result>
</action>
</package>
</struts>
(2)注意:
1.用下划线隔开字符串
2.用“*”表示传入的参数{1}、{2}(按下划线隔开的顺序)
3.访问 http://localhost:8080/当前项目/addAction_update.action时候,调用add方法