Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息

</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">
&lt;!-- DispatcherServlet Context: defines this servlet&#39;s request-processing infrastructure --&gt;

&lt;!-- Scans within the base package of the application for @Components to configure as beans --&gt;
&lt;!-- @Controller, @Service, @Configuration, etc. --&gt;
&lt;context:component-scan base-package=&quot;edu.bit.myhello&quot; /&gt;

&lt;!-- Enables the Spring MVC @Controller programming model --&gt;
&lt;mvc:annotation-driven /&gt;

&lt;!-- Resolve logical view names to .jsp resources in the /WEB-INF/views directory --&gt;
&lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
	&lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/views/&quot; /&gt;
	&lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&gt;
&lt;/bean&gt;

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

</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>

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






Published At
comments powered by Disqus