带参数的Action跳转

常用的Action跳转是在struts-config.xml文件中定义了forward标记后在Action内部通过ActionForward对象来跳转。但是有时后想在跳转的时候带上参数,例如说从Action1跳转到Action2,并且带上Action2所需的参数,那么应该怎么实现?首先要明确一点:在struts-config.xml文件中是不能通过forward标记的配置来带参数的,例如下面所写是不合法的:

 

   <action

      path="/addStuAction"

      type="com.test.manager.MyAction">

      <forward name="addStuSucess" path="/index.jsp?stuNo=stuNo" />
 

 

 Google了一下,有以下两种解决思路(以上面所说的配置为例):

 (一)、在Action内部代码使用新的ActionForward对象来跳转:

 

 

//获取在struts-config.xml文件中配置的路径

String path = mapping.findForward("addStuSucess").getPath();

//添加参数

ActionForward forward = new ActionForward(path + "?stuNo=stuNo“);

//跳转

forward .freeze();

return forward;
 

(二)、在Action内部代码中仍旧使用原来的ActionForward对象来跳转,但是在跳转前用requst.setAttribute()添加参数:

 

//为请求添加参数

requst.setAttribute(”stuNo“,”stuNo“);

mapping.findForward("addStuSucess"); 

你可能感兴趣的:(jsp,xml,struts)