处理异常的方式有3种:
①在spring-security.xml配置文件中来处理:
|
<security:http auto-config-”true” use-expressions=”true”> <security:access-denied-handler error-page=”/403.jsp”/> 只能处理403异常 </security:http> |
②在web.xml中,针对不同的httpcode,去指定对应的jsp页面;
③实现Spring提供的HandlerExceptionResolver接口,并在实现类上添加@Controller/@Component注解,来让Spring加载到IOC容器中;
或者
自定义一个类,然后在类上添加@ControllerAdvice注解,该注解默认就是表示该自定义类实现了HandlerExceptionResolver接口且添加到IOC容器中,然后在方法上添加@ExceptionHandler(异常类.class)注解即可,当然,为了让方法跳到对应的页面,方法返回值是String/View/ModelAndView;
本文详细介绍了Spring框架中处理异常的三种方式:通过spring-security.xml配置文件、web.xml配置HTTP状态码对应页面以及实现HandlerExceptionResolver接口或使用@ControllerAdvice进行异常处理。每种方式的特点和适用场景均有阐述。
1万+

被折叠的 条评论
为什么被折叠?



