Just remove the @ResponseBody
annotation from ajaxAction()
method.
That way Spring MVC will call the view resolver and will try to display /WEB-INF/pages/ajax.jsp
. @ResponseBody
means that the framework will call the Jackson mapper that will parse the object you return from the controller method to JSON. When you use @ResponseBody
you can return any object that Jackson can process. When you want to redirect to some page( without @ResponseBody
) you need to return String that will be handled by the view resolver.(or return the old school ModelAndView
object)
servlet-mapping and spring InternalResourceViewResolver: which controls what mapping?
Question
My configuration looks like this:
web.xml
<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>
mvc-dispatcher-servlet.xml
<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>
My controller code looks like this:
PersonController2.java
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";
}
}
WEB-INFO/pages/index.jsp
<%@ 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>
WEB-INFO/pages/ajax.jsp
<!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>
when I hit rest/list, it returns JSON just fine. All my JSPs are in WEB-INF/pages including ajax.jsp which will call rest/list and parse the returned JSON. What I can't figure out is how to invoke ajax.jsp. Any help would be greatly appreciated.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow