SpringMVC___配置异常处理

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

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷学习被我发现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值