`
13146489
  • 浏览: 252068 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Sturts2 全局异常日志

    博客分类:
  • J2EE
阅读更多
原文地址:http://www.brucephillips.name/blog/index.cfm/2009/12/8/Struts-2-Global-Exception-Handling-With-Logging
Struts 2 Global Exception Handling With Logging
Posted At : December 8, 2009 5:15 PM | Posted By : Bruce Phillips
Related Categories: Java
Introduction

One of the perks of my current job is I work with a pretty smart group of Java developers. Jeff Day, one of the programmers here at KU, showed me how to enable logging when using Struts 2's global exception handling mechanism.

Global Exception Handling

I've been using global exception handling (see ref. 3) to handle runtime exceptions my application may throw but that I don't catch explicitly. For example in struts.xml:


<global-results>
<result name="error">/error.jsp</result>
<result name="securityerror">/securityerror.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception="edu.ku.it.si.struts2_jsp_example.exceptions.SecurityBreachException" result="securityerror" />
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
Any exception that inherits from java.lang.Exception that is thrown but not caught by my application will be handled by the Struts 2 framework (specifically the ExceptionMappingInterceptor [ref. 2]) and the user's browser will be directed to error.jsp.

Enable Logging

What my clever co-worker Jeff showed me was that you can enable logging of the exceptions being handled by the Struts 2 framework by specifying some parameter values in struts.xml. If you examine the ExceptionMappingInterceptor class API (ref. 2) there are three parameter values you can set to enable logging (logEnabled), the log level to use (logLevel), and the log category (logCategory) to specify in the log message.

To set these parameter values for all actions that use a specific stack of interceptors in a package include the following in struts.xml just after the opening package node.


<interceptors>
<interceptor-stack name="appDefaultStack">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>

<default-interceptor-ref name="appDefaultStack" />
In the above xml I'm configuring a new stack of Struts 2 interceptors named appDefaultStack. This stack of interceptors is based upon the defaultStack of interceptors. The ExceptionMappingInterceptor is one of the Struts 2 interceptors that is part of the default stack (ref. 3). In the definition of the struts defaultStack, the ExceptionMappingInterceptor is given the name of exception. By specifying a param node with the name of exception.logEnabled and a value of true, I'm setting the logEnabled parameter of the ExceptionMappingInterceptor class to true.

Now when my application throws an exception that I'm not catching, the Struts 2 framework will handle it and also write an entry to my log that includes the stack trace. In my example above, I've set the level to log these exceptions to be ERROR.

Example Application

I created a simple Struts 2 web application that demonstrates using global exception handling and logging (ref. 1). The example application was created using Eclipse 3.5 with the Maven 2 plugin. You should be able to import the archive download directly into Eclipse.

If you are not using Eclipse, unzip the download and if your Java IDE supports importing Maven projects import the unzipped project.

You can run the application using the Maven command mvn jetty:run in a command (terminal) window after navigating to the project's root folder (the location of Maven's pom.xml file). When you see "[INFO] Started Jetty Server" in the command window open a web browser and load this URL: http://localhost:8080/struts2jspexample/index.jsp.

When the web page opens there will be some links you can click on to call Struts 2 actions that generate exceptions. When you click on these actions note the log message written to the Jetty console (the log4j.xml for this project just logs to the console).

To stop the Jetty server type CTRL-C in the command window.

Summary

Logging the exceptions thrown and being handled by Struts 2 global exception handling mechanism is useful during development and production. During development you could also display the exception details using the s:property value="exception" and s:property value="exceptionStack" tags in the JSP that is mapped to the exception. But of course in production you'd would not want to expose those details to the user. By setting the logging parameters Struts 2 will write the exception information to your log files.

References

Example Struts 2 Project, Global Exception Handling With Logging
Class ExceptionMappingInterceptor, Struts 2.1.8.1 API
Exception Configuration, Apache Struts 2 Documentation
Struts 2 in Action, Donald Brown, Chad Michael Davis, and Scott Stanlick, Manning (2008), pages 88-89
Apache Struts 2 Web Application Development, Dave Newton, Packt (2009), page 173, pages 193-195
Comments (1) |  Print |  Send |   del.icio.us |  Digg It! |  Linking Blogs | 3717 Views
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment] [Subscribe to Comments]
To enable your Struts 2 portlet application to log all uncaught exceptions and redirect the user to an error portlet view add the following after the package opening node:

<interceptors>
<interceptor-stack name="appDefault">
<interceptor-ref name="portletDefaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
</interceptor-stack>

</interceptors>

<default-interceptor-ref name="appDefault" />

<global-results>

<result name="error">/portlet/error.jsp</result>

</global-results>


<global-exception-mappings>

<exception-mapping exception="java.lang.Exception" result="error" />

</global-exception-mappings>

In a standard Struts 2 web application the interceptor-ref line is just <interceptor-ref name="defaultStack"> but because a Struts 2 portlet application uses a package statement that extends struts-portlet-default, the interceptor-ref must have a name value of portletDefaultStack.
# Posted By Bruce | 1/4/10 11:19 AM
分享到:
评论

相关推荐

    Struts2之异常处理案例struts003

    Struts2集成了Log4j等日志框架,开发者可以配置日志级别,以便在开发和生产环境中获取合适的日志信息。 10. **最佳实践** - 应该尽量避免在Action方法中使用`try-catch`块,而是依赖于Struts2的异常处理机制。 - ...

    struts2中异常处理(demo)

    1. **全局异常处理**:在Struts2的配置文件中,可以定义一个或多个`&lt;global-exception-mappings&gt;`标签来处理全局异常。这些映射可以指定一个特定的Action或结果来处理特定类型的异常。例如,当发生`...

    Struts的全局异常控制和common-validator校验框架

    全局异常控制是通过在Struts配置文件(struts-config.xml)中定义全局异常映射(global-exception-mappings)来实现的。在这个映射中,你可以指定当特定类型的异常发生时,Struts 应该调用哪个Action或者Forward进行...

    Struts2异常处理机制

    通过在`struts.xml`或相应的配置文件中定义全局异常处理,可以为整个应用设定统一的异常处理策略。全局异常处理通常用于处理那些没有被Action或者拦截器捕获的异常。你可以定义一个全局的结果类型(如`global-...

    struts2全局转换的问题

    在Struts2中,全局转换器(Global Converters)是一个关键特性,它允许开发者为整个应用定义统一的数据类型转换规则,而不是在每个Action类中单独配置。这个特性大大提高了代码的复用性和维护性。 标题“Struts2...

    Struts2的异常处理

    1. 全局异常处理:Struts2提供了一个`struts-default.xml`配置文件,其中可以定义一个`&lt;global-exception-mappings&gt;`标签来处理未被捕获的异常。例如,你可以为`NullPointerException`指定一个处理结果,这个结果...

    Struts2 异常处理的四种获取属性方法

    全局异常映射是Struts2配置文件(通常为struts.xml或struts-default.xml)中的一种机制,用于定义如何处理特定类型的异常。通过在`&lt;package&gt;`标签内添加`&lt;global-exception-mappings&gt;`标签,可以指定当特定异常发生...

    留言板留言板struts2留言板struts2

    9. **异常处理**:Struts2通过全局异常映射(Global Exception Mapping)来统一处理应用程序中抛出的异常,提高代码的可维护性。 10. **国际化与本地化**:Struts2支持多语言环境,可以通过资源包(properties文件...

    Struts1异常处理

    2. **Struts-config.xml配置异常处理**:在框架配置文件中,`&lt;global-exceptions&gt;`标签用于定义全局异常处理规则。例如: ```xml ``` 这段配置表示,如果任何地方抛出了`java.lang.Exception`或其子类,...

    Struts2视频教程

    - **异常处理**:配置异常拦截器,实现全局异常捕获和处理,提升用户体验。 - **文件上传与下载**:Struts2内置了对文件上传的支持,可以轻松实现文件上传功能;同时也可以配置下载功能,满足文件分发的需求。 - **...

    Struts2 Struts2 超好的Struts2 pdf 文档

    9. **异常处理**:Struts2提供了全局和局部的异常处理机制,可以统一处理应用中的异常情况,提高代码的可维护性和用户体验。 10. **国际化(i18n)**:Struts2支持多语言环境,通过资源包(Properties文件)来实现...

    Struts2的视频学习代码

    10. **异常处理**:Struts2提供了全局的异常处理机制,通过`&lt;global-exception-mappings&gt;`标签定义异常映射,可以统一处理各类运行时异常。 "Struts2-part01"可能包含的是该系列学习的第一部分内容,可能涵盖了...

    Struts2调试方法

    8. **异常处理**:Struts2提供了全局异常处理机制,当Action执行抛出异常时,可以通过全局异常处理器进行统一处理。确保异常处理器配置正确,并能捕获到预期的异常。 9. **使用开发工具**:利用浏览器的开发者工具...

    Struts2主要Lib

    9. **异常处理**:Struts2提供了一套全面的异常处理机制,通过配置`&lt;global-exception-mappings&gt;`标签,可以定义全局的异常处理策略。 在"struts2lib"这个压缩包中,可能包含的文件有`struts2-core.jar`(核心库)...

    Struts2全部jar包

    9. **异常处理**:通过全局异常处理,Struts2可以统一处理未捕获的异常,避免错误信息直接暴露给用户。 10. **类型转换**:Struts2提供了类型转换机制,自动将请求参数转换为Action类的属性类型,简化开发工作。 ...

    struts2核心技术整理

    Struts2的全局异常处理机制允许开发者定义全局的错误页面或Action,统一处理应用中抛出的异常。这通常通过`&lt;global-exception-mappings&gt;`和`&lt;global-results&gt;`配置实现。 六、插件体系 Struts2拥有丰富的插件系统...

    struts2 jar包

    9. **异常处理**:Struts2提供了一套全面的异常处理机制,可以在全局范围内捕获和处理异常,避免因未捕获异常导致的应用崩溃。 10. **测试支持**:Struts2提供了JUnit集成,方便开发者进行单元测试和集成测试。 总...

    Struts2——教程

    通过全局异常映射,开发者可以指定不同类型的异常对应不同的结果,使得错误页面的显示更加统一和规范。 九、国际化的支持 Struts2内置了对多语言的支持,可以通过资源包(properties文件)来实现界面的国际化。 十...

    Struts2实战(Struts2 In Action中文版)

    8. **异常处理**:理解Struts2的异常处理机制,如何自定义错误页面和全局异常处理器。 9. **集成其他技术**:如Spring、Hibernate等,实现业务层和服务层的松耦合。 10. **最佳实践与性能优化**:分享Struts2开发...

    黑马程序员Struts2笔记

    全局异常处理可以在struts.xml中定义,对所有Action生效;局部异常处理则可以在Action类中定义,针对特定的Action或方法。 8. **OGNL表达式语言** OGNL(Object-Graph Navigation Language)是Struts2中的默认表示...

Global site tag (gtag.js) - Google Analytics