struts2 从一个action 跳转到另一个action的两种方法
从action1直接跳转到action2,有两种方法:
1. 需要保存前一个action的属性信息时使用,保存住action1的request对象:
<result type= "chain " name="a2">action2</result>
2. 不保存前一个action的参数可以用这种方法:
<result type= "redirectAction "> action2</result>
Struts.xml
<?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_*" method="{1}"><!--<result type="chain" name="sa2">sa2_sa2</result> --><result type="redirectAction" name="sa2">sa2_sa2</result></action><action name="sa2_*" method="{1}"><result>/index.jsp</result></action></package></struts>
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";}}
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;}}