struts2开发过程中控制台不输出错误日志,log4j也没办法记录,struts2处理异常的类在 com.opensymphony.xwork2.interceptor 中, ,加上日志和控制台打印即可如下
package com.opensymphony.xwork2.interceptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.log4j.Logger;
/**
* <!-- START SNIPPET: javadoc -->
*
* A simple wrapper around an exception, providing an easy way to print out the stack trace of the exception as well as
* a way to get a handle on the exception itself.
*
* <!-- END SNIPPET: javadoc -->
*
* @author Matthew E. Porter (matthew dot porter at metissian dot com)
*/
public class ExceptionHolder {
private static final Logger LOG = Logger.getLogger(ExceptionHolder.class);
private Exception exception;
/**
* Holds the given exception
*
* @param exception the exception to hold.
*/
public ExceptionHolder(Exception exception) {
this.exception = exception;
}
/**
* Gets the holded exception
*
* @return the holded exception
*/
public Exception getException() {
return this.exception;
}
/**
* Gets the holded exception stacktrace using {@link Exception#printStackTrace()}.
*
* @return stacktrace
*/
public String getExceptionStack() {
String exceptionStack = null;
if (getException() != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try {
getException().printStackTrace(pw);
exceptionStack = sw.toString();
LOG.error(exceptionStack);
}
finally {
try {
sw.close();
pw.close();
} catch (IOException e) {
// ignore
}
}
}
return exceptionStack;
}
}
分享到:
相关推荐
NULL 博文链接:https://adrain-work-163-com.iteye.com/blog/1545091
本实例是用struts2 + log4j完成的实例,可直接用于记录向数据库插入数据,记录系统所有用户的基本操作。采用框架,利于扩展!
tomcat下struts2的log4j日志配置.doc
struts2的基础jar包,以及日志管理需要的包
struts2.5.10.1 使用的是log4j2 所以需要导入log4j-api-2.7log4j-core-2.7 这两个jar包 并且为了从log4j 平稳的过度到log4j2导入log4j-1.2-api-2.7 jar(避免修改代码)
纯struts2开发带log4j的用户登录demo,包含数据库文件
ssh2 spring hibernate struts2 log4j ant
里面包含struts2 、hibernate3、ojdbc14.jar、 log4j-1.2.13.jar slf4j-log4j12-1.6.1.jar javassist-3.11.0.GA.jar ejb3-persistence.jar几乎是最全的了
在eclipse中开发struts 2.x + log4j 1.x web项目时,只要引入这些jar包就够了,不会引起版本冲突等。
集成 struts2 hibernate spring log4j tomcat mysql 的一个简单项目 在MyEclipse8.5 tomcat 下运行成功
搭建SSH框架需要引入的Struts2 包 搭建SSH框架需要引入的Struts2 包
搭建struts hibernate log4j spring(好东西)
针对struts2漏洞,特上传log4j-core-2.7.jar和log4j-api-2.7.jar等必备工具包,同时将log4j2.xml添加到项目中,否则会有异常。
struts2 hibernate spring log4j 框架的简单列子 jar.jpg为对应jar包。lib下jar包已去除
Log4j配置详解 特别清晰,大家可以下载来参考学习,有问题可以反馈
该压缩文件包含了JavaWeb开发中常用的Struts2核心jar,MySQL的JDBC驱动jar,Oracle的JDBC驱动jar,还有log4j日志jar,MD5 jar ,文件上传下载jar,生成验证码jar--patcha。主要提供给JavaWeb阶段的学习者使用
项目中出现的异常通常要用一个友好的异常页面来显示,通过对struts2.xml的配置能拦截全局异常,只要出现异常就会转向异常页面。
struts+hibernate+spring+xdoclet+ant+log4j 技术整合,有实例参考,最好的框架!
struts2.5.10.1 使用的是log4j2 所以需要导入log4j-api-2.7和log4j-core-2.7 这两个jar包,同时将log4j2.xml放到classpath目录下
由于struts2.25.22以前的版本存在远程代码执行漏洞,故对struts进行升级,对涉及的log4j升级,主要包括log4j-core-2.6.1.jar,log4j-api-2.6.1.jar,log4j-1.2-api-2.6.1.jar