java - Tomcat 没有在 Web 应用程序的上下文中添加斜杠

标签 java spring tomcat war

如果输入的 url 没有它,我想让 Tomcat 自动在我的应用程序的上下文中添加一个尾部斜杠。

当我使用 Jetty 进行测试时,它会自动将斜杠添加到我的应用程序的上下文中,但 Tomcat 不会这样做。

我不确定一旦部署上下文将被命名,因为我将 WAR 交给其他人,所以 HTML 中的任何资源引用都是相对的。有没有办法让 Tomcat 自动重定向到相同的上下文并添加斜杠?

目前使用 Tomcat 7 和 Spring 3。

最佳答案

这是一篇旧文章,但从 Tomcat 7.0.67 开始,您需要将以下属性添加到您的 context.xml 文件中:

<Context mapperContextRootRedirectEnabled="true">...</Context>

根据 7.0.67 changelog :

Move the functionality that provides redirects for context roots and directories where a trailing / is added from the Mapper to the DefaultServlet. This enables such requests to be processed by any configured Valves and Filters before the redirect is made. This behaviour is configurable via the mapperContextRootRedirectEnabled and mapperDirectoryRedirectEnabled attributes of the Context which may be used to restore the previous behaviour.

在 Tomcat 上下文中 documentation :

mapperContextRootRedirectEnabled: If enabled, requests for a web application context root will be redirected (adding a trailing slash) if necessary by the Mapper rather than the default Servlet. This is more efficient but has the side effect of confirming that the context path exists. If not specified, the default value of false is used.

关于java - Tomcat 没有在 Web 应用程序的上下文中添加斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11055608/

相关文章:

java - MockMVC 和 Mockito 返回状态预期 <200> 但为 <415>

java - 使用@Transactional 的类层次结构问题

linux - Tomcat 正在启动但无法访问本地主机 :8080

tomcat - SQLException: 找不到合适的驱动程序

android - 如何使用tomcat服务器验证android应用程序?

java - HAXM 已安装但模拟器未运行

java - 循环不会添加总计

spring - 在 Spring Security + Angular 中实现刷新 token

java - 没有这样的文件或目录 Eclipse Java Mac OSX

java - 消息太大时 AppEngine 任务队列异常