Spring 3.0.5 MVC 异常处理

[b]SimpleMappingExceptionResolver[/b]
简单,清晰,够用,异常类型与视图的映射,自定义的任何异常类型都可以在这里和错误页面进行映射,颗粒度够细
[b]springmvc.xml[/b]
<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
</prop>
<prop key="java.lang.RuntimeException">
redirect:/error.jsp?flag=RuntimeException
</prop>
</props>
</property>
</bean>


[b]AnnotationMethodHandlerExceptionResolver[/b]
Spring 3新增的注解是异常处理,在Control类中加入

@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会不起作用

[b]HandlerExceptionResolver[/b]
自定义异常实现

public class WebExceptionResolver implements HandlerExceptionResolver {

public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object object, Exception e) {
HttpSession session = request.getSession();
session.getId();
//处理异常

return null;
}

}

[b]springmvc.xml[/b]

<bean id="webExceptionResolver" class="com.a.a.WebExceptionResolver"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值