Struts 2 错误处理 - 如何获取 stackTrace 和异常属性?
我正在使用 Struts2,但我遇到了它的错误处理机制的问题。 我想做的是获取 stackTrace 和 异常 属性以便在操作类中使用它(如打印 控制台)或 JSP 页面内(不使用标签库)。
下面是我的 struts.xml 文件的片段:
<default-interceptor-ref name="defaultStack"/>
<global-results>
<result name="Exception" type="redirect">/error.action</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="Exception" exception="java.lang.Exception" />
</global-exception-mappings>
<action name="error" class="fend.ErrorAction">
<result>/error.jsp</result>
<interceptor-ref name="configStack"/>
</action>
提前致谢!
I'm working with Struts2 and I'm having an issue with it's error handling mechanism.
What I would like to do is to get the stackTrace and exception
attributes in order to use it inside an action class (as for printing on
the console) or inside a JSP page (without using taglibs).
Bellow there is a snippet of my struts.xml file:
<default-interceptor-ref name="defaultStack"/>
<global-results>
<result name="Exception" type="redirect">/error.action</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="Exception" exception="java.lang.Exception" />
</global-exception-mappings>
<action name="error" class="fend.ErrorAction">
<result>/error.jsp</result>
<interceptor-ref name="configStack"/>
</action>
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你的 JSP 中:
in your JSP:
我的设置与您相同,只是我将异常重定向到 JSP 页面 exception.jsp。
在该页面中,我有以下内容:
我知道您想避免标记库,但也许异常属性将在您的操作中可用?我记得在将异常作为异常对象传递给我的操作时遇到了麻烦。我只能将它作为字符串传递(我的记忆对此很模糊,已经有一段时间了)
最后我重定向到一个 JSP 页面,包括上面的 s:set 标记和下面的 JSP sciptlet:
记录器类获取了堆栈从 Exception 类进行跟踪并将其记录到文件中:
我记得必须完成此操作,因此这是我不得不解决一些混乱代码但它有效的情况之一...如果您认为我们有更好的东西,请告诉我
I have the same set up as you except I redirect my exception to a JSP page exception.jsp.
In that page I have the following:
I know you want to avoid tag libs but perhaps the exception property will be available in your action? I remember having trouble passing the exception to my action as an Exception Object. I couuld only pass it as a string (my memory is fuzzy on this, its been a while)
In the end I redirected to a JSP page, included the above s:set tag and the following JSP sciptlet:
The logger class got the stack trace from the Exception class and logs it to a file:
I remember having to get this done so it was one of those situations where I had to settle for some messy code but it worked... If you figure our something better let me know
您可以使用com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor以所需的日志级别(以及所需的记录器)记录异常。 (在此处阅读文档< /a>)
最简单的方法是在 struts.xml 中自定义 defaultStack ,如下所示:
You could use the com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor to log your exceptions at a desired log level (and a desired logger too). (Read the documentation here)
The easiest way would be to customize the defaultStack in your struts.xml as below: