• SpringMVC的异常处理


    1.方式一:

      springxml配置文件中定义即可,适合全局处理简单的异常,缺点不能自定义异常信息

      使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver

      

    1     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    2         <property name="exceptionMappings">
    3             <props><!-- 异常类与页面匹配    定义异常处理页面-->
    4                 <prop key="java.lang.Exception">exceptionPage</prop>
    5                 <prop key="java.lang.NumberFormatException">numberPage</prop>
    6             </props>
    7         </property>
    8     </bean>

    2.方式二:

    实现HandlerExceptionResolver 接口自定义异常处理器(缺点自己检查异常的类型)

     

     1 /**
     2  * HandlerExceptionResolver  springmvc定义的接口,自定义异常解析器需要实现该接口
     3  */
     4 @Component
     5 public class MyException implements HandlerExceptionResolver {
     6     /**
     7      * 当程序中发生了异常现象,spring会自动调用当前的自定义异常解析器对象,
     8      * 并执行resolveException方法来处理异常情况
     9      */
    10     @Override
    11     public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
    12             Exception ex) {
    13         if(ex instanceof NullPointerException){
    14             request.setAttribute("msg", "空指针异常!");
    15         }else if(ex instanceof NumberFormatException){
    16             request.setAttribute("msg", "数字格式化异常!");
    17         }else{
    18             request.setAttribute("msg", "您的操作过于频繁!");
    19         }//返回异常处理页面
    20         return new ModelAndView("myException");
    21     }
    22 
    23 }

    3.方式三:

    在方法上使用@ExceptionHandler注解,适合局部处理需要自定义异常处理过程

    定义异常处理方法

     1 /**
     2  * BaseController  定义通用的设置
     3  * @author Administrator
     4  *
     5  *    @ExceptionHandler spring提供的自定义异常注解   适合定义一些局部异常
     6  *  该异常注解所在的类和子类中有效
     7  *
     8  */
     9 public class BaseController {
    10     @ExceptionHandler(value={NullPointerException.class,NumberFormatException.class})
    11     public String handleException(Exception e,HttpServletRequest request){
    12         request.setAttribute("msg", "inner handle exception");
    13         return "innerException";
    14     }
    15 }
    @Controller
    @RequestMapping(value={"/","/person"})
    public class PersonController extends BaseController{
        @RequestMapping("/login.action")
        public String login(){
            //模拟引发异常,让spring处理
    //        Integer.parseInt("rjl");
            String str=null;
            str.length();
            return "login";
        }

    框架底层异常和无法捕获的异常处理方案:

    web.xml文件中定义此类的处理方法

    在web.xml中有两种配置error-page的方法,一是通过错误码来配置,二是通过异常的类型来配

    注意:页面处理路径为服务器路径

      <!-- 设置框架无法进行捕获的异常处理 -->
      <error-page>
          <exception-type>java.lang.Throwable</exception-type>
          <location>/WEB-INF/view/error1.jsp</location>
      </error-page>
      <!-- 配置错误状态码的异常处理 -->
      <error-page>
          <error-code>404</error-code>
          <location>/WEB-INF/view/error2.jsp</location>
      </error-page>
  • 相关阅读:
    Git centos 6.5 搭建(gitosis)
    error at ::0 can't find referenced pointcut xxx
    MySql 主从配置
    svn 搭建
    Flatbuffers学习
    python3 获取函数变量
    pyqt5 重启相同线程错误:QThread: Destroyed while thread is still running
    WIN10 使用注册表设置单应用KIOSK模式(不限win10版本)
    WIN10 使用POWERSHELL 设置单应用KIOSK模式(win10家庭版或企业版)
    py文件加密打包成exe文件
  • 原文地址:https://www.cnblogs.com/57rongjielong/p/7850928.html
Copyright © 2020-2023  润新知