springmvc----异常处理----笔记

目录结构

在这里插入图片描述

控制器

package top.demo.test.Control;

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

import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException;




@Controller
public class MainControl {

	
	/*
	 * 异常处理注解@ExceptionHandler 参数为异常Class数组 指处理哪些异常类型
	 * 带@ExceptionHandler的方法 处理的先后关系  如一个方法处理AlgorithmAlreadyRegisteredException 一个处理Exception
	 * 当除零时 是被处理AlgorithmA..的异常处理方法处理了。
	 * 框架自带有DefaultHandlerExceptionResolver、ResponseStatusExceptionResolver、ExceptionHandler三个异常解析器
	 * 
	 * 异常处理 当控制器没有@ExceptionHandler的方法,或者没有对应的异常类型
	 * 会寻找带有@ControllerAdvice的类,查找类里的方法有没有对应的处理方法
	 * 
	 * 类SimpleMappingExceptionResolver
	 * 在springmvc配置文件里添加Bean:SimpleMappingExceptionResolver
	 * 设置相应的属性即可捕获对应的异常转向相应的页面 。也就是可以简单的配置出哪个异常转到哪个页面
	 * 
	 * */
	
	
	@ExceptionHandler(value= {AlgorithmAlreadyRegisteredException.class})
	public ModelAndView handlerException(Exception ex)
	{
		ModelAndView modelAndView=new ModelAndView();
		System.out.println("handlerException::handler Exception");
		modelAndView.setViewName("error");
		modelAndView.addObject("message", "出了异常:"+ex.getMessage());
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("/index/{id}")
	public String Index(@PathVariable(name="id",required=false) Integer id) throws Exception {
		
		System.out.println("index");
		if(id!=null)
		{
			if(id==0)
			{
				throw new AlgorithmAlreadyRegisteredException("测试AlgorithmAlreadyRegisteredException");
			}
			else if(id==1)
			{
				throw new RuntimeException("测试RuntimeException");
			}
			else if(id==2)
			{
				throw new Exception("测试Exception");
			}
			else if(id==3)
			{
				throw new ArrayIndexOutOfBoundsException("测试ArrayIndexOutOfBoundsException");
			}
		}

		return "hi";
	}
	
	
}

添加@ControllerAdvice的异常处理类

package top.demo.test.ExceptionHand;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;


@ControllerAdvice
public class MyHandler {
	
	@ExceptionHandler(value= {Exception.class})
	public ModelAndView handlerException(Exception ex)
	{
		ModelAndView modelAndView=new ModelAndView();
		System.out.println("ModelAndView ::handler Exception");
		modelAndView.setViewName("error");
		modelAndView.addObject("message", "出了异常:"+ex.getMessage());
		return modelAndView;
	}

}

springmvc配置文件

<?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-2.0.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="top.demo.test"></context:component-scan>

<mvc:annotation-driven></mvc:annotation-driven>

<mvc:interceptors>
	<bean class="top.demo.test.Interceptor.MyInterceptor"></bean>
	
<!-- 
	多个拦截器的执行顺序,preHandle按配置的先后执行,其他两个则按配置先后的反序执行

	还有这种配置方式,指定包含哪些页面路径 (<mvc:mapping path=""/>)或者 排除哪些(<mvc:exclude-mapping path=""/> )
	<mvc:interceptor>
		<mvc:exclude-mapping path=""/> 
		<bean></bean>
		
	</mvc:interceptor>
 -->

	
</mvc:interceptors>

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	
	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<!-- 设置转向页面前,把异常放入请求域时 的key是什么 -->
		<property name="exceptionAttribute" value="message"></property>
		<!-- 设置异常和异常跳转页面的映射 private Properties exceptionMappings; -->
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
			</props>
		</property>
	</bean>

</beans>

输出
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值