SpringMVC学习笔记——文件上传

1. 使用 @ExceptionHandler 注解

使用前需在springmvc.xml中添加<mvc:annotation-driven></mvc:annotation-driven>

	/*
	 * 1. 在 @ExceptionHandler方法的入参中可以加入 Exception 类型的参数,
	 * 		该参数即对应发生的异常对象
	 * 2. @ExceptionHandler 方法的入参中不能传入Map,
	 * 		若希望把异常信息传到页面上,需要使用ModelAndView 作为返回值
	 * 3. @ExceptionHandler 方法标记有优先级的问题,匹配度高的优先
	 * 4. @ControllerAdvice: 如果在当前 Handler 中找不到 @ExceptionHandler
	 * 		方法来处理当前方法出现的异常,
	 * 		则将去 @ControllerAdvice 标记的类中查找 @ExceptionHandler
	 * 		标记的方法来处理异常
	 */	
    @ExceptionHandler({ArithmeticException.class})
	public ModelAndView handleArithmeticException(Exception ex) {
		System.out.println("出异常了:" + ex);
		ModelAndView mv = new ModelAndView("error");
		mv.addObject("exception", ex);
		return mv;
	}
	
	@RequestMapping("/testExceptionHandlerExceptionResolver")
	public String testExceptionHandlerExceptionResolver(
			@RequestParam("i") int i) {
		System.out.println("result: " + (10 / i));
		return "success";
	}

[email protected]注解

@ControllerAdvice
public class SpringMVCTestExceptionHandler {
	@ExceptionHandler({ArithmeticException.class})
	public ModelAndView handleArithmeticException(Exception ex) {
		System.out.println("---->出异常了:" + ex);
		ModelAndView mv = new ModelAndView("error");
		mv.addObject("exception", ex);
		return mv;
	}
}

 [email protected]注解

1. 定义一个带有@ResponseStatus注解修饰的异常类

@ResponseStatus(value=HttpStatus.FORBIDDEN, reason="用户名和密码不匹配")
public class UserNameNotMathPasswordException extends RuntimeException{

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

}

2.在处理器方法抛出该异常

@RequestMapping("/testResponseStatusExceptionResolver")
	public String testResponseStatusExceptionResolver(
			@RequestParam("i") int i) {
		if(i == 13) {
			throw new UserNameNotMathPasswordException();
		}
		System.out.println("testResponseStatusExceptionResolver...");
		
		return "success";
		
	}

3. 结果

SpringMVC学习笔记——文件上传

4.也可直接在处理方法上添加 

@ResponseStatus(reason="测试",value=HttpStatus.NOT_FOUND)
	@RequestMapping("/testResponseStatusExceptionResolver")
	public String testResponseStatusExceptionResolver(
			@RequestParam("i") int i) {
		if(i == 13) {
			throw new UserNameNotMathPasswordException();
		}
		System.out.println("testResponseStatusExceptionResolver...");
		
		return "success";
		
	}

4.使用SimpleMappingExceptionResolver 

SpringMVC学习笔记——文件上传

1.首先配置springmvc.xml

若出现指定异常,则跳转到error.jsp页面

<!-- 配置使用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>

2.处理方法

@RequestMapping("/testSimpleMappingExceptionResolver")
	public String testSimpleMappingExceptionResolver(@RequestParam("i") int i) {
		String[] vals = new String[10];
		System.out.println(vals[i]);
		return "success";
	}

3. error.jsp --> 可打印异常

SpringMVC学习笔记——文件上传