2014-01-08 100 views
0

我使用spring MVC框架来构建我的网站并在tomcat上运行它。如何处理没有url映射的情况?

使用http://127.0.0.1/subject/math时,它将映射到我在我的控制器中定义的math.jsp

但是,如果用户使用我的控制器中无法处理的url,例如http://127.0.0.1/subject/math123http://127.0.0.1/otherPage,我希望该网页将直接转到自定义页面,如http://127.0.0.1/default

如果我不处理的情况,就会产生一个错误页HTTP Status 404和描述是The requested resource is not available.

非常感谢!

回答

1

你需要的是确定应用程序的WEB-INF/web.xml内部错误页面

<error-page> 
    <!-- These are standard HTTP error codes --> 
    <error-code>404</error-code> 
    <location>/MyCustomErrorPage.jsp</location> 
</error-page> 

<!-- You can also map error pages against any exception that 
    may occur in the application --> 
<error-page> 
    <!-- Fully qualified name of the exception --> 
    <exception-type>java.lang.Exception</exception-type> 
    <location>/MyCustomErrorPage.jsp</location> 
</error-page> 

编辑

要从的errorPage.jsp转发到其他资源的说(homePage.jsp)无更新浏览器中的URL,您可以使用RequestDispatcher.forward(request,response)。像在你的errorPage.jsp中一样,添加

<% 
RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/path/To/homePage.jsp"); 
dispatcher.forward(request, response); 
%> 
+0

非常感谢!有用!但我仍然有一些问题。因为我想让访问错误链接的用户直接进入主页。你的解决方案可以让用户看到主页,但url链接仍然是错误的。我该如何解决它。 – LoveTW

+0

例如,我的主页是'http:// 127.0.0.1/default'。当用户访问“http:// 127.0.0.1/otherPage”时,它将看到“http:// 127.0.0.1/default”中的内容,但该url仍然是“http://127.0.0.1/otherPage” ' – LoveTW

+2

它可以完成,虽然我会建议反对它。错误页面就是错误页面!它应该只包含告诉用户您已登陆某个错误页面的信息,并且您需要返回相应的页面继续执行该应用程序。不过,如果你想要的话,我已经更新了答案 –

相关问题