星空网 > 软件开发 > Java

springMVC全局Exception异常处理SimpleMappingExceptionResolver

继承了SimpleMappingExceptionResolver

贴上代码

/** * 对controller异常进行全局处理 * 区分了对普通请求和ajax请求的异常处理,普通请求返回到配置的errorCode页面,或者返回到指定的页面 * @author * */public class CustomException extends SimpleMappingExceptionResolver {  private final transient Logger logger = LoggerFactory.getLogger(getClass());  @Override  protected ModelAndView doResolveException(HttpServletRequest request,      HttpServletResponse response, Object handler, Exception ex) {    String viewName = determineViewName(ex, request);    if (viewName != null) {// JSP格式返回      //增加普通提交返回到自己页面errorPage      String errorPage = String.valueOf(request.getAttribute("errorPage"));      //回到自己的页面      if(StringUtils.isNotBlank(errorPage)){        viewName = errorPage;      }      if (!(request.getHeader("accept").indexOf("application/json") > -1 || (request          .getHeader("X-Requested-With") != null && request          .getHeader("X-Requested-With").indexOf("))) {        // 如果不是异步请求        // Apply HTTP status code for error views, if specified.        // Only apply it if we're processing a top-level request.        Integer statusCode = determineStatusCode(request, viewName);        if (statusCode != null) {          applyStatusCodeIfPossible(request, response, statusCode);        }        return getModelAndView(viewName, ex, request);      } else {// JSON格式返回        try {          Map<String, Object> jsonMap = new HashMap<String, Object>();          // 返回是错误          jsonMap.put(BaseController.AJAX_RESULT, false);          jsonMap.put(BaseController.RESULT_MESSAGE, ex.getMessage());          response.setContentType("text/html;charset=UTF-8");          PrintWriter writer = response.getWriter();          writer.write(JSON.toJSONString(jsonMap));          writer.close();        } catch (Exception e) {          logger.error("doResolveException", "系统异常!", e);        }        return null;      }    } else {      return null;    }  }}

 

 spring.

<bean class="cn.tomcat.quickstart.exception.CustomException">     <!-- 定义默认的异常处理页面,当该异常类型的注册时使用 -->     <property name="defaultErrorView" value="error"></property>     <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->     <property name="exceptionAttribute" value="ex"></property>     <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值 -->     <property name="exceptionMappings">       <props>         <prop key="IOException">error/ioexp</prop>         <prop key="java.sql.SQLException">error/sqlexp</prop>       </props>     </property>   </bean> 

 




原标题:springMVC全局Exception异常处理SimpleMappingExceptionResolver

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

一个灯具卖家的跨境征程:把爱好和热情做成事业:https://www.ikjzd.com/articles/101236
2019热浪来袭,夏季如何选品?热卖榜单在这里!:https://www.ikjzd.com/articles/101238
2019亚马逊旺季销售如何做?老外4部曲详解促销技巧!:https://www.ikjzd.com/articles/101242
如何有效解决Facebook广告疲劳?:https://www.ikjzd.com/articles/101243
2019最新:Google Shopping产品销售全套教程!:https://www.ikjzd.com/articles/101244
外贸课堂:你不得不知的谷歌搜索指令用法 !:https://www.ikjzd.com/articles/101246
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流