2014-02-26 75 views
0

我已經用STS創建了一個Spring MVC項目。在這個項目中,我有@RequestMapping(「/」)的默認HomeController。該控制器被正確調用。@RequestMapping中的404錯誤(value =「/ projects」,method = RequestMethod.GET)

然後,我有另外該控制器

package de.gl.rm; 
import java.util.ArrayList; 
import java.util.List; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 

import de.gl.rm.model.Project; 

@Controller 
public class Projects { 

    @RequestMapping(value = "/projects", method = RequestMethod.GET) 
    public @ResponseBody List<Project> getProjects() { 
     Project p1 = new Project(1, "P1"); 
     Project p2 = new Project(2, "P2"); 

     ArrayList<Project> list = new ArrayList<Project>(); 
     list.add(p1); 
     list.add(p2); 
     return list; 
    } 
} 

但在/項目我的webapp不響應。我的配置是一樣

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/js/**" location="/resources/js" /> 
<resources mapping="/css/**" location="/resources/css" /> 
<resources mapping="/img/**" location="/resources/img" /> 
<resources mapping="/resources/**" location="/resources" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

<context:component-scan base-package="de.gl.rm" /> 
<context:component-scan base-package="de.gl.rm.controller" /> 

我不知道爲什麼/項目不起作用。有人有個想法嗎?

感謝

+0

如何計算/項目通過ajax? – Rembo

+0

不,來自地址欄的瀏覽器請求。 – Gerrit

+0

然後刪除'ResponseBody'註釋,你應該從Get方法返回視圖名稱。 – Rembo

回答

1

我可以給你答案的一部分:

1-刪除<context:component-scan base-package="de.gl.rm.controller" />因爲<context:component-scan base-package="de.gl.rm" />將掃描de.gl.rm和子包爲好。

2-刪除

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

因爲你有一個@ResponseBody(通常用於處理網頁內的一些AJAX請求),所以它只是當你請求localhost:8080/rm/projects不需要一個視圖解析器的打印List<Project>,但你需要一個內容關聯,如果你想要JSON,添加produces = {"application/json"}並添加lib來完成你的pom,jackson的工作。

3-你的web.xml中需要用servlet調度配置:

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/rm/*</url-pattern> 
</servlet-mapping> 

隨着rmurl-pattern,因爲您的網址是localhost:8080/rm/projects(假設你的上下文路徑是/),否則這將是localhost:8080/<context-path>/rm/projects

4-我建議你只使用註解配置(沒有任何XML和一個web.xml空)

+0

我不會;噸建議刪除的視圖解析器,它probabyl其他地方使用 – NimChimpsky

+0

是肯定的,它只是,如果我們只使用@ResponseBody,它只是爲理解,但你的權利,這當然是在別處使用。 –

0

什t確切地說是記錄的輸出,但有一些問題,但它們都不會導致404 ...

您正在返回一個reposnsebody,因此它將返回一個項目列表,如json或xml 。然而,由於這是一個自定義類,您將需要提供一個轉換器/序列化器。

添加mvc-annotation驅動到您的配置,這會給你默認的序列化器/轉換器(它可能會立即與你的項目類的toString方法一起工作,但不知道)。

0

使用

xmlns:mvc="http://www.springframework.org/schema/mvc" 

<mvc:annotation-driven /> 

在你的servlet的XML。

這爲我解決了404問題,當我試圖在我的控制器中使用BindingResults來檢查表單的結果是否與我的數據模型有關時(例如:不接受空名稱)。

希望這能解決你的問題:)

相關問題