Under Tomcat 9 src/main/webapp
folder, I created a sub-folder named app
containing an index.jsp file. If I request localhost:8080/app
I get a 404 error, while with a trailing slash localhost:8080/app/
it works fine.
If I disable spring mvc the problem disappears. Tomcat logs with Spring MVC enabled:
With trailing slash
21:17:33.351 [http-nio-8080-exec-7] DEBUG o.s.web.servlet.DispatcherServlet.traceDebug(91) - GET "/app/", parameters={}
21:17:33.351 [http-nio-8080-exec-7] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping.getHandler(414) - Mapped to ResourceHttpRequestHandler ["/"]
21:17:33.352 [http-nio-8080-exec-7] DEBUG o.s.web.servlet.DispatcherServlet.logResult(1131) - Completed 304 NOT_MODIFIED
Without traling slash:
21:17:57.046 [http-nio-8080-exec-9] DEBUG o.s.web.servlet.DispatcherServlet.traceDebug(91) - GET "/app", parameters={}
21:17:57.047 [http-nio-8080-exec-9] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping.getHandler(414) - Mapped to ResourceHttpRequestHandler ["/"]
21:17:57.048 [http-nio-8080-exec-9] DEBUG o.s.w.s.r.ResourceHttpRequestHandler.handleRequest(487) - Resource not found
21:17:57.048 [http-nio-8080-exec-9] DEBUG o.s.web.servlet.DispatcherServlet.logResult(1131) - Completed 404 NOT_FOUND
My web.xml dispatcher servlet section:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
My webapp context:
<mvc:resources location="/" mapping="/**"></mvc:resources>
<mvc:annotation-driven>
<mvc:path-matching trailing-slash="true" />
</mvc:annotation-driven>
<context:component-scan base-package="com.example.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/spring/views/" />
<property name="suffix" value=".jsp" />
</bean>
How can I configure Spring MVC to automatically add '/' in a path request (E.g. /app)?
Letting Tomcat serve static resources solves my problem. I removed Spring's static resources handler:
<mvc:resources location="/" mapping="/**"></mvc:resources>
and added the default servlet handler in order to let Tomcat handle requests of static files:
<mvc:default-servlet-handler/>