springMVC——异常处理

1 前言

springMVC 通过 HandlerExceptionResolver 处理程序的异常,包括 handler 映射、数据绑定、目标方法执行时发生的异常,用户可以通过配置 SimpleMappingExceptionResolver 自定义指定异常时跳转到指定页面。

在控制层中可以定义不同异常跳转到不同的页面,但是如果需要将同一类异常跳转到同一个页面中,控制层中所有能发生该类异常的地方都需要定义,这样会增加代码量。在配置文件中,只需配置一下,就能实现所有同类异常跳转到同一个页面。

2 实验环境

(1)导入 JAR 包

(2)工作目录

 (3)配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
  <!-- 首页网页 -->
  <welcome-file-list>
  	<welcome-file>/WEB-INF/view/index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置核心(前端)控制器 DispatcherServlet -->
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<!-- 加载IOC容器配置文件 -->
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext.xml</param-value> 
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.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.0.xsd">	
	<!-- 扫描组件,将加@Controller注解的类作为SpringMVC的控制层 -->
	<context:component-scan base-package="com.test"></context:component-scan>
	
	<!-- 配置视图解析器 -->
 	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>	

	<!-- 自定义异常处理 -->	
	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.ArithmeticException">error</prop>
			</props>
		</property>
	</bean>
</beans>

注意:error 指将要跳入的页面,即 error.jsp

3 案例分析

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>首页</title>
</head>
<body>	
	<a href="test">测试异常</a>
</body>
</html>

Test.java

package com.test;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Test {
	
	@RequestMapping(value="test")
	public String test(){
		System.out.println(1/0);
		return "success";
	}
}

注意:这里有个被零整除算术异常 

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>成功</title>
</head>
<body>	
	SUCCESS
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>异常</title>
</head>
<body>	
	异常信息:${exception}
</body>
</html>

注意:exception 作用域中存放异常信息。

在地址栏输入:http://localhost:8080/SpringMVC/,显示如下:

 点击【测试异常】,跳入 error.jsp 页面,如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章