How to integrating DWR with Spring MVC

DWR

DWR: Easy AJAX for JAVA

DWR is a RPC library which makes it easy to call Java functions from JavaScript and to call JavaScript functions from Java (a.k.a Reverse Ajax).

DWR has a number of features like call batching, marshalling of virtually any data-structure between Java and Javascript (including binary file uploading and downloading), exception handling, advanced CSRF protection and deep integration with several Java server-side technologies like Spring and Guice.

In this tutorial we will show “How to integrating DWR with Spring MVC”

Initial considerations

  • Make sure you have the appropriate version of Spring. DWR 3 requires Spring version 2.5 or greater.
  • Make sure you understand everything on the getting started with DWR page.
  • Make sure your Spring beans work properly outside of DWR (unit test).
  • Select your configuration style based on the version of Spring you are using, etc. (see below).
    Configure DWR to work with Spring.
  • Check the DWR generated test page: http://localhost:[PORT]/[WEBAPP]/dwr/index.html to make sure your spring beans appear.

For Spring MVC, web.xml should look something like this:

<servlet>
	<servlet-name>springDispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:projectSpringContext.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>springDispatcher</servlet-name>
  	<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
  	<servlet-name>springDispatcher</servlet-name>
  	<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

The only thing you need to add is the mapping of /dwr/* to the Spring dispatcher servlet. If you are mapping your dispatcher servlet to / or /*, you will still need the /dwr/* mapping and it must be defined before the global mapping

The namespace

The first task you need to accomplish is adding the following lines (in bold, below) to any of your Spring XML files that includes at least one DWR specific tag. Add them inside the beans declaration (at the beginning of the file):

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" 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/mvc
      	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      	http://www.directwebremoting.org/schema/spring-dwr
      	http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

...
</beans>

If you are using Spring MVC you must declare one tag.

You may also specify configuration parameters for the dwrController:

<dwr:controller id="dwrController" debug="true">
	<dwr:config-param name="activeReverseAjaxEnabled" value="true"></dwr:config-param>
</dwr:controller>

The configuration tag

The is used to mimic the behavior of the configuration available in dwr.xml. This tag is optional and it may have nested tags (init, creator, signatures,..). These nested tags mimic the behavior of those available in dwr.xml. Example:

<dwr:configuration>
	<dwr:convert type="bean" class="com.jkoder.bean.KeywordBean"></dwr:convert>
</dwr:configuration>
</pre>
<p style="text-align: justify;"><strong>The remote tag</strong></p>
<p style="text-align: justify;">Inside each bean you want to remote include a tag. There you can specify the methods that are going to be proxied and those that won't. For example:</p>


<bean id="timeConvert" class="com.jkoder.dwr.GleanAjaxDwr">
	<dwr:remote javascript="gleanAjaxDwr"></dwr:remote>
</bean>

Finally your Dispatcher Servlet xml should look like this

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" 
	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/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.directwebremoting.org/schema/spring-dwr 
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

	...
	
	<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
	    <property name="definitions">
	        <list>
	            <value>/WEB-INF/tiles-defs.xml</value>
	        </list>
	    </property>
        </bean>
        <!-- DWR configuration -->
        <dwr:controller id="dwrController" debug="true"/>

        <!-- Configure DWR handlers -->
	<bean id="dwrUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    	<property name="alwaysUseFullPath" value="true"/>
    	<property name="mappings">
        	<props>
            	<prop key="/dwr/**/*">dwrController</prop>
        	</props>
    	</property>
	</bean>

	<!-- Configure Convertor -->
	<dwr:configuration>
		<dwr:convert type="bean" class="com.jkoder.bean.KeywordBean"/>
	</dwr:configuration>
	

        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>/WEB-INF/properties/messages</value>
			</list>
		</property>
		<property name="cacheSeconds" value="60" />
		<property name="useCodeAsDefaultMessage" value="true" />
	</bean>
	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass">
			<value>org.springframework.web.servlet.view.tiles2.TilesView</value>
		</property>
	</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>
	
	<bean id="timeConvert" class="com.jkoder.dwr.GleanAjaxDwr">
		<dwr:remote javascript="gleanAjaxDwr"/>
	</bean>
</beans>