UserAction .java
public class UserAction extends ActionSupport{
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
public UserAction() {
request= (Map) ActionContext.getContext().get("request");
session= (Map) ActionContext.getContext().getSession();
application= (Map) ActionContext.getContext().getApplication();
}
public String add(){
request.put("request1", "request12_add");
session.put("session1", "session12_add");
application.put("application1", "application12_add");
return "add";
}
public String del(){
request.put("request1", "request12_del");
session.put("session1", "session12_del");
application.put("application1", "application12_del");
return "del";
}
public String error(){
request.put("request1", "request12_error");
session.put("session1", "session12_error");
application.put("application1", "application12_error");
return "error";
}
public String update(){
request.put("request1", "request12_update");
session.put("session1", "session12_update");
application.put("application1", "application12_update");
return "update";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" /><!-- 开发模式,修改xml文件后不需要重启tomcat,立即生效 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<package name="user" namespace="/user" extends="struts-default">
<action name="useradd" class="com.test.user.UserAction" method="add">
<!-- result的type属性默认是dispatcher 默认结果类型,用来呈现JSP页面-->
<result type="dispatcher" name="add">/useradd.jsp</result>
</action>
<action name="userdel" class="com.test.user.UserAction" method="del">
<!-- redirect将用户重定向到一个已配置好的URL-->
<result type="redirect" name="del">/userdel.jsp</result>
</action>
<action name="usererror" class="com.test.user.UserAction" method="error">
<!-- chain将action和另外一个action链接起来,这里链接到useradd的action-->
<result type="chain" name="error">useradd</result>
</action>
<action name="userupdate" class="com.test.user.UserAction" method="update">
<!-- redirectAction将用户重定向到一个已定义好的action,这里链接到userdel的action-->
<result type="redirectAction" name="update">userdel</result>
</action>
</package>
</struts>