Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

In applicationContext.xml or some other context file that's loaded by web.xml, add the following:

Code Block
xmlxml
titleapplicationContext.xml
xml
<bean id="ajaxResponseController" class="org.jasig.web.servlet.mvc.AjaxResponseController"/>

    <bean class="org.springframework.web.servlet.view.XmlViewResolver"
    	p:order="0"/>

By default XmlViewResolver looks for WEB-INF/views.xml
jsonView automatically digests your model objects and represents them as JSON. You can probably tweak this by manipulating JsonConfig in the jsonView bean.

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

    <bean id="jsonView" class="net.sf.json.spring.web.servlet.view.JsonView">
        <property name="contentType" value="application/json" />
    </bean>
</beans>

Set the order parameter for your existing view resolvers to something greater than 0.

xml
Code Block
xml
titleFreemarker View Resolver example
xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
		p:order="1"
		p:cache="true"
		p:prefix=""
		p:suffix=".ftl"/>

...

The AJAX Servlet listens for AJAX requests. Add the following sections to your web.xml file in the appropriate locations.

xml
Code Block
xml
titleweb.xml excerpt
xml
<servlet>
        <servlet-name>AjaxResponseServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/ajaxResponseServletContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

<servlet-mapping>
        <servlet-name>AjaxResponseServlet</servlet-name>
        <url-pattern>/ajaxResponse</url-pattern>
    </servlet-mapping>

Create this file in the location specified above.

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

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="order" value="0"/>
        <property name="alwaysUseFullPath" value="true"/>
        <property name="defaultHandler" ref="ajaxResponseController" />
        <property name="urlMap">
            <map>
                <entry key="/ajaxResponse" value-ref="ajaxResponseController"/>
            </map>
        </property>
    </bean>

    <bean id="requiredAnnotationBeanPostProcessor" class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
</beans>

...

With an annotated controller method, this looks something like this. (Note: This uses an alternate way of passing the model)

java
Code Block
java
titleAnnotated Controller Method Example
java
@RequestMapping(params="action=ajaxTest")
public void ajaxTest(ActionRequest request,ActionResponse response) throws Exception {
   final Map<String, Object> model = new HashMap<String, Object>();
   model.put("testData", "foo");
   this.ajaxPortletSupportService.redirectAjaxResponse(this.ajaxServletName, model, request, response);
}

...