SimpleMappingExceptionResolver
Specific Exception handler
Class SimpleleMappingExceptionResolver implements interface HandlerExceptionResolver. You can implement own specific class or extends class SimpleleMappingExceptionResolver.
MailErrorHandlerException
If occure some exception it send mail.
public class ErrorHandlerController implements HandlerExceptionResolver{
public static final String DEFAULT_EXCEPTION_ATTRIBUTE = "exception";
public static final String DEFAULT_ERROR_WIEW = "error";
private MailManager mailManager;
private String from;
private String to;
private String subject;
private String defaultErrorView = DEFAULT_ERROR_WIEW;
private String exceptionAttribute = DEFAULT_EXCEPTION_ATTRIBUTE;
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
if (mailManager != null && from != null && to != null && subject != null){
sendMailToWebmaster(ex);
}
return getModelAndView(defaultErrorView, ex);
}
private void sendMailToWebmaster(Exception ex) {
mailManager.setFrom( from );
mailManager.setTo( to );
mailManager.setSubject( subject );
mailManager.setText( stack2string( ex ));
mailManager.sendMail();
}
protected ModelAndView getModelAndView(String viewName, Exception ex) {
ModelAndView mv = new ModelAndView(viewName);
mv.addObject(this.exceptionAttribute, ex);
return mv;
}
public static String stack2string(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return "------\r\n" + sw.toString() + "------\r\n";
}
public void setDefaultErrorView(String defaultErrorView) {
this.defaultErrorView = defaultErrorView;
}
public void setExceptionAttribute(String exceptionAttribute) {
this.exceptionAttribute = exceptionAttribute;
}
public void setMailManager(MailManager mailManager) {
this.mailManager = mailManager;
}
public void setFrom(String from) {
this.from = from;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setTo(String to) {
this.to = to;
}
}
No comments:
Post a Comment