学习SpringMVC(二十四)之异常处理

SpringMVC通过HandlerExceptionResolver处理程序的异常,包括Handler映射,数据绑定以及目标方法执行时发生的异常。



1.在@ExceptionHandler 方法的入参中可以加入 Exception 类型的参数, 该参数即对应发生的异常对象

2.@ExceptionHandler方法的入参中不能传入 Map.若希望把异常信息传导页面上,需要使用 ModelAndView作为返回值
3.@ExceptionHandler 方法标记的异常有优先级的问题.会先处理匹配度高的异常

直接上实例:

在Controller中:

<span style="font-size:18px;"><span style="font-family:SimSun;font-size:18px;">package com.cgf.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@RequestMapping(value="/springmvc")
@Controller
public class MyException {	
	<span style="color:#ff0000;">@ExceptionHandler(value={Exception.class})</span>
	public ModelAndView myException(Exception ex){
		ModelAndView mv=new ModelAndView("error");
		mv.addObject("exception", ex);
		return mv;
	}	
	@RequestMapping(value="/testException")
	public String testException(@RequestParam(value="id")int id){
		System.out.println("id=:"+10/id);
		return "success";		
	}

}</span></span>
在sprin gmvc.xml中:需要配置<mvc:annotation-driven/>

在index.jsp中;

<span style="font-size:18px;"><span style="font-family:SimSun;">  <h1>异常处理</h1>
  <a href="springmvc/testException?id=1">Test Exception</a>
  <br></span></span>
4.@ControllerAdvice: 如果在当前 Handler 中找不到 @ExceptionHandler 方法来出来当前方法出现的异常, 

则将去@ControllerAdvice 标记的类中查找 @ExceptionHandler 标记的方法来处理异常.

<span style="font-size:18px;">package com.cgf.springmvc.handlers;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

<span style="background-color: rgb(255, 255, 255);"><span style="color:#ff0000;">@ControllerAdvice</span></span>
public class MyArithmeticException {
	
	@ExceptionHandler(value={ArithmeticException.class})
	public ModelAndView myException(Exception ex){
		System.out.println("MyArithmeticException");
		ModelAndView mv=new ModelAndView("error");
		mv.addObject("exception", ex);
		return mv;
	}

}</span>

5.ResponseStatusExceptionResolver处理用@ResponseStatus标识的异常类或异常方法

<span style="font-family:SimSun;font-size:18px;">package com.cgf.springmvc.handlers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(reason="用户名与密码不匹配",value=HttpStatus.FORBIDDEN)
public class UserNameNotMatchPasswordException extends RuntimeException{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
}</span>

6.配置SimpleMappingExceptionResolver来映射异常

<bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionAttribute" value="exception"></property>
<property name="exceptionMappings">
	<props>
	<!-- 全限定名、视图名 -->
		<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
	</props>
</property>
</bean>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值