我的配置如下:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Demo Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<tx:annotation-driven />
<mvc:annotation-driven />
<context:component-scan base-package="com.company.controller" />
<context:component-scan base-package="com.company.service" />
<context:component-scan base-package="com.company.dao" />
<context:component-scan base-package="com.company.hibernate" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.databaseurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="validationQuery" value="SELECT 1" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
我的控制器代码如下所示:
package com.company.controller;
@Controller
@RequestMapping("rest")
public class PersonController2 {
@Autowired
private PersonService personService;
@RequestMapping(value = "list", method = RequestMethod.GET)
public @ResponseBody
List<Person> listAction() {
return personService.listPerson();
}
@RequestMapping(value = "ajax", method = RequestMethod.GET)
public @ResponseBody
String ajaxAction() {
return "ajax";
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<style>
table,th,td
{
border:1px solid black;
background-color:#00FF00;
}
</style>
</head>
<body>
<table align="center">
<tr>
<td style="text-decoration: blink">
<a href="/myprj/ajax">
A simple page using Spring 3 restful service, ajax and AngularJS</a>
</td>
</tr>
</table>
</body>
</html>
<!doctype html>
<html lang="en" ng-app id="ng-app">
<head>
<title>My Page</title>
<script src="scripts/angular.min.js"></script>
<script>
function AjaxGet($scope, $http) {
$http({
method : 'GET',
url : 'myprj/rest/list'
}).success(function(data) {
$scope.persons = data; // response data
});
}
</script>
</head>
<body>
<div id="ng-app" ng-app ng-controller="AjaxGet">
<div ng-repeat="person in persons">
<h2>
<a href='{{person.firstName}}'></a>
</h2>
</div>
</div>
</body>
</html>
当我点击rest / list时,它返回JSON就好了。我的所有JSP都在WEB-INF / pages中,包括ajax.jsp,它将调用rest / list并解析返回的JSON。我无法弄清楚的是如何调用ajax.jsp。任何帮助将不胜感激。
答案 0 :(得分:1)
只需从@ResponseBody
方法中删除ajaxAction()
注释即可。
这样Spring MVC将调用视图解析器并尝试显示/WEB-INF/pages/ajax.jsp
。 @ResponseBody
表示框架将调用Jackson映射器,该映射器将从控制器方法返回的对象解析为JSON。当您使用@ResponseBody
时,您可以返回杰克逊可以处理的任何对象。当您想要重定向到某个页面(没有@ResponseBody
)时,您需要返回将由视图解析器处理的String。(或返回旧学校ModelAndView
对象)