何时在 struts2 中使用重定向和链接结果类型
在我的 struts 2 项目中,当使用重定向操作时,我会丢失所有值,例如操作错误和字段错误。
我在网上查了一下,发现了 2 个选项
- 链 - 这并没有被太多使用,我不知道为什么..
- MessageStoreInterceptor - 这需要放置在每个操作中
所以任何人都可以让我知道何时首选重定向(或 RedirectAction) 和什么时候首选链条。
In my struts 2 project when using redirect action i m loosing all my values such as action error and field errors.
I looked it up on net and found 2 options
- Chain - This isn't used much i donno why ..
- MessageStoreInterceptor - This needs to be placed in every action
So can any one please let me know when is redirect(or RedirectAction) preferred and when is chain preferred.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个选项
另一个选项
First option
Another option
重定向操作会丢失当前值堆栈(请求范围内的任何内容),您当然可以通过将这些值作为参数传递给下一个操作来设置操作来保留这些值,但这有点痛苦。
Chain 保留了值堆栈,因此下一个操作可以处理从前一个操作创建的参数,而无需显式传递它们,而且由于存在雪球效应,您可以使用视图中的所有参数。
但人们普遍认为自上而下的解决方案(也许自上而下不是最好的词......“结构化”)比构建意大利面条动作的迷宫更好。
因此,当您面临让某些东西正常工作的压力并且不太熟悉 struts2 时,请使用链或重定向,然后一定要回来修复它!一般来说,您应该使用拦截器。
如果某个操作根据某些条件路由到其他操作,最好让拦截器将其应用于包并将需要此有趣行为的所有操作放入该包中。那么这适用于哪些动作就非常清楚了。
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 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.
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.