异常处理

异常处理类

package com.hous.crud.controller;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
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.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import com.hous.crud.exception.HousException;

@Controller
public class ExceptionController {

	
//	@ExceptionHandler({RuntimeException.class})
//	public ModelAndView handlerArithmeticException2(Exception ex) {
//		System.out.println("我的异常-_-]: " + ex);
//		ModelAndView mv = new ModelAndView("error");
//		mv.addObject("exception", ex);
//		return mv;
//	}
	
	/**
	 * 1.在@ExceptionHandler方法的入参中可以加入Exception类型的参数,该参数对应发生的异常对象
	 * 2.@ExceptionHandler方法入参中不能传入Map,若希望把异常信息传导页面上,需要使用ModelAndView作为返回值
	 * 3.@ExceptionHandler标注的异常存在优先级,关系近的优先级高
	 * 4.@ControllerAdvice如果在当前Handler中找不到@ExceptionHandler方法且当前方法抛出异常,
	 * 	将去@ControllerAdvice标注的类中查找@ExceptionHandler标注的方法处理异常
	 */
	@ExceptionHandler({ArithmeticException.class})
	public ModelAndView handlerArithmeticException(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";
	}
	
	/**
	 * 此处不能有对应的@ExceptionHandler({RuntimeException.class})处理方法,否则把你处理掉了
	 */
	@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="测试Shanshanbox.com")
	@RequestMapping("/testResponseStatusExceptionResolver")
	public String testResponseStatusExceptionResolver(@RequestParam("i") int i) {
		if(i == 317) {
			throw new HousException();
		}
		System.out.println("testResponseStatusExceptionResolver...");
		return "success";
	}
	
	@RequestMapping("/testSimpleMappingExceptionResolver")
	public String testSimpleMappingExceptionResolver(@RequestParam("i") int i) {
		String[] arr = new String[10];
		arr[i] = "shanshanbox";
		return "success";
	}
}

 

package com.hous.crud.controller;

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

@ControllerAdvice
public class MyExcetionController {
	
	@ExceptionHandler({ArithmeticException.class})
	public ModelAndView handlerArithmeticException(Exception ex) {
		System.out.println("我的异常[&_&]: " + ex);
		ModelAndView mv = new ModelAndView("error");
		mv.addObject("exception", ex);
		return mv;
	}
	
}

 

package com.hous.crud.exception;

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

@ResponseStatus(value=HttpStatus.FORBIDDEN, reason="不给你访问")
public class HousException extends RuntimeException{

	private static final long serialVersionUID = 1L;

}

 

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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.2.xsd">

	<!-- 自动扫描bean -->
	<context:component-scan base-package="com.hous.crud"></context:component-scan>
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 解决静态资源路径问题 -->
	<mvc:default-servlet-handler/>
	
	<mvc:annotation-driven conversion-service="conversionService"/>
	<!-- 
		将自定义的转换器注册到springmvc上下文中
		org.springframework.context.support.ConversionServiceFactoryBean
		org.springframework.format.support.FormattingConversionServiceFactoryBean
	 -->
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<ref bean="employeeConverter"/>
			</set>
		</property>
	</bean>
	<!-- 配置国际化 -->
	<bean id="messageSource" 
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="i18n"></property>
	</bean>
	<!-- 配置SessionLocaleResolver -->
	<bean id="localeResolver" 
		class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
	<mvc:interceptors>
		<!-- 配置自定义拦截器 -->
		<bean class="com.hous.crud.interceptor.FirstInterceptor" />
		<!-- 配置LocaleChangeInterceptor -->
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
		<mvc:interceptor>
			<mvc:mapping path="/json"/>
			<bean class="com.hous.crud.interceptor.SecondInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>
	<!-- 
	<mvc:view-controller path="/i18n"/>
	 -->
	<mvc:view-controller path="/i18n2"/>
	
	<!-- 文件上传解析器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="UTF-8"></property>
		<property name="maxUploadSize" value="1024000"></property>
	</bean>
	
	<!-- 配置SimpleMappingExceptionResolver映射异常 -->
	<bean 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>
	
</beans>

 

<%@ 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>
<h3>error</h3>
${exception}
</body>
</html>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值