Struts2 redirect、redirectAction、chain区别

Chain Result:
这个result调用另外的一个action,连接自己的拦截器栈和result。

  • actionName (默认) - 被调用的action的名字
  • namespace - 被调用的action的名称空间. 如果名称空间为空,这默认为当前名称空间
  • method - 用于指定目标action的另一个方法被调用. 如果空,默认为excute方法

Redirect Action Result:
这个Result使用ActionMapperFactory提供的ActionMapper来重定位浏览器的URL来调用指定的action和(可选的)namespace. 这个Result比ServletRedirectResult要好.因为你不需要把URL编码成xwork.xml中配置的ActionMapper提供的模式. 这就是说你可以在任意点上改变URL模式而不会影响你的应用程序. 因此强烈推荐使用这个Result而不是标准的redirect result来解决重定位到某个action的情况.

  • ActionName (默认) - 重定位到的action名
  • namespace - action的名称空间. 如果为null,则为当前名称空间

<result name="topic" type="redirectAction">
     <param name="actionName">findTopics</param>
     <param name="topicId">${topicId}</param>
</result>

Redirect Result

调用{@link HttpServletResponse#sendRedirect(String) sendRedirect}方法来转到指定的位置. HTTP响应被告知使浏览器直接跳转到指定的位置(产生客户端的一个新请求). 这样做的结果会使刚刚执行的action(包括action实例,action中的错误消息等)丢失, 不再可用. 这是因为action是建立在单线程模型基础上的. 传递数据的唯一方式就是通过Session或者可以为Ognl表达式的web参数(url?name=value)

  • location (默认) - action执行后跳转的地址.
  • parse - 默认为true. 如果设置为false, location参数不会被当作Ognl表达式解析.

 

<result name="success" type="redirect">/displayCart.action?userId=${userId}</result>

*********************************************************************************************************


今天在用struts2在做项目时候,从一个action我想跳转到另一个action,并且呢得带上值。说说我的做法吧,首先你得在你的第一个action中这个id必须要有set、get方法。
跳转时你的struts.xml:
(方法一):
     <result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}</result>
(方法二):
     <result name="topic" type="redirect-action">
           <param name="actionName">findTopics</param>
           <param name="topicId">${topicId}</param>
      </result>

如果是多个参数的话,继续再加几个<param>就行了,对于(方法一)如果是多个参数的怎么办?
<result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}&amp;elementId=${elementId}</result>
这不就行了。


********************************************************************************
使用redirect重置链接需要后缀名,使用了redirectAction就不能使用了,
就例如使用chain一样,只需要写action的配置名,如果加入后缀名.action,就会报出异常,action未配置正确。

 

struts2 redirect-action 传递 参数
      在做一个系统,使用struts2框架,在提交一个请求后,将获取的数据对象再要生成一个序列号,为了防止刷新生成冗余序列号,就在请求处理完成后,直接重定向到显示该信息的action中:

<action name="enterpreinfo" class="preinfoBusinessAction" method="enterPreinfoSub">
    <result name="success" type="redirectAction">
          showpreinfo?preinfo.order_number=${preinfo.order_number}&amp;preinfo.company_name=${preinfo.company_name}
    </result>
    <result name="error" type="redirect">
        <param name="location">/error.jsp</param>
    </result>
</action>
因为使用了redirectAction,所以要注意不能将showpreinf?preinfo.order_number=${preinfo.order_number}写成showpreinf.action?preinfo.order_number=${preinfo.order_number}

在这个配置文件里,多个参数的连接符使用了"&amp;",但XML的语法规范,应该使用"&amp;"代替"&",原理和HTML中的转义相同,开始没有注意,在struts分析配置文件时,总是报出这样的错误:

The reference to entity "preinfo" must end with the ';' delimiter.

?

进行上面说明的替换后,就正常了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值