【问题标题】:Struts 2 - How to map a wildcard-matched method to an exception?Struts 2 - 如何将通配符匹配的方法映射到异常?
【发布时间】:2010-06-25 12:48:17
【问题描述】:

我正在使用 Struts2。这是我的 struts.xml 文件的 sn-p:

<action name="*test" class="fend.config.TestAction" method="{1}">
            <exception-mapping result="fail" exception="java.lang.UnsupportedOperationException"/>
            <result name="success">/registerCrisis.jsp</result>
            <result name="dummy">/dummy.jsp</result>
            <result name="fail">error.jsp</result>
            <interceptor-ref name="configStack"/>
        </action>

当我像这样运行应用程序时:http://localhost:8080/appContext/viewtest.action struts 调用 TestAction 类中的视图方法。我在视图方法中放置了生成 java.lang.UnsupportedOperationException 的代码,仅用于测试目的。

我的意图是重定向到名为 fail 的结果,以便显示 error.jsp。但它不会重定向到页面。我错过了什么?

谢谢。

【问题讨论】:

  • 这里只是一个勘误表,我在 sn-p 上打错了失败结果。您在哪里阅读 error.jsp 是 /error.jsp。
  • 如果返回成功,它会呈现 registerCrisis.jsp 页面吗?换句话说,你确定问题出在&lt;exception-mapping&gt;,而不是&lt;result&gt;?
  • 如果它返回成功,它会呈现 registerCrisis.jsp。但是当我得到一个 UnsupportedOperationException 时,我不知道 struts 返回什么,但它的渲染 registerCrisis.jsp。我认为随着映射异常被抛出 struts 会呈现 error.jsp 页面。例如,如果我在执行方法中遇到相同的异常,它会正确呈现 error.jsp。
  • 您能在 struts.xml 中向我们展示您的&lt;exception-mapping&gt; 片段吗?您是否将每个异常都映射到“失败”结果?
  • @leonbloy 我刚刚在 xml 中添加了这个 ,只是这个动作的内部。

标签: java exception-handling struts2


【解决方案1】:

您可以在现有操作下创建另一个操作,这可能允许其余路径(错误路径)重定向另一个操作,这可能允许其余路径(错误路径)重定向另一个页面。

<action name="*test" class="fend.config.TestAction" method="{1}">

        <result name="success">/registerCrisis.jsp</result>
        <result name="dummy">/dummy.jsp</result>
        <result name="fail">error.jsp</result>
        <interceptor-ref name="configStack"/>
    </action>

<!-- SOLUTION, ADD THIS-->

    <action name="*" method="myErrorMethod" class="fend.config.TestAction">
        <result type="redirectAction">
            <param name="namespace">/myErrorPath</param>
            <param name="actionName">myErrorMethodtest</param><!--*test (if your error method has wildcard) -->
        </result>
    </action>

【讨论】:

    猜你喜欢
    • 2013-11-27
    • 2014-05-06
    • 1970-01-01
    • 2011-08-21
    • 2011-07-25
    • 1970-01-01
    • 2016-09-10
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多