Struts2 中的错误处理
我对如何在 Struts2 中进行错误处理有点困惑。 我希望制作一个中央页面,如果发生错误,用户将被引导到该页面。 此外,当发生错误时,我希望记录它,因为我使用的是 log4j,所以我会将其记录为 log.error(e.getMessage(), e);
但是,在我的操作类中如果我捕获错误(将所有代码放入 try/catch 中),那么中央/公共错误页面不会出现。 所以我决定不捕获错误,如果我没有捕获错误,则会出现中央错误页面。 但现在如何将错误消息/堆栈跟踪放入日志中?
阅读此链接后 我做了以下操作:
<global-results>
<result name="Exception" type="chain">
<param name="actionName">ErrorPage</param>
<param name="namespace">/error</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="Exception"/>
</global-exception-mappings>
<action name="selectionPage" class="reports.ReportSelection">
<result>/reports/SelectionPage.jsp</result>
</action>
</package>
<package name="secure" namespace="/error">
<action name="ErrorPage" class="com.myErrorClass">
<result>errorpage.jsp</result>
</action>
</package>
根据上面的配置,最初错误是在 reports.ReportSelection 中抛出的(但我没有在那里捕获它),所以最后控制权转到 com.myErrorClass 。 我可以记录此类中的错误,但我的问题是,日志消息是否仍然可用......因为它最初是在 reports.ReportSelection 中抛出的?
I am a little confused on how to Error handling in Struts2. I wish to make once central page where the users will be directed if an error occurs. Furthermore, when an error occurs i wish to log it, since i am using log4j I'll be logging it as log.error(e.getMessage(), e);
However, in my action class if I catch the error (put all my code in try/catch) then the central/common error page does not come up. So I decided against catching the error, if i dont catch the error then central error page comes up. But now how do I put the error message/stacktrack into the logs??
After reading this link
I did the following:
<global-results>
<result name="Exception" type="chain">
<param name="actionName">ErrorPage</param>
<param name="namespace">/error</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="Exception"/>
</global-exception-mappings>
<action name="selectionPage" class="reports.ReportSelection">
<result>/reports/SelectionPage.jsp</result>
</action>
</package>
<package name="secure" namespace="/error">
<action name="ErrorPage" class="com.myErrorClass">
<result>errorpage.jsp</result>
</action>
</package>
According to the above configuration, originally the error is thrown in reports.ReportSelection (but I am not catching it there) so finally the control comes to com.myErrorClass. I CAN log the errors in this class but my question is, whether the log message still be available...since it was originally thrown in reports.ReportSelection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为替代方案,您还可以“覆盖”defaultStack 拦截器堆栈
As an alternative, you can also "overwrite" the defaultStack interceptor stack
在你捕获并记录它之后,你会重新处理它吗? 如果这样做,那么框架异常管理应该启动。您的错误处理代码应该类似于:
有了它,您应该能够返回到在操作类中记录和重新记录它的简化方法,并配置一个单个全局错误页面。
After you catch and log it, are you retrhowing it? If you do, then the framework exception management should kick in. Your error handling code should look something like:
With that in place you should be able to go back to your simplified approach of logging and retrhowing it in the action class, and configuring a single global error page.
以下是记录您的操作引发的错误的方法。 我不知道为什么默认情况下没有打开此功能。 将其放入 struts.xml 文件中。
您不需要在每个操作方法周围都有 try-catch 块。
Here is how you log errors that your actions throw. I don't know why this isn't turned on by default. Put this in your struts.xml file.
You don't need to have try-catch blocks around every action method.