【发布时间】:2014-02-26 08:04:23
【问题描述】:
我用 STS 创建了一个 Spring MVC 项目。在这个项目中,我有带有 @RequestMapping("/") 的默认 HomeController。该控制器被正确调用。
那我还有这个控制器
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;
}
}
但是在 /projects 我的 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" />
我不知道为什么 /projects 不起作用。有人有想法吗?
谢谢
【问题讨论】:
-
你如何使用 /projects 使用 ajax?
-
不,来自地址栏的浏览器请求。
-
然后删除
ResponseBody注释,您应该从 Get 方法返回视图名称。 -
我总是想检索 json
-
然后通过 AJAX 调用并在您的视图中呈现数据。
标签: spring spring-mvc