码迷,mamicode.com
首页 > Web开发 > 详细

web项目中处理捕获异常统一处理

时间:2018-02-12 18:40:25      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:log   web   case   eth   https   erro   ror   系统   print   

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 }
View Code

 

web项目中处理捕获异常统一处理

标签:log   web   case   eth   https   erro   ror   系统   print   

原文地址:https://www.cnblogs.com/bug1024/p/8445080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!