首页 > 编程语言 > 详细

SpringMVC___配置异常处理

时间:2019-07-08 13:04:49      阅读:109      评论:0      收藏:0      [点我收藏+]

编写错误页面,异常页面,当项目发布出现了异常和错误,你肯定不想给客户看到那些错误码,严重影响体验。

Jar包:
技术分享图片

控制层MyController:

package com.zsl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {
    
    @RequestMapping("/userA")
    public ModelAndView userA(Integer num) {
        // TODO Auto-generated method stub
        System.out.println("进入了userA");
        if (num == 1) {
            throw new NullPointerException();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user");
        return modelAndView;
    }
    
    
    @RequestMapping("/userB")
    public ModelAndView userB(Integer num) {
        // TODO Auto-generated method stub
        System.out.println("进入了userB");
        if (num == 2) {
            throw new ArithmeticException();
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user");
        return modelAndView;
    }
}

异常处理类:

package com.zsl.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice
@Component
public class MyException {
    
    @ExceptionHandler(value=NullPointerException.class)
    public ModelAndView exceptionA() {
        // TODO Auto-generated method stub
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "空指针异常");
        modelAndView.setViewName("error");
        return modelAndView;
    }
    
    @ExceptionHandler(value=ArithmeticException.class)
    public ModelAndView exceptionB() {
        // TODO Auto-generated method stub
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "算术异常");
        modelAndView.setViewName("error");
        return modelAndView;
    }
}

异常处理类还可以这么配置,不加注解但是需要实现HandlerExceptionResolver接口:

package com.zsl.exception;

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

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
@Component
public class MyException implements HandlerExceptionResolver{

    @Override
    public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
            Exception arg3) {
        // TODO Auto-generated method stub
        ModelAndView modelAndView = new ModelAndView();
        if (arg3 instanceof NullPointerException) {
            modelAndView.addObject("msg", "空指针异常");
        } else if(arg3 instanceof ArithmeticException){
            modelAndView.addObject("msg", "算术异常");
        }else {
            modelAndView.addObject("msg", "其他异常");
        }
        modelAndView.setViewName("error");
        return modelAndView;
    }
    
}

Pojo:

package com.zsl.pojo;

public class User {
    private String name;
    private String sex;
    private Integer Age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return Age;
    }
    public void setAge(Integer age) {
        Age = age;
    }
    public User(String name, String sex, Integer age) {
        super();
        this.name = name;
        this.sex = sex;
        Age = age;
    }
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", sex=" + sex + ", Age=" + Age + "]";
    }
    
}

Springmvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
        
        <!-- 扫描注解 -->
        <context:component-scan base-package="com.zsl.*"/>
        
        <!-- 开启MVC注解 -->
        <mvc:annotation-driven/>
        
        <!-- 视图解析 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
</beans>

SpringMVC___配置异常处理

原文:https://www.cnblogs.com/zhangsonglin/p/11150211.html

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