500的页面处理是比较简单的,用java config或者xml的形式,定义如下的bean即可
- <bean
- class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
- <property name="exceptionMappings">
- <props>
- <prop key="org.apache.shiro.authz.UnauthenticatedException">pages/403</prop>
- <prop key="org.apache.shiro.authz.UnauthorizedException">pages/403</prop>
- <prop key="org.apache.shiro.authc.LockedAccountException">pages/locked</prop>
- <prop key="java.lang.Throwable">pages/500</prop>
- </props>
- </property>
- </bean>
404就比较特殊了,有2种方法可以参考:
1.
先设置dispatcherServlet
- @Bean
- public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
- ServletRegistrationBean registration = new ServletRegistrationBean(
- dispatcherServlet);
- dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
- return registration;
- }
再增加处理错误页面的handler,加上@ControllerAdvice 注解
- @ControllerAdvice
- public class GlobalControllerExceptionHandler {
- public static final String DEFAULT_ERROR_VIEW = "pages/404";
- @ExceptionHandler(value = NoHandlerFoundException.class)
- public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
- ModelAndView mav = new ModelAndView();
- mav.addObject("exception", e);
- mav.addObject("url", req.getRequestURL());
- mav.setViewName(DEFAULT_ERROR_VIEW);
- return mav;
- }
- }
不过上面这种处理方法,会造成对js,css等资源的过滤,最好使用第二种方法
2. 集成ErrorController
- @Controller
- public class MainsiteErrorController implements ErrorController {
- private static final String ERROR_PATH = "/error";
- @RequestMapping(value=ERROR_PATH)
- public String handleError(){
- return "pages/404";
- }
- @Override
- public String getErrorPath() {
- // TODO Auto-generated method stub
- return ERROR_PATH;
- }
- }