我已經用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" />
我不知道爲什麼/項目不起作用。有人有個想法嗎?
感謝
如何計算/項目通過ajax? – Rembo
不,來自地址欄的瀏覽器請求。 – Gerrit
然後刪除'ResponseBody'註釋,你應該從Get方法返回視圖名稱。 – Rembo