例如从你的login.action到register.action 有两种实现方式
1. 设置type="redirect"
<package name="struts" extends="struts-default">
<action name="login" class="com.aihua.example.LoginAction">
<result name="success" type="redirect">register.action </result>
<result name="error">/index.jsp </result>
</action>
</package>
2.设置 type="chain"
<package name="struts" extends="struts-default">
<action name="login" class="com.aihua.example.LoginAction">
<result name="success" type="chain" >action 名称 </result>
<result name="error">/index.jsp </result>
</action>
</package>
当type为chain 时,说明是action链,运行完第一个action java文件接着会运行第二个action JAVA 文件,相当于forward(客户端的url不会改变).当type为redirect时,说明会跳转到第二个action的url (客户端的url会改变)
从action1直接跳转到action2,有两种方法:
1. 需要保存前一个action的属性信息时使用,保存住action1的request对象:
<result type= "chain " name="a2">action2</result>
2. 不保存前一个action的参数可以用这种方法:
<result type= "redirectAction "> action2</result>
Struts.xml
Java代码
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <!--一个action跳转到另一个action 跳转方式为chain 会保留request对象 redirectAction 则不能保留-->
- <package name="limin" extends="struts-default">
- <action name="sa1_*" class="com.Action.StrAction1" method="{1}">
- <!--<result type="chain" name="sa2">sa2_sa2</result> -->
- <result type="redirectAction" name="sa2">sa2_sa2</result>
- </action>
- <action name="sa2_*" class="com.Action.StrAction2" method="{1}">
- <result>/index.jsp</result>
- </action>
- </package>
- </struts>
[java] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <!--一个action跳转到另一个action 跳转方式为chain 会保留request对象 redirectAction 则不能保留-->
- <package name="limin" extends="struts-default">
- <action name="sa1_*" class="com.Action.StrAction1" method="{1}">
- <!--<result type="chain" name="sa2">sa2_sa2</result> -->
- <result type="redirectAction" name="sa2">sa2_sa2</result>
- </action>
- <action name="sa2_*" class="com.Action.StrAction2" method="{1}">
- <result>/index.jsp</result>
- </action>
- </package>
- </struts>
action1
Java代码
- public class StrAction1 {
- String a ="liminhappygirl";
- public String getA() {
- return a;
- }
- public void setA(String a) {
- this.a = a;
- }
- public String sa1() {
- HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
- request.setAttribute("limin", "happygirl");
- System.out.println("sal已经执行");
- return "sa2";
- }
- }
[java] view plain copy print ?
- public class StrAction1 {
- String a ="liminhappygirl";
- public String getA() {
- return a;
- }
- public void setA(String a) {
- this.a = a;
- }
- public String sa1() {
- HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
- request.setAttribute("limin", "happygirl");
- System.out.println("sal已经执行");
- return "sa2";
- }
- }
action2
Java代码
- public class StrAction2 {
- public String sa2() {
- HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
- //当type="chain" 时能保存住request对象
- System.out.println(request.getAttribute("limin"));
- return Action.SUCCESS;
- }
- }
[java] view plain copy print ?
- public class StrAction2 {
- public String sa2() {
- HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
- //当type="chain" 时能保存住request对象
- System.out.println(request.getAttribute("limin"));
- return Action.SUCCESS;
- }
- }