Tomcat没有向Web应用程序的上下文添加尾部斜杠

问题描述:

如果在没有它的情况下输入网址,我想让Tomcat自动为我的应用程序的上下文添加一个斜杠。

I'd like to have Tomcat automatically add a trailing slash to my app's context if the url is entered without it.

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

When I test with Jetty, it automatically adds the trailing slash to my app's context, but Tomcat doesn't do this.

我不确定部署后将命名的上下文,因为我正在处理WAR关闭给其他人,因此HTML中的任何资源引用都是相对的。有没有办法让Tomcat自动重定向到添加了尾部斜杠的相同上下文?

I'm uncertain what the context will be named once deployed, as I'm handing the WAR off to someone else, so any resource references in HTML is all relative. Is there any way to have Tomcat automatically redirect to the same context with a trailing slash added?

目前在Spring 3中使用Tomcat 7.

Currently Using Tomcat 7 with Spring 3.

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

It's an old post, but as of Tomcat 7.0.67, you need to add the following attribute to your context.xml file:

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

根据7.0.67 changelog


移动提供重定向的功能用于从Mapper向DefaultServlet添加尾随/的上下文根和目录。这样,在重定向之前,任何已配置的阀门和过滤器都可以处理此类请求。此行为可通过Context的mapperContextRootRedirectEnabled和mapperDirectoryRedirectEnabled属性进行配置,该属性可用于恢复先前的行为。

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中上下文文档

And in the Tomcat context documentation:


mapperContextRootRedirectEnabled:如果启用,将在必要时由Mapper而不是默认Servlet重定向(添加尾部斜杠)对Web应用程序上下文根的请求。这更有效,但具有确认上下文路径存在的副作用。如果未指定,则使用默认值false。

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.