<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>
1.
先设置dispatcherServlet
@Bean public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) { ServletRegistrationBean registration = new ServletRegistrationBean( dispatcherServlet); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); return registration; }
@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; } }
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; } }
原文地址:http://blog.csdn.net/projectarchitect/article/details/42463471