Monday, June 18, 2012


Configuration of Dwr 3 with Spring 3.0




If you are using spring MVC then web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                                                                     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringDwr</display-name>
  <servlet>
    <servlet-name>springdwr</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet- class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springdwr</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

 <servlet-mapping>
  <servlet-name>springdwr</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
 <welcome-file-list>
        <welcome-file>add.jsp</welcome-file>
</welcome-file-list>
           
</web-app>


You will not require to have dwr.xml.


Along with the spring configuration in your application-servlet.xml following entries will be done:

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


<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
   
<!-- DWR will scan all Spring managed beans containing @RemoteProxy or
     @RemoteMethod annotations and register Creator proxies for them.
     This will NOT scan any classes not managed by Spring. -->
      <dwr:annotation-config id="springdwr" />

<!-- DWR will scan the classpath and search classes containing @RemoteProxy                                                                                                                                                                                                            or @RemoteMethod annotations. This will register the beans and Creator proxies for these classes.-->
      <dwr:annotation-scan base package="com.springdwr.controller,com.springdwr.service" scanDataTransferObject="true" scanRemoteProxy="true" />

      <!-- DWR will map util.js and engine.js files to the dwrController. You can then include this files as external Javascript references from your JSP -->
      <dwr:url-mapping />

      <!-- Defines the dwrController. During production, set the debug property to false -->
      <dwr:controller id="dwrController" debug="true" />

<!-- This is required if you want to configure any beans not managed by Spring. Leaving it enabled doesn't do any negative effects. Here's a sample config: -->
      <dwr:configuration />
      <context:component-scan base-package="com.springdwr.*" />



The class which will be used:

package com.springdwr.service;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Service enables the class to be used as a Spring service
 * @RemoteProxy enables the class to be used as a DWR service
 * @Transactional enables transaction support for this clas
 */
@Service("springService")
@RemoteProxy(name="dwrService")
@Transactional
public class ArithmeticService {
      /**
       * @RemoteMethod exposes this method to DWR.
       * Your JSP pages can access this method as Javascript
       * Your Spring beans can still access this method.
       */
      @RemoteMethod
      public Integer add(Integer value1, Integer value2) {
            return value1+ value2;
      }
     
}


The JSP :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <script type='text/javascript' src="/SpringDwr/dwr/engine.js"></script>
 <script type='text/javascript' src="/SpringDwr/dwr/util.js"></script>
 <script type="text/javascript" src="/SpringDwr/dwr/interface/dwrService.js"></script>     
      <title>Spring 3.0 MVC – DWR 3.0 Integration Tutorial</title>
</head>
<body>
<h3>3.0 MVC – DWR 3.0 Integration Tutorial</h3>
<div style="border: 1px solid #000; width: 3000px;">
      Add Two Numbers: <br/>
      <input id="num1" type="text" size="5"> +
      <input id="num2" type="text" size="5">
      <input type="submit" value="Add" onclick="add()" /> <br/>
      Sum: <span id="sum">(Result will be shown here)</span>
</div>
<script type="text/javascript">
      function add() {
            // Retrieve the value of text inputs
            var operand1 = dwr.util.getValue("num1");
            var operand2 = dwr.util.getValue("num2");
           
            // Pass two numbers, a callback function, and error function
            dwrService.add(operand1, operand2, {
                  callback : handleAddSuccess,
                  errorHandler : handleAddError
            });
      }

      // data contains the returned value
      function handleAddSuccess(data) {
            // Assigns data to result id
            dwr.util.setValue("sum", data);
      } 
      function handleAddError() {
            // Show a popup message
            alert("Can't add the values!");
      }
</script>

</body>
</html>

4 comments:

  1. Hi,

    I follow your example with some slight tweakings. Below are my functions, the issue I am having is with my callback function - handleAddSuccess:

    dwrService.getNewCares(careIDs, {
    callback : handleAddSuccess,
    errorHandler : handleAddError
    });

    function handleAddSuccess(data) {
    var caresArray = data;
    alert(" data.length = " + data.length) ;
    //this works!
    for (i = 0; i < data.length; i++) {
    dwr.util.setValue("definition"+i, data[i].definition);
    dwr.util.setValue("situation"+i, data[i].situation);
    alert(" definition"+i+" = " + data[i].definition) ;
    alert(" situation"+i+" = " + data[i].situation) ;
    alert(" endo of datalength " ) ;
    }
    }

    I seem to be able to set the values for these html ids as I can see them temporarily, however, they disappear once the outer fn finished.

    I read that callback in JS does not return values, is that the reason why I am not able to set these values permanently? I don't think I am returning values, but setting them with setValue ... I might be wrong. You could set the value for one item, sum. I am setting the values which are returned from a collection - not sure if that makes any difference.

    I looked in the net regarding callbacks, and I am hoping to avoid it if possible. If you could shed some lights, it will really be helpful. Tks!

    Lin


    ReplyDelete
    Replies
    1. Hey Lin,

      As per my understanding this "callback in JS does not return values" is not the reason, because if that must me the case then you weren't able to see them temporarily too. So problem must be with the retaining and that too in JS. So if you can provide your jsp code may be i will be able to figure out where we are going wrong.

      Thanks :)

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Great and I have a super proposal: Where To Learn Home Renovation home renovation jobs near me

    ReplyDelete