- 浏览: 246103 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 1084只要这样配置好久能使用gmail了 <bean id= ... -
log4j 常用配置
2012-03-22 21:02 1044原文地址:http://www.benmccann.com/d ... -
Dependency Injection - An Introductory Tutorial - Part 1
2012-02-20 10:57 1177原文地址:http://code.google.com/p/j ... -
struts2 排除拦截部分路径
2011-11-30 13:27 5741情况:在web.xml中配置一个servlet映射路径为/te ... -
java image scale
2011-07-20 13:47 896http://code.google.com/p/java-i ... -
实现自定义截取图片
2011-07-13 17:30 1058几种插件: http://odyniec.net/projec ... -
jms基础概念和应用场景
2011-07-01 13:55 1572原文地址:http://blog.csdn.net/KimmK ... -
Envers –tracked your Entity Objects
2011-06-29 09:05 1530原文地址:http://get2java.wordpress. ... -
JSON Compression algorithms: HPack VS CJSON
2011-06-28 17:24 3091总结:HPack 优于 CJSON json1 json2 ... -
Why we don’t use Doubles for Financial Calculations
2011-06-27 11:14 1244原文地址:http://bloodredsun.com/?p= ... -
http://jbaruch.wordpress.com/2011/06/22/unified-logging-using-slf4j/
2011-06-27 10:48 1181原文地址:http://jbaruch.wordpress.c ... -
eclipse的build与clean
2011-06-22 09:01 1542现象:无论怎么改变代码,程序的行为始终不变,一直报错。。。。 ... -
jfreechart 图标各部分名字
2011-06-09 19:57 794图标各部分名字 -
jfreechart 自定义饼图颜色
2011-06-09 18:52 4826关键代码: private static class Pi ... -
jfreechart demo 代码
2011-06-07 18:33 2919jfreechart 官方demo 源代码 -
jfreechart 中文问题
2011-06-07 16:52 902基本如果将jfreechart生成图片的字体多改成中文的字体就 ... -
SVN 安装
2011-05-30 15:45 987collabnet svn 现在出了整和的SVN EDGE。这 ... -
amazon 云计算
2011-05-28 21:45 1039最近看了amazon的云计算 ... -
c3p0 java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
2011-05-28 16:07 1555修改日志级别为info http://hi.baidu.com ... -
mybatis打印sql
2011-05-09 15:03 19892mybatis默认使用log4j,当有self4j这个日志ja ...
相关推荐
项目中出现的异常通常要用一个友好的异常页面来显示,通过对struts2.xml的配置能拦截全局异常,只要出现异常就会转向异常页面。
Struts的全局异常控制和common-validator校验框架.doc
NULL 博文链接:https://johnson2132.iteye.com/blog/720812
NULL 博文链接:https://cdxs2.iteye.com/blog/1926354
struts2的异常处理,解决异步请求时,异常以json格式发送到客户端。以及dao单元测试的配置。
Struts2异常处理机制
struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别...
struts 初步认识Struts2并部署验证 struts2 将要进行自定义类型转换的点数据从1个增加到3个 struts3 进行全局的类型转换 struts4 使用Struts2中内部类中的方法进行自定义类型转换,用到的类StrutsTypeConverter ...
struts2 的异常收集
配合博文:http://blog.csdn.net/u011638419/article/details/41343683
NULL 博文链接:https://adrain-work-163-com.iteye.com/blog/1545091
struts2一些异常和如何搭struts2项目 非常详细我在开发中总结的 ,也可能有点乱,
NULL 博文链接:https://yuxisanren.iteye.com/blog/1707508
在struts2中使用result里type="stream"的结果类型时,可以实现文件的下载管理,使用时也是比较顺畅,但是当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常,异常内容...
本文档主要介绍了struts2的异常处理机制,希望对你有所帮助。
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全...
struts aop日志系统,struts aop日志系统
struts2 资料struts2 资料struts2 资料struts2 资料
Struts2是一套非常优秀的Java WEB应用框架,实现优雅、功能强大、使用简洁。目前已有大量的企业项目采用Struts2来作为Web框架进行开发,掌握Struts2是每个进行Web开发的Java程序员的一项必备技能。 本视频对Struts2...