</pre>Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p><h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px; text-indent:28px">第三部分:显示全局异常信息,而不是局部异常信息。</h3><p></p><p>        当有很多异常时,光是UserController这个控制器中的异常通过局部异常信息处理,已经不能满足需求。在其他控制器中(包括UserController)中的异常,都可以使用全局异常处理异常信息。</p><p>        1. 在myhello-Servelet.xml文件中,添加SimpleMappingExceptionResolver</p><p>        2. 注入<property>标签,通过<prop>标签列出全局中所有需要处理的异常。</p><p>        3. 然后注释掉局部异常处理code,在UserController控制器的代码中。</p><p>        4. 在error.jsp视图中,使用 ${exception.message } 显示全局异常处理结果。      </p><p></p><p>在myhello-servelet.xml文件中:</p><pre code_snippet_id="526777" snippet_file_name="blog_20141120_1_7394166" name="code" class="html"><?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Scans within the base package of the application for @Components to configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="edu.bit.myhello" /> <!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven /> <!-- Resolve logical view names to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean> <bean id = "exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key = "edu.bit.model.UserException">error</prop> <!--key值的是你要映射哪个类 ,value=error直接写 -->
<!-- 这个的意思就是当在UserException中发现异常,就在error中处理,error中就要用exception对象
同样还可以:
<prop key = "java.lang.NullPointer"> exception </prop> 处理空指针异常,在exception.jsp视图中处理
-->
</props>
</property>
</bean> </beans>

加入的部分:

  	<bean id = "exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key = "edu.bit.model.UserException">error</prop> <!--key值的是你要映射哪个类 ,value=error直接写 -->
<!-- 这个的意思就是当在UserException中发现异常,就在error中处理,error中就要用exception对象
同样还可以:
<prop key = "java.lang.NullPointer"> exception </prop> 处理空指针异常,在exception.jsp视图中处理
-->
</props>
</property>
</bean>

在UserController.java中,注释掉局部异常处理代码:

/*
* 显示局部的异常信息:仅仅只能处理这个控制器中的异常
* 写一个方法,HandlerException, 把 UserException传进来
*/
// @ExceptionHandler(value={UserException.class}) //用ExceptionHandler来映射,要处理的value是一个数组
// //要处理一个对象就这样写,可处理多个对象。
// public String handlerException(UserException ue, HttpServletRequest req){
// //1.把 UserException传进来;
// //2.不能用model来传值,因为不是RequestMapping,用HttpSeverletRequest req
// //把异常对象存进去:
// req.setAttribute("e", ue); //req 的 “e”参数,被set为 ue
// return "error";
// }

在error.jsp中,改为:

<h1>
<!-- ${e.message} -->
${exception.message }
</h1>

这样就完成了全局异常处理。

最新文章

  1. 2016总结Android面试题
  2. php打印中文乱码
  3. WebService 不依赖配置文件直接在构造函数配置地址
  4. [转]非OpenVZ下利用谷歌TCP-BBR协议单边加速你的VPS
  5. 怎样从命令行进入mac桌面
  6. iOS---》点击uitableview 的section展开或隐藏
  7. 在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)
  8. [ES6] 17. Set
  9. POJ 1275 Cashier Employment(差分约束)
  10. nyoj 37回文串
  11. poj1935(树形dp)
  12. 实例学习SSIS(二)--使用迭代
  13. vue.js移动端app实战1:初始配置
  14. centOS 6 服务管理与服务脚本
  15. 使用Spring框架实现用户登录实例
  16. 剑指offer(66)机器人的运动范围
  17. cglib之Enhancer
  18. 代码实战之AdaBoost
  19. Jenkins-Pipeline 流水线发布部署项目
  20. 18 Issues in Current Deep Reinforcement Learning from ZhiHu

热门文章

  1. sqlalchemy操作----多表关联
  2. elixir环境配置
  3. 数据仓库专题(2)-Kimball维度建模四步骤
  4. webpack学习总结(一)
  5. 《Java并发编程实战》笔记-取消与关闭
  6. Linux环境下配置maven环境
  7. 网站优化URL需要注意的几个细节
  8. 基于folly的AtomicIntrusiveLinkedList无锁队列进行简单封装的多生产多消费模型
  9. C语言强化——指针
  10. vue.js 的环境搭建