2016-04-28 80 views
1

比方說,我有以下春天控制器:爲什麼Spring DispatcherServlet找不到我的控制器方法?

@Controller 
public class FooController 
{ 
    @RequestMapping(value = "/foo", method = RequestMethod.GET, headers = { "Content-Type=text/plain" }) 
    @ResponseBody 
    public String foo(String bar) 
    { 
     return "Bar is " + bar; 
    } 
} 

下面是相關的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" 
    version="2.5"> 
    <display-name>Foo</display-name> 
    <servlet> 
    <servlet-name>FooServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>0</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>FooServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 
</web-app> 

而這裏的相關servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
    <mvc:annotation-driven/> 
    <bean id="FooController" class="foo.FooController"/> 
</beans> 

FYI ,我正在使用Spring Framework 3.0.5版。我知道這是一箇舊版本,但這是一個我必須努力的約束。

因此,我將從上面構建的WAR文件(例如FooApp.war)部署到Tomcat(版本7.0.67)。一切都很好。但是,當我瀏覽到http://localhost:8080/FooApp/foo?bar=baz,我在瀏覽器中得到一個404錯誤,並在Tomcat的控制檯以下:

Apr 28, 2016 9:43:33 AM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleNoSuchRequestHandlingMethod 
WARNING: No matching handler method found for servlet request: path '/foo', method 'GET', parameters map['bar' -> array<String>['baz']] 

所以我的問題是,我在做什麼錯在我的配置是造成這個問題?我確定這是一件事,但我很難弄清楚它是什麼。

編輯:刪除了SimpleUrlHandlerMapping,根據M. Deinum的評論。

+0

你能看到在啓動日誌中創建的映射嗎?如果創建映射,那肯定會在那裏提及。你也使用註釋,所以你爲什麼不添加組件:掃描到你的servlet配置,你不需要做手動的URL處理器映射。這在春季支持3 –

+1

刪除'SimpleUrlHandlerMapping'。 –

+0

@ M.Deinum謝謝,我刪除了它,但我仍然遇到問題。我猜'SimpleUrlHandlerMapping'無論如何都是多餘的? –

回答

0

我從here下載了示例項目,並開始研究它和我的測試項目有什麼不同。我終於縮小了它在我的控制器方法中@RequestMapping註釋中存在的headers = { "Content-Type=text/plain" }。刪除它後,一切正常。

-1

將「context:component-scan base-package =」軟件包名稱「/」添加到servlet.xml中。這將啓用您的控制器的自動掃描。

+0

是的,我意識到這一點,但我想了解我在手動設置控制器時做了什麼錯誤。簡單地將它留給Spring Framework的「automagic」並沒有給我更多的見解。 –

+0

你在做什麼手動設置控制器。查看何時在servlet.xml中只定義bean。您的控制器僅被註冊爲bean。沒有控制器屬性。好的,在你的xml中添加「context:annotation-config」。它會告訴Spring將Controller作爲請求處理程序bean,然後將其註冊爲控制器。 – Rahul

+0

在servlet.xml中定義了「context:annotation-config」之後,@Controller註釋將起作用,因爲它將指示spring搜索此註釋並將其註冊爲處理程序。我認爲這會幫助你 – Rahul

相關問題