何时在 struts2 中使用重定向和链接结果类型

发布于 2024-10-12 20:15:32 字数 231 浏览 0 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-10-19 20:15:33

第一个选项

<action name="remove" class="com.action.firstAction" method="remove">
         <result name="success" type="redirectAction">
            secondaction
            <param name="actionName">secondaction</param>
            <param name="namespace">/</param>
            <param name="param name">${param value}</param>
        </result>           
    </action>
<action name="secondaction" class="com.action.secondAction" method="result">
    <result name="success">result.jsp</result>
</action>

另一个选项

<action name="remove" class="com.action.firstAction" method="remove">
     <result name="success" type="chain">secondaction</result>
</action>
<action name="second action" class="com.action.secondAction" method="result">
    <result name="success">result.jsp</result>
</action>

First option

<action name="remove" class="com.action.firstAction" method="remove">
         <result name="success" type="redirectAction">
            secondaction
            <param name="actionName">secondaction</param>
            <param name="namespace">/</param>
            <param name="param name">${param value}</param>
        </result>           
    </action>
<action name="secondaction" class="com.action.secondAction" method="result">
    <result name="success">result.jsp</result>
</action>

Another option

<action name="remove" class="com.action.firstAction" method="remove">
     <result name="success" type="chain">secondaction</result>
</action>
<action name="second action" class="com.action.secondAction" method="result">
    <result name="success">result.jsp</result>
</action>
无声情话 2024-10-19 20:15:32

重定向操作会丢失当前值堆栈(请求范围内的任何内容),您当然可以通过将这些值作为参数传递给下一个操作来设置操作来保留这些值,但这有点痛苦。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文