【问题标题】:Action error is empty when using Struts 2 dispatcher result使用 Struts 2 调度程序结果时操作错误为空
【发布时间】:2013-09-06 13:59:08
【问题描述】:

当我在 Struts 2 中使用 dispatcher 时,我没有收到操作错误。

在动作类中,下面的代码用于添加错误信息。

addActionError("Error");
return "Failure";

在 Struts 配置中:

...
<result name="Failure" type="dispatcher">/ShowError.do</result>
...
<action name="ShowError">
    <result>/jsp/ShowActionErrror.jsp</result>
</action>

ShowActionErrror.jsp:

<div class="error"><s:actionerror /></div>

但是,我在ShowActionErrror.jsp 中没有收到操作错误消息?

【问题讨论】:

  • 要将调度程序与操作一起使用,您需要配置容器以通过 S2 过滤器处理转发。不过,总的来说,我建议不要这样做。

标签: java jsp struts2 error-handling actionresult


【解决方案1】:

在 JSP 中使用 dispatcher 结果类型

<result name="Failure" type="dispatcher">/jsp/ShowActionErrror.jsp</result>

【讨论】:

  • 是的,我已经尝试过使用 JSP 它工作正常。但为什么它不适合行动呢?
  • 因为调度程序不打算转发到另一个动作。
  • @john Struts2 不鼓励以任何方式链接操作,您甚至没有配置您所说的操作。查看您的问题中的代码非 Struts1 用户将意味着您使用了 dispatcher 结果与位置 /ShowError.do,而 Struts2 将不会提供它,因为它不接受转发的请求。
【解决方案2】:

Dispatcher 是默认的 Struts2 Result Type

用于执行标准行为,从Action到JSP

需要特殊结果来执行其他操作,例如 Action to Action 到 JSP,例如 RedirectAction Result、Chain Result(不鼓励)等。请注意您将丢失 Value Stack 对象(因此有 ActionErrors 和 ActionMessages)during this kind of routing

在您的情况下,您应该简单地使用默认的 Dispatcher Result Type:

<result name="Failure" type="dispatcher">/jsp/ShowActionErrror.jsp</result>

或者干脆

<result name="Failure">/jsp/ShowActionErrror.jsp</result>

阅读更多Result Configuration

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 2013-08-04
    • 2014-12-09
    相关资源
    最近更新 更多