SimpleMappingExceptionResolver
简单,清晰,够用,异常类型与视图的映射,自定义的任何异常类型都可以在这里和错误页面进行映射,颗粒度够细
springmvc.xml
Xml代码
- <bean id="webExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
- <property name="defaultErrorView" value="redirect:/error.jsp?flag=defaultErrorView" />
- <property name="exceptionMappings">
- <props>
- <prop key="com.a.a.a.exception.BaseServiceException">
- redirect:/error.jsp?flag=BaseServiceException
- < span>prop>
- <prop key="java.lang.RuntimeException">
- redirect:/error.jsp?flag=RuntimeException
- < span>prop>
- < span>props>
- < span>property>
- < span>bean>
Spring 3新增的注解是异常处理,在Control类中加入
Java代码
- @RequestMapping("exception")
- public void throwException() {
- throw new RuntimeException("This is the runtime exception");
- }
- @ExceptionHandler(Exception.class)
- public @ResponseBody String handleException(Exception ex) {
- return ex.getMessage();
- }
也可以将@ExceptionHandle抽象到BaseControl里,不过若用了SimpleMappingExceptionResolver,则@ExceptionHandle会不起作用
HandlerExceptionResolver
自定义异常实现
Java代码
- public class WebExceptionResolver implements HandlerExceptionResolver {
- public ModelAndView resolveException(HttpServletRequest request,
- HttpServletResponse response, Object object, Exception e) {
- HttpSession session = request.getSession();
- session.getId();
- //处理异常
- return null;
- }
- }
springmvc.xml
Xml代码
- <bean id="webExceptionResolver" class="com.a.a.WebExceptionResolver"/>
【编辑推荐】
- Spring Hibernate简单讨论
- OSGi与Spring:设置Spring DM开发环境
- 使用Spring DM创建Hello World,以及OSGi服务
- Spring MVC总结:善用注解,生活更轻松
- 概括spring hibernate集成