首页 > 代码库 > Spring --- 异常处理机制

Spring --- 异常处理机制

1.定义全局异常处理器,为全局的异常,如出现将调用 error.JSP

<!-- 定义异常处理器 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
	<property name="exceptionMappings">
		<props>
			<!-- 如果Controller抛出Exception异常调用error.jsp -->
			<prop key="java.lang.Exception">error</prop>
		</props>
	</property>
</bean>

 

	@RequestMapping("hello.do")
	public String excute(@ModelAttribute("username") String username,String pwd,Model model){
		model.addAttribute("name","TOM");
		//有可能抛出异常
		String s = null;
		s.length();
		return "hello";
	}

 2.定义局部异常处理,Controller里面的请求捕获

 

	//局部异常处理,当前Controller发生错误调用dateerror.jsp
	@ExceptionHandler
	public String handleException(
		HttpServletRequest request,
		Exception ex){
		request.setAttribute("error", ex);
		return "dateerror";
	}

  

 

Spring --- 异常处理机制