37、(知识篇)SpringMVC14 SpringMVC异常处理

/**
* SpringMVC异常处理 ExceptionHandler/ ResponseStatusExceptionResolver 
* / DefaultHandlerExceptionResolver / SimpleMappingExceptionResolver:


* 1、@ExceptionHandler的使用(参考MyExceptionHandler)
* 访问测试地址:http://127.0.0.1:8080/37Spring14/myMapping?row=0
* 在目标方法中使用 @ExceptionHandler 来修饰,针对异常则会跳到相应返回的页面
* 不用返回String型,不能用Map去传递错误信息
*   可以用ModelAndView代替
*  可以在异常类中用@ControllerAdvice 修饰,SpringMVC会去找该类的@ExceptionHandler方法

* 2、@ResponseStatus(参考MyResponseStatusException)

* 在自定义的异常类中使用该注解,如果抛异常,则会按照ResponseStatus中的设定值
* (code=HttpStatus.NOT_FOUND,reason="用户名不能为空") 返回对应的错误码和原因
* 测试地址:http://127.0.0.1:8080/37Spring14/myMapping2

* 3、@DefaultHandlerExceptionResolver
* 该注解是SpringMVC做处理的异常处理类,
* 例如制定POST方法,但是用get方法去请求时,SpringMVC会报
*  Request method ‘get’ is not suppoded
*  
* 4、在xml中配置 SimpleMappingExceptionResolver

* <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionAttribute" value="ex"></property>
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
</props>
</property>
</bean>
* 配置exceptionAttribute,使用jstl中 ${ex} 的属性名字
*  配置exceptionMappings 配置遇到错误时返回的路径

* @param row
* @return

*/

测试类:

package com.spring.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TestController {

	/**
	 * SpringMVC异常处理 ExceptionHandler/ ResponseStatusExceptionResolver 
	 * / DefaultHandlerExceptionResolver / SimpleMappingExceptionResolver:
	 * 
	 * 
	 * 1、@ExceptionHandler的使用(参考MyExceptionHandler)
	 * 	访问测试地址:http://127.0.0.1:8080/37Spring14/myMapping?row=0
	 * 	 在目标方法中使用 @ExceptionHandler 来修饰,针对异常则会跳到相应返回的页面
	 * 	 不用返回String型,不能用Map去传递错误信息
	 *   可以用ModelAndView代替
	 * 	  可以在异常类中用@ControllerAdvice 修饰,SpringMVC会去找该类的@ExceptionHandler方法
	 * 
	 * 2、@ResponseStatus(参考MyResponseStatusException)
	 * 
	 * 在自定义的异常类中使用该注解,如果抛异常,则会按照ResponseStatus中的设定值
	 * (code=HttpStatus.NOT_FOUND,reason="用户名不能为空") 返回对应的错误码和原因
	 * 	测试地址:http://127.0.0.1:8080/37Spring14/myMapping2
	 * 
	 * 3、@DefaultHandlerExceptionResolver
	 * 	该注解是SpringMVC做处理的异常处理类,
	 * 	例如制定POST方法,但是用get方法去请求时,SpringMVC会报
	 *  Request method ‘get’ is not suppoded
	 *  
	 * 4、在xml中配置 SimpleMappingExceptionResolver
	 * 
	 * 	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
			<property name="exceptionAttribute" value="ex"></property>
			<property name="exceptionMappings">
				<props>
					<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
				</props>
			</property>
		</bean>
	 * 	配置exceptionAttribute,使用jstl中 ${ex} 的属性名字
	 *  配置exceptionMappings 配置遇到错误时返回的路径
	 * 
	 * @param row
	 * @return
	 */
	
	
	@RequestMapping("myMapping")
	public String myMapping(@RequestParam("row") int row){
		int result = 10/row;
		System.out.println("result == "+result);
		return "error";
	}
	
	

	@RequestMapping("myMapping2")
	public String myMapping2(){
		String a = null;
		if(a==null){
			throw new MyResponseStatusException();
		}
		return "error";
	}
	
	
	@RequestMapping("myMapping3")
	public String myMapping3(){
		String[] args = new String[1];
		String result = args[2];
		System.out.println("result == "+result);
		return "redirect:http://www.baidu.com";
	}
	
}

MyExceptionHandler类:

package com.spring.test;

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

@ControllerAdvice
public class MyExceptionHandler {

	@ExceptionHandler({ArithmeticException.class})
	public ModelAndView myArithmeticException(Exception ex){
		System.out.println("myArithmeticException...");
		ModelAndView mv = new ModelAndView("error");
		mv.addObject("ex", ex);
		return mv;
	}
	
	
	/*@ExceptionHandler({RuntimeException.class})
	public ModelAndView myRuntimeException(Exception ex){
		System.out.println("myRuntimeException...");
		ModelAndView mv = new ModelAndView("error");
		mv.addObject("ex", ex);
		return mv;
	}*/
	
}

MyResponseStatusException类:

package com.spring.test;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(code=HttpStatus.NOT_FOUND,reason="用户名不能为空")
public class MyResponseStatusException extends NullPointerException{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3152759002903603198L;

}

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: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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<context:component-scan base-package="com.spring.test" >
	</context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	
	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionAttribute" value="ex"></property>
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
			</props>
		</property>
	</bean>
	
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
	
	
</beans>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	${ex }
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值