- 浏览: 251402 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
wilsonchen:
...
pdf.js html5展现pdf -
angole:
不错,又学习到了。
mybatis打印sql -
hft24dq:
rxcss66 写道 个人没有搞明白什么原理,不过跟一楼说的一 ...
mybatis打印sql -
fireinjava:
fireinjava 写道org.apache.ibatis. ...
mybatis打印sql -
fireinjava:
org.apache.ibatis.logging.LogFa ...
mybatis打印sql
原文地址: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
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
发表评论
-
spring send gmail
2012-04-24 11:06 1147只要这样配置好久能使用gmail了 <bean id= ... -
log4j 常用配置
2012-03-22 21:02 1089原文地址:http://www.benmccann.com/d ... -
Dependency Injection - An Introductory Tutorial - Part 1
2012-02-20 10:57 1208原文地址:http://code.google.com/p/j ... -
struts2 排除拦截部分路径
2011-11-30 13:27 5781情况:在web.xml中配置一个servlet映射路径为/te ... -
java image scale
2011-07-20 13:47 930http://code.google.com/p/java-i ... -
实现自定义截取图片
2011-07-13 17:30 1106几种插件: http://odyniec.net/projec ... -
jms基础概念和应用场景
2011-07-01 13:55 1605原文地址:http://blog.csdn.net/KimmK ... -
Envers –tracked your Entity Objects
2011-06-29 09:05 1575原文地址:http://get2java.wordpress. ... -
JSON Compression algorithms: HPack VS CJSON
2011-06-28 17:24 3155总结:HPack 优于 CJSON json1 json2 ... -
Why we don’t use Doubles for Financial Calculations
2011-06-27 11:14 1295原文地址:http://bloodredsun.com/?p= ... -
http://jbaruch.wordpress.com/2011/06/22/unified-logging-using-slf4j/
2011-06-27 10:48 1212原文地址:http://jbaruch.wordpress.c ... -
eclipse的build与clean
2011-06-22 09:01 1578现象:无论怎么改变代码,程序的行为始终不变,一直报错。。。。 ... -
jfreechart 图标各部分名字
2011-06-09 19:57 837图标各部分名字 -
jfreechart 自定义饼图颜色
2011-06-09 18:52 4900关键代码: private static class Pi ... -
jfreechart demo 代码
2011-06-07 18:33 2953jfreechart 官方demo 源代码 -
jfreechart 中文问题
2011-06-07 16:52 944基本如果将jfreechart生成图片的字体多改成中文的字体就 ... -
SVN 安装
2011-05-30 15:45 1020collabnet svn 现在出了整和的SVN EDGE。这 ... -
amazon 云计算
2011-05-28 21:45 1080最近看了amazon的云计算 ... -
c3p0 java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
2011-05-28 16:07 1589修改日志级别为info http://hi.baidu.com ... -
mybatis打印sql
2011-05-09 15:03 20013mybatis默认使用log4j,当有self4j这个日志ja ...
相关推荐
Struts2集成了Log4j等日志框架,开发者可以配置日志级别,以便在开发和生产环境中获取合适的日志信息。 10. **最佳实践** - 应该尽量避免在Action方法中使用`try-catch`块,而是依赖于Struts2的异常处理机制。 - ...
1. **全局异常处理**:在Struts2的配置文件中,可以定义一个或多个`<global-exception-mappings>`标签来处理全局异常。这些映射可以指定一个特定的Action或结果来处理特定类型的异常。例如,当发生`...
全局异常控制是通过在Struts配置文件(struts-config.xml)中定义全局异常映射(global-exception-mappings)来实现的。在这个映射中,你可以指定当特定类型的异常发生时,Struts 应该调用哪个Action或者Forward进行...
通过在`struts.xml`或相应的配置文件中定义全局异常处理,可以为整个应用设定统一的异常处理策略。全局异常处理通常用于处理那些没有被Action或者拦截器捕获的异常。你可以定义一个全局的结果类型(如`global-...
在Struts2中,全局转换器(Global Converters)是一个关键特性,它允许开发者为整个应用定义统一的数据类型转换规则,而不是在每个Action类中单独配置。这个特性大大提高了代码的复用性和维护性。 标题“Struts2...
1. 全局异常处理:Struts2提供了一个`struts-default.xml`配置文件,其中可以定义一个`<global-exception-mappings>`标签来处理未被捕获的异常。例如,你可以为`NullPointerException`指定一个处理结果,这个结果...
全局异常映射是Struts2配置文件(通常为struts.xml或struts-default.xml)中的一种机制,用于定义如何处理特定类型的异常。通过在`<package>`标签内添加`<global-exception-mappings>`标签,可以指定当特定异常发生...
9. **异常处理**:Struts2通过全局异常映射(Global Exception Mapping)来统一处理应用程序中抛出的异常,提高代码的可维护性。 10. **国际化与本地化**:Struts2支持多语言环境,可以通过资源包(properties文件...
2. **Struts-config.xml配置异常处理**:在框架配置文件中,`<global-exceptions>`标签用于定义全局异常处理规则。例如: ```xml ``` 这段配置表示,如果任何地方抛出了`java.lang.Exception`或其子类,...
- **异常处理**:配置异常拦截器,实现全局异常捕获和处理,提升用户体验。 - **文件上传与下载**:Struts2内置了对文件上传的支持,可以轻松实现文件上传功能;同时也可以配置下载功能,满足文件分发的需求。 - **...
9. **异常处理**:Struts2提供了全局和局部的异常处理机制,可以统一处理应用中的异常情况,提高代码的可维护性和用户体验。 10. **国际化(i18n)**:Struts2支持多语言环境,通过资源包(Properties文件)来实现...
10. **异常处理**:Struts2提供了全局的异常处理机制,通过`<global-exception-mappings>`标签定义异常映射,可以统一处理各类运行时异常。 "Struts2-part01"可能包含的是该系列学习的第一部分内容,可能涵盖了...
8. **异常处理**:Struts2提供了全局异常处理机制,当Action执行抛出异常时,可以通过全局异常处理器进行统一处理。确保异常处理器配置正确,并能捕获到预期的异常。 9. **使用开发工具**:利用浏览器的开发者工具...
9. **异常处理**:Struts2提供了一套全面的异常处理机制,通过配置`<global-exception-mappings>`标签,可以定义全局的异常处理策略。 在"struts2lib"这个压缩包中,可能包含的文件有`struts2-core.jar`(核心库)...
9. **异常处理**:通过全局异常处理,Struts2可以统一处理未捕获的异常,避免错误信息直接暴露给用户。 10. **类型转换**:Struts2提供了类型转换机制,自动将请求参数转换为Action类的属性类型,简化开发工作。 ...
Struts2的全局异常处理机制允许开发者定义全局的错误页面或Action,统一处理应用中抛出的异常。这通常通过`<global-exception-mappings>`和`<global-results>`配置实现。 六、插件体系 Struts2拥有丰富的插件系统...
9. **异常处理**:Struts2提供了一套全面的异常处理机制,可以在全局范围内捕获和处理异常,避免因未捕获异常导致的应用崩溃。 10. **测试支持**:Struts2提供了JUnit集成,方便开发者进行单元测试和集成测试。 总...
通过全局异常映射,开发者可以指定不同类型的异常对应不同的结果,使得错误页面的显示更加统一和规范。 九、国际化的支持 Struts2内置了对多语言的支持,可以通过资源包(properties文件)来实现界面的国际化。 十...
8. **异常处理**:理解Struts2的异常处理机制,如何自定义错误页面和全局异常处理器。 9. **集成其他技术**:如Spring、Hibernate等,实现业务层和服务层的松耦合。 10. **最佳实践与性能优化**:分享Struts2开发...
全局异常处理可以在struts.xml中定义,对所有Action生效;局部异常处理则可以在Action类中定义,针对特定的Action或方法。 8. **OGNL表达式语言** OGNL(Object-Graph Navigation Language)是Struts2中的默认表示...