标签:404 自定义404 springmvc dispatchservlet
<error-page> <error-code>404</error-code> <location>/error/404.htm</location> </error-page>
@Controller public class UrlNotFoundController { @RequestMapping("*") public String test404(){ //TODO return "404Page"; } }
@Controller("/home") public class HomeController{ @RequestMapping("a") public String a(){ // } }
<servlet> <servlet-name>theDispatcher</servlet-name> <servlet-class>base.web.MyDispatchServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc-servlet.xml</param-value> </init-param> <init-param> <param-name>fileNotFondUrl</param-name> <param-value>/error/404</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>theDispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
public class MyDispatchServlet extends DispatcherServlet { private static final long serialVersionUID = 1L; private static final UrlPathHelper urlPathHelper = new UrlPathHelper(); private String fileNotFondUrl = "/error/404.html"; public void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception { if (pageNotFoundLogger.isWarnEnabled()) { String requestUri = urlPathHelper.getRequestUri(request); pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + requestUri + "] in DispatcherServlet with name ‘" + getServletName() + "‘"); } response.sendRedirect(request.getContextPath() + fileNotFondUrl); } public String getFileNotFondUrl() { return fileNotFondUrl; } public void setFileNotFondUrl(String fileNotFondUrl) { this.fileNotFondUrl = fileNotFondUrl; } }
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/static/*</url-pattern> </servlet-mapping>
三种思路实现自定义404页面:Tomcat、SpringMVC精准匹配、重写DispatchServlet
标签:404 自定义404 springmvc dispatchservlet
原文地址:http://blog.csdn.net/fansunion/article/details/44349729