1.jfinal 中设置拦截器
2.在拦截器 捕获异常信息统一处理
1 public class ExceptionInterceptor implements Interceptor { 2 3 //@Override 4 public void intercept(Invocation ai) { 5 BaseController controller = (BaseController) ai.getController(); 6 HttpServletRequest request = controller.getRequest(); 7 boolean successed = false; 8 String exception = ""; 9 try { 10 ai.invoke(); 11 successed = true; 12 } catch (Exception e) { 13 e.printStackTrace(); 14 successed = false; 15 // 判断是否ajax请求 16 String header = request.getHeader("X-Requested-With"); 17 boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header); 18 String msg = e.getMessage(); 19 exception = Exceptions.getStackTraceAsString(e); 20 controller.keepPara(); 21 if (isAjax) { 22 msg = new StringBuilder().append("{\"source\":\"ExceptionInterceptor\",\"code\":\"1\",\"message\":\"系统异常:").append(msg).append("\"}").toString(); 23 controller.renderJson(msg); 24 } else { 25 String redirctUrl = request.getHeader("referer"); 26 if (StringUtils.isBlank(redirctUrl)) { 27 redirctUrl = request.getRequestURI(); 28 } 29 controller.setAttr("message", msg); 30 controller.setAttr("stack", exception); 31 controller.setAttr("redirctUrl", redirctUrl); 32 controller.render("/WEB-INF/views/error/500.jsp"); 33 } 34 } finally { 35 36 } 37 return; 38 39 } 40 41 }