13.4、异常处理——SimpleMappingExceptionResolver
来源:互联网 发布:如何举报淘宝违规销售 编辑:程序博客网 时间:2024/06/15 09:02
如果希望对所有的异常进行统一的处理,比如当指定的异常发生时,把它映射到要显示的错误的网页中,此时用SimpleMappingExceptionResolver进行解析。DispatcherServlet中没有实现SimpleMappingExceptionResolver的Bean,所有需要在spring的配置文件中进行配置。示例如下:
1、web.xml和spring的配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
spring的配置文件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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.lzj.springmvc"></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:annotation-driven></mvc:annotation-driven></beans>
2、创建请求
在WebContent下面创建index.jsp,内容为:
<a href="springMVC/testSimpleMappingExceptionResolver?i=11">Test SimpleMappingExceptionResolver</a>
3、创建请求控制器
控制器用于截获index.jsp中的请求
@Controller@RequestMapping("/springMVC")public class TestSpringMVC { @RequestMapping("/testSimpleMappingExceptionResolver") public String testSimpleMappingExceptionResolver(){ String[] values = new String[10]; //下标越界了 System.out.println(values[11]); return "success"; }}
发送index.jsp中的超链接请求,控制器捕获请求后处理控制器逻辑,由于在逻辑中,数组越界,会抛出ArrayIndexOutOfBoundsException异常。
4、处理异常
spring中的配置文件应改为如下<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.lzj.springmvc"></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:annotation-driven></mvc:annotation-driven> <!-- 配置使用SimpleMappingExceptionResolver来映射异常 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!-- 给异常命名一个别名 --> <property name="exceptionAttribute" value="ex"></property> <property name="exceptionMappings"> <props> <!-- 一定要异常的全类名。 表示出现ArrayIndexOutOfBoundsException异常,就跳转到error.jsp视图--> <prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop> </props> </property> </bean></beans>
另外在/WEB-INF/views下新建一个error.jsp视图。因为上面配置的InternalResourceViewResolver视图解析器默认把error字符串解析为error.jsp视图。error.jsp内容为:
<h1>Error Page</h1> ${requestScope.ex }
下面重新发送index.jsp中的超链接请求后,控制器截获请求并处理请求时,数组越界抛出一个ArrayIndexOutOfBoundsException一个异常,此时由SimpleMappingExceptionResolver异常解析器捕获并把异常映射到error.jsp视图中,通过${requestScope.ex }获取异常。在浏览器中显示为:
阅读全文
0 0
- 13.4、异常处理——SimpleMappingExceptionResolver
- Spring:SimpleMappingExceptionResolver异常处理
- Spring:SimpleMappingExceptionResolver异常处理
- Spring:SimpleMappingExceptionResolver异常处理
- SpringMvc声明式异常处理之SimpleMappingExceptionResolver
- 自定义SimpleMappingExceptionResolver实现个性化的异常处理
- Spring MVC异常处理SimpleMappingExceptionResolver【转】
- springmvc学习笔记(30)——SimpleMappingExceptionResolver异常映射
- Spring MVC 全局异常处理(1) --SimpleMappingExceptionResolver
- Spring MVC 全局异常处理(1) --SimpleMappingExceptionResolver
- springMVC—异常处理
- Javase—异常处理
- JAVA——异常及异常处理
- php异常处理—多个异常
- php异常处理—重新抛出异常
- 详解Java异常处理——编程式异常处理
- 详解Java异常处理——自动异常处理
- java异常处理三——怎么处理异常
- nginx的spinlock的学习
- springmvc配置文件:web.xml
- PAT (Basic Level) Practise (中文)1072. 开学寄语(20)
- 第4章 树莓派常用软件及服务
- PAT Basic 1009 with Java.
- 13.4、异常处理——SimpleMappingExceptionResolver
- lru ---百度百科节选
- 洛谷p1307
- 常用dos命令
- NASA 顶级程序员是如何编程的?这里有十大准则
- openstack【Kilo】入门 【glance篇】十:glance安装配置【控制节点】
- [HNOI2012 永无乡]线段树+并查集
- JDK8中的Object源码分析
- 文章标题