标签:
<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
标签:
原文地址:http://www.cnblogs.com/bhlsheji/p/4889997.html