何时在 struts2 中使用重定向和链接结果类型 [英] When to use redirect and chain result types in struts2

查看:33
本文介绍了何时在 struts2 中使用重定向和链接结果类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 struts 2 项目中,当使用重定向操作时,我丢失了所有值,例如操作错误和字段错误.

In my struts 2 project when using redirect action i m loosing all my values such as action error and field errors.

我在网上查了一下,找到了 2 个选项

I looked it up on net and found 2 options

  • 链 - 这不常用我不知道为什么..
  • MessageStoreInterceptor - 这需要放在每个动作中

那么任何人都可以告诉我什么时候首选重定向(或 RedirectAction),什么时候首选链式.

So can any one please let me know when is redirect(or RedirectAction) preferred and when is chain preferred.

推荐答案

重定向操作会丢失当前值堆栈(请求范围内的任何内容)您当然可以设置您的操作以通过将它们作为参数传递给下一个动作,不过有点痛.

Redirecting an action looses the current value stack (anything in request scope) you can of course set up your action to preserve these values by passing them as parameters to the next action, but it is a bit of a pain.

Chain 保留了值堆栈,因此下一个操作可以处理从前一个操作创建的参数,而无需显式传递它们,而且由于存在这种雪球效应,您可以使用视图中的所有参数.

Chain preserves the value stack, so the next action can work on parameters created from the previous action without needing to explicitly pass them, also since there is this snow ball effect you can use all the parameters in the view.

但人们普遍认为,自上而下的解决方案(也许自上而下不是最好的词......结构化")比构建意大利面条式动作的迷宫要好.

But it is generally recognized that a top down solution (maybe top down isn't the best word... 'structured') is better than building a maze of spaghetti actions.

因此,当您面临让某些东西工作的压力并且对 struts2 不太熟悉时,请使用链或重定向,然后肯定会回来修复它!一般来说,你应该使用拦截器.

So when you're under pressure to get something working and not overly familiar with struts2 then use chain or redirection, and then definitely come back and fix it! In general you should use an interceptor.

如果一个动作根据某些条件路由到其他动作,最好让拦截器将其应用于包并将所有需要这种有趣行为的动作放在该包中.然后很清楚这适用于哪些操作.

In the event of an action that routes to other actions based on some condition it would be better to make that an interceptor apply that to a package and put all actions which require this interesting behavior in that package. Then it is very clear which actions this applies to.

这篇关于何时在 struts2 中使用重定向和链接结果类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆