首页 > 编程语言 > 详细

Spring第七篇【异常处理】

时间:2020-08-28 23:16:47      阅读:70      评论:0      收藏:0      [点我收藏+]

SimpleMappingExceptionResolver

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView" value="error.jsp"/>
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.ArithmeticException">error1.jsp</prop>
                <prop key="java.lang.NullPointerException">error2.jsp</prop>
            </props>
        </property>
    </bean>

自定义异常处理器

      <!--注册bean-->
      <bean class="com.MyExceptionResolver"/>
package com;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        if(e instanceof ArithmeticException){
            modelAndView.setViewName("error1.jsp");
        }else if(e instanceof NullPointerException){
            modelAndView.setViewName("error2.jsp");
        }else {
            modelAndView.setViewName("error.jsp");
        }
        return modelAndView;
    }
}

Spring第七篇【异常处理】

原文:https://www.cnblogs.com/kwdlh/p/13579686.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!