Ajax request processing using Spring MVC 3.X

Are you trying to integrate DWR and Spring MVC 3.X? If your answer is yes, then you’re wasting valuable development time in fixing integration problems.
May be it is good to have DWR library for Ajax calls while developing a web application, but when you are using Spring MVC3 for developing your application this is not a good solution, because Spring Frame work itself capable for handling Ajax Calls. In this article, I would like to explain

Basic way of handling Ajax calls using Spring MVC:

  1. Dependency configuration:
    <dependency>
    
    <groupId>org.springframework</groupId>
    
    <artifactId>spring-beans</artifactId>
    
    <version>3.1.3.RELEASE</version>
    
    </dependency>
    
    <dependency>
    
    <groupId>org.springframework</groupId>
    
    <artifactId>spring-context</artifactId>
    
    <version>3.1.3.RELEASE</version>
    
    </dependency>
    
    <dependency>
    
    <groupId>org.springframework</groupId>
    
    <artifactId>spring-web</artifactId>
    
    <version>3.1.3.RELEASE</version>
    
    </dependency>
    
    <dependency>
    
    <groupId>org.springframework</groupId>
    
    <artifactId>spring-webmvc</artifactId>
    
    <version>3.1.3.RELEASE</version>
    
    </dependency>
    
    <dependency>
    
    <groupId>org.springframework</groupId>
    
    <artifactId>spring-core</artifactId>
    
    <version>3.1.3.RELEASE</version>
    
    </dependency>
    
     
  2. Server Side Coding: The code below describes how to write a Controller to process asynchronous requests.
    Content in this post has been moved,  please Click Here…  for new location of the actual contentSorry for the inconvenience. Please click the above link to continue to read.This is all because the content has been moved to the new website www.dailypaper.in/technical/.

    Please follow me in the new website www.dailypaper.in/technical for more technical stuff.

Advertisements

Integrating DWR3 and Spring3

Exceptions while integrating DWR3 and Spring3.X annotation based MVC:

Following are the solutions for the Exception commonly occur while integration:

  1. “org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionParser for element [annotation-scan]”.

Solution:

  • Don’t use DWR with version number  DWR 3.0M1(available in Maven Repository)
  • Use DWR0-RC1-116 or later version. You can down load the latest version from
<repositories>
 <repository>
   <id>repository.sonatype.org -public</id>
   <name>DWR repository</name>
   <url>https://oss.sonatype.org/content/groups/public</url>
 </repository>
</repositories>
<dependency>
 <groupId>org.directwebremoting</groupId>
 <artifactId>dwr</artifactId>
 <version>3.0.0-rc3-SNAPSHOT</version>
</dependency>
 2.If you are trying to keep DWR and Spring Configuration in the same xml file there may be a chance to occur duplicate name found Exception. 
org.springframework.beans.factory.BeanCreationException:Error creating bean with name ‘DwrAnnotationURLMapper’: Initialization of bean failed;nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name ‘dwrController’: Invocation of init method failed;nested exception is java.lang.IllegalArgumentException:java.lang.IllegalArgumentException:Duplicate name found
Solution: keep separate dwr.xml and don’t import into spring-config.xml file instead configure in web.xml as shown below

<servlet>
<servlet-name>SampleDWR</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath: dwr.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
  1. No adapter for handler [directwebremoting.spring.DwrController@1b3f577]: Does your handler implement a supported interface like Controller?

Solution: keep the following line of code in dwr.xml file

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

This is because mvc: annotation-driven element doesn’t declare a SimpleControllerHandlerAdapter

  1. If you are using Tomcat7 and DWR.0-RC1-116.jar file then you will get

“CSRF Security Error”: java.lang.SecurityException: CSRF Security Error
Solution:

  • One fix is to make the default Tomcat 7 tomcat/conf/context.xml (or your application’s context XML) put the useHTTPOnly on the Context element, like so:
    <Context useHttpOnly=”false”>
  • You can also fix this issue by using the latest DWR build

https://oss.sonatype.org/content/groups/public/org/directwebremoting/dwr/3.0.0-rc3-SNAPSHOT/