在Spring MVC应用程序中,404 error code 被合适的配置。web.xml文件中配置如下所示:
<!-- spring mvc start --> <servlet> <servlet-name>SpringMVC_FrontControl</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/xmlConfig/webConfig/*.xml,</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC_FrontControl</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- spring mvc ends --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <error-page> <error-code>404</error-code> <location>/WEB-INF/webPage/test/404.jsp</location> </error-page>
当用户访问任意不存在或者报错时的资源时,它就会转向404.jsp页面。