我在 Tomcat 上遇到了 Spring MVC 的这个问题,我认为这与这篇文章中解决的问题相同,但他们没有发布解决方案。
Web.xml
<!-- Spring MVC app -->
<servlet>
<servlet-name>client</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/client/client-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>client</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
客户端-servlet.xml
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<context:component-scan base-package="com.company.client.controllers" />
<!-- Enable an interceptor to set up the Trace objects for all Controller invocations -->
<mvc:interceptors>
<bean class="com.company.client.interceptor.TraceInterceptor"/>
</mvc:interceptors>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/WEB-INF/client/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/client/views directory -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/client/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
我使用标准的 ContextLoaderInitializer。
我的控制器使用@RequestMapping(value = {"", "/*"}, method = RequestMethod.GET)
@RequestMapping(value = {"", "/*"}, method = RequestMethod.GET)
public String home(Model model) {
retrieveOffers(model);
logger.warning("Loading home page");
return "main";
}
当我访问 mydomain.com 或 mydomain.com/ - 我得到 Tomcat 405 GET not supported 错误。如果我在尾随路径中放置任何内容,它会很好地加载页面。(例如 mydomain.com/a)
有趣的是,我总是看到“加载主页”日志语句,从我的日志来看,请求之间没有区别。似乎 Tomcat 在控制器完成和视图渲染之间的某个时间拦截了响应。有什么想法吗?