最近遇到了严重的问题,搞了几天无法保存异常信息到数据库。
在dao、service等注入而使用的环境下,如何才能在异常中处理类中调用这些业务方法来存取异常信息。
对struts2.0+hibernate+spring整合比较熟,但是对struts1.2+hibernate+spring真的是无奈能解决,郁闷,唉,到了几天都。public class ExceptionHandler extends org.apache.struts.action.ExceptionHandler{
/**
* Commons logging instance.
*/
protected Logger log = Logger.getLogger(ExceptionHandler.class); public final static String EXCEPTION_STACKTRACE = "errStackTrace";
protected AdminFacadeManager adminFacadeManager=new AdminFacadeManagerImpl(); //方法1new一个实例,
// 还是用不了
/*public void setAdminFacadeManager(AdminFacadeManager adminFacadeManager) {
System.out.println("@@@@@@@@@@@@@@@@@@@@@adminFacadeManager注入ExceptionHandler成功!!!!!");
this.adminFacadeManager = adminFacadeManager; //方法2注入不成功 }*/ /*public ExceptionManager getExceptionManager() {
return exceptionManager;
} public void setExceptionManager(ExceptionManager exceptionManager) {
System.out.println("ExceptionHandler注入ExceptionManager成功!!!!!");
this.exceptionManager = exceptionManager;
}*/ public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
ActionForward forward;
ActionMessage error;
String property;
MaskExceptionModel maskExceptionModel;
ExceptionManager exceptionManager=new ExceptionManagerImpl(); // Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
error = new ActionMessage(ae.getKey(), ex.getMessage());
property = error.getKey();
} // Logging the exception using log4j
log.error("An Exception logged!", ex); // Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
request.setAttribute(EXCEPTION_STACKTRACE, ex + "\r\n"
+ stackTrace2String(ex.getStackTrace()));
storeException(request, property, error, forward, ae.getScope()); String type="java.lang.Exception";
String message=ex.getMessage();
String causeStackTrace=ex.getCause().toString();
String rootStackTrace=this.stackTrace2String(ex.getStackTrace());
String messageKey=ex.getCause().toString();
String requestedUrl=request.getRequestURL()+"?" + request.getQueryString();
String clientInfo="";
String raiserId="";
Date thrownDate=new Date();
System.out.println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
System.out.println("type=="+type+"XXXXXXXXXXXXXXXXX");
System.out.println("ex.getMessage()=="+ex.getMessage()+"XXXXXXXXXXXXXXXXX");
System.out.println("causeStackTrace=ex.getCause().toString()=="+ex.getCause().toString()+"XXXXXXXXXXXXXXXXX");
System.out.println("rootStackTrace=ex.getStackTrace().toString()=="+this.stackTrace2String(ex.getStackTrace())+"XXXXXXXXXXXXXXXXX");
System.out.println("messageKey=ex.getCause().toString()=="+ex.getCause().toString()+"XXXXXXXXXXXXXXXXX");
System.out.println("requestedUrl=="+request.getRequestURL()+"?" + request.getQueryString()+"XXXXXXXXXXXXXXXXX");
System.out.println("clientInfo=="+clientInfo+"XXXXXXXXXXXXXXXXX");
System.out.println("raiserId=="+raiserId+"XXXXXXXXXXXXXXXXX");
System.out.println("thrownDate=="+thrownDate+"XXXXXXXXXXXXXXXXX");
System.out.println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
maskExceptionModel=new MaskExceptionModel("java.lang.Exception", message,
"", rootStackTrace, "",requestedUrl,clientInfo,raiserId,null); adminFacadeManager.saveException(maskExceptionModel);
就是这里保存时,显示dao.save(object)空指针异常
if (!response.isCommitted()) {
return forward;
}
return null;
} protected void storeException(HttpServletRequest request, String property,
ActionMessage error, ActionForward forward, String scope) {
ActionMessages errors = new ActionMessages(); errors.add(property, error); if ("request".equals(scope)) {
request.setAttribute(Globals.ERROR_KEY, errors);
} else {
request.getSession().setAttribute(Globals.ERROR_KEY, errors);
}
} protected String stackTrace2String(StackTraceElement[] stackTrace) {
StringBuffer stackString = new StringBuffer(0);
for (int i = 0; i < stackTrace.length; i++) {
stackString.append(stackTrace[i].toString()).append("\r\n");
}
return stackString.toString();
}}希望高手快来帮忙。郁闷啊。我感之不尽
在dao、service等注入而使用的环境下,如何才能在异常中处理类中调用这些业务方法来存取异常信息。
对struts2.0+hibernate+spring整合比较熟,但是对struts1.2+hibernate+spring真的是无奈能解决,郁闷,唉,到了几天都。public class ExceptionHandler extends org.apache.struts.action.ExceptionHandler{
/**
* Commons logging instance.
*/
protected Logger log = Logger.getLogger(ExceptionHandler.class); public final static String EXCEPTION_STACKTRACE = "errStackTrace";
protected AdminFacadeManager adminFacadeManager=new AdminFacadeManagerImpl(); //方法1new一个实例,
// 还是用不了
/*public void setAdminFacadeManager(AdminFacadeManager adminFacadeManager) {
System.out.println("@@@@@@@@@@@@@@@@@@@@@adminFacadeManager注入ExceptionHandler成功!!!!!");
this.adminFacadeManager = adminFacadeManager; //方法2注入不成功 }*/ /*public ExceptionManager getExceptionManager() {
return exceptionManager;
} public void setExceptionManager(ExceptionManager exceptionManager) {
System.out.println("ExceptionHandler注入ExceptionManager成功!!!!!");
this.exceptionManager = exceptionManager;
}*/ public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
ActionForward forward;
ActionMessage error;
String property;
MaskExceptionModel maskExceptionModel;
ExceptionManager exceptionManager=new ExceptionManagerImpl(); // Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
error = new ActionMessage(ae.getKey(), ex.getMessage());
property = error.getKey();
} // Logging the exception using log4j
log.error("An Exception logged!", ex); // Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
request.setAttribute(EXCEPTION_STACKTRACE, ex + "\r\n"
+ stackTrace2String(ex.getStackTrace()));
storeException(request, property, error, forward, ae.getScope()); String type="java.lang.Exception";
String message=ex.getMessage();
String causeStackTrace=ex.getCause().toString();
String rootStackTrace=this.stackTrace2String(ex.getStackTrace());
String messageKey=ex.getCause().toString();
String requestedUrl=request.getRequestURL()+"?" + request.getQueryString();
String clientInfo="";
String raiserId="";
Date thrownDate=new Date();
System.out.println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
System.out.println("type=="+type+"XXXXXXXXXXXXXXXXX");
System.out.println("ex.getMessage()=="+ex.getMessage()+"XXXXXXXXXXXXXXXXX");
System.out.println("causeStackTrace=ex.getCause().toString()=="+ex.getCause().toString()+"XXXXXXXXXXXXXXXXX");
System.out.println("rootStackTrace=ex.getStackTrace().toString()=="+this.stackTrace2String(ex.getStackTrace())+"XXXXXXXXXXXXXXXXX");
System.out.println("messageKey=ex.getCause().toString()=="+ex.getCause().toString()+"XXXXXXXXXXXXXXXXX");
System.out.println("requestedUrl=="+request.getRequestURL()+"?" + request.getQueryString()+"XXXXXXXXXXXXXXXXX");
System.out.println("clientInfo=="+clientInfo+"XXXXXXXXXXXXXXXXX");
System.out.println("raiserId=="+raiserId+"XXXXXXXXXXXXXXXXX");
System.out.println("thrownDate=="+thrownDate+"XXXXXXXXXXXXXXXXX");
System.out.println("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
maskExceptionModel=new MaskExceptionModel("java.lang.Exception", message,
"", rootStackTrace, "",requestedUrl,clientInfo,raiserId,null); adminFacadeManager.saveException(maskExceptionModel);
就是这里保存时,显示dao.save(object)空指针异常
if (!response.isCommitted()) {
return forward;
}
return null;
} protected void storeException(HttpServletRequest request, String property,
ActionMessage error, ActionForward forward, String scope) {
ActionMessages errors = new ActionMessages(); errors.add(property, error); if ("request".equals(scope)) {
request.setAttribute(Globals.ERROR_KEY, errors);
} else {
request.getSession().setAttribute(Globals.ERROR_KEY, errors);
}
} protected String stackTrace2String(StackTraceElement[] stackTrace) {
StringBuffer stackString = new StringBuffer(0);
for (int i = 0; i < stackTrace.length; i++) {
stackString.append(stackTrace[i].toString()).append("\r\n");
}
return stackString.toString();
}}希望高手快来帮忙。郁闷啊。我感之不尽
解决方案 »
- 对于同一个程序进行junit断点调试,一个方法测试正常,另一个方法总是直接跳入Class File Editor,如何解决???
- 页面跳转问题求救。
- quartz任务调度中,使每次执行的任务sleep() 30秒如何做到?
- 小小菜鸟做的微博客户端(含源代码)。
- 八个J2EE的高级群,按层次划分,共享教程代码,期待各位朋友加入(需考试)
- 小声的问一下:当今用java做桌面程序的多不多啊?哪些桌面程序是用java做的啊?
- 这段代码怎么改才能文字在左边,图片在右边,下面的“详细内容”居中,而且点“详细内容”另外新打开一个相应的页?
- 怎么在JbuliderX使用Oracle AS 10g?
- 招聘j2ee程序员,工作地点在深圳
- proguard 之后报java.io.FileNotFoundException
- SSH电子商务网站论文请教
- 求助:spring3+hibernate注解dao为null
我在异常用注入不了业务方法,new也不行。烦啊。
好像struts1比不上struts2好用,它的异常处理如果想保存异常信息到数据库里面,应当怎样做???
能用得上spring把业务方法注入到异常处理类里面吗?