我在Tomcat上遇到了Spring MVC的這個問題,我認爲這個問題與post中的問題相同,但他們沒有發佈解決方案。tomcat 7上的Spring MVC爲沒有路徑的請求提供HTTP 405
的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不支持錯誤。 如果我把任何東西放在尾隨路徑中,它就會加載頁面。 (例如mydomain.com/a)
有趣的是,我總是看到「加載主頁」日誌語句,並且從我的日誌中請求之間沒有區別。就好像Tomcat在控制器完成和視圖渲染之間截獲響應。有什麼想法嗎?
感謝您的幫助 - 我添加了spring配置,基本上我已經準確地發佈了您所發佈的內容,除了我特意將viewClass設置爲JstlView(我認爲是默認設置)。奇怪的是與路徑和返回相同的字符串'主'視圖得到解決。 – 2012-08-10 16:58:46