spring mvc 异常处理机制

一 异常处理逻辑

1.1 处理的流程

系统的Dao、service、controller出现都能通过throws Exception向上抛,最后由spring mvc的前端控制器交给异常处理器进行处理。

1.2 处理的方法有

1.使用spring mvc 的简单处理器simpleMappingExceptionResolver

2.自定义处理器:实现spring的异常处理接口HandlerExceptionResolver自定义的异常处理器。

二 springmvc 中异常处理案例

2.1 使用springmvc 框架异常处理器

1.controller

package com.ljf.spring.mvc.demo.controller;

import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @ClassName: ExceptionHandler
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/01/17 10:55:06 
 * @Version: V1.0
 **/
@Controller
public class ExceptionHandler  {

    @RequestMapping("/exception")
    public  void  exceptionDemo() throws Exception{
        System.out.println("空指针异常:");
        List list=null;
        list.get(0);

    }
}

2.springmvc配置文件

 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
     <property name="defaultErrorView" value="error"></property>
        <property name="exceptionMappings">
            <map>
                 <!--空指针异常 -->
                <entry key="java.lang.NullPointerException" value="exception_demo"></entry>
            </map>
        </property>

    </bean>

3.jsp页面

1.error.jsp

<%--
  Created by IntelliJ IDEA.
  User: jurfl
  Date: 2021/1/15
  Time: 12:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>网络开了小差,或者您没有权限!!!!!</h1>
</body>
</html>

2.exception_demo.jsp页面

<%--
  Created by IntelliJ IDEA.
  User: jurfl
  Date: 2021/1/17
  Time: 11:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
 <h1>异常处理页面!!!!</h1>
</body>
</html>

3.访问

2.2 自定义异常处理

1.创建异常处理器类实现HandlerExceptionResolver

2.配置异常处理器

3.编写异常页面

4.测试跳转

1.controller

package com.ljf.spring.mvc.demo.controller;

import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @ClassName: ExceptionHandler
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/01/17 10:55:06 
 * @Version: V1.0
 **/
@Controller
public class ExceptionHandler  {

    @RequestMapping("/exception")
    public  void  exceptionDemo() throws Exception{
        System.out.println("空指针异常:");
        List list=null;
        list.get(0);

    }
}

 

2.自定义异常处理类

package com.ljf.spring.mvc.demo.controller;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

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

/**
 * @ClassName: MyExceptionController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/01/17 11:25:23 
 * @Version: V1.0
 **/
public class MyExceptionController implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
       ModelAndView modelAndView=new ModelAndView();
       if(e instanceof  NullPointerException){
           modelAndView.addObject("info","空指针异常");
       }
       modelAndView.setViewName("exception_demo");
        return  modelAndView;
    }
}

3.配置异常处理器,将自定义类放到容器中

 <!--自定义异常处理器 -->
    <bean class="com.ljf.spring.mvc.demo.controller.MyExceptionController"></bean>

4.错误页面

<%--
  Created by IntelliJ IDEA.
  User: jurfl
  Date: 2021/1/17
  Time: 11:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
 <h1>异常处理页面!!!!</h1>
${info}
</body>
</html>

5.测试访问

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值