Results 1 to 3 of 3

Thread: Spring MVC and DWR - making it work

  1. #1
    Join Date
    Feb 2008
    Posts
    7

    Default Spring MVC and DWR - making it work

    Hi!

    I have a simple, working Spring MVC application that has one bean configuration file: myapp-servlet.xml

    In that file, I initialize data source, data beans, Data Access Object (DAO) and service classes.

    I want to retrieve some data on the front end using DWR Ajax.

    I added dwr.jar to lib, dwr servlet to web.xml, added DTD references to DWR in bean declarations, added dwr.xmlthat specifies the service method I would like to call from JavaScript:

    Code:
    <dwr>
      <allow>
        <create creator="new" javascript="ServiceManager">
          <param name="class" value="myapp.service.SimpleProductManager"/>
        </create>
      </allow>
    </dwr>
    The JavaScript correctly invokes the methods on the ServiceManager from JSP. One major problem - the DAO and other classes are not being initialized/injected. I.e., when DWR invokes the service class, the myapp-servlet.xml is not being loaded and the DAO object called from the service class is not being properly initiatized (so I get the NullPointerException).

    How can I fix that and force DWR to inject all the objects defined in myapp-servlet.xml?

  2. #2

    Default

    Maybe I don't understand your question, but I use Dependency Injection as usual to my objects and then expose them to DWR, like.

    Code:
    <bean id="myController" class="my.namespace.MyController">   
      <property name="myProp" ref="myProp"/> 
      <dwr:remote javascript="MyController" />
    </bean>

  3. #3
    Join Date
    Feb 2008
    Posts
    7

    Default

    Quote Originally Posted by TSH View Post
    Maybe I don't understand your question, but I use Dependency Injection as usual to my objects and then expose them to DWR, like.

    Code:
    <bean id="myController" class="my.namespace.MyController">   
      <property name="myProp" ref="myProp"/> 
      <dwr:remote javascript="MyController" />
    </bean>
    I tried that. I added the dwr:remote line to the myapp-servlet.xml like this:

    Code:
    <bean id="productManager" class="springapp.service.SimpleProductManager">
      <property name="productDao" ref="productDao"/>
      <dwr:remote javascript="ProductManager" />
    </bean>
    ProductManager, in turn, calls the productDao class that pull data from the database. The DAO still remained null and uninitialized when productManager is invoked by DWR from JSP - so the problem persists.

    In myapp-servlet.xml I have the DAO declared as follows - do I need to add/change anything:

    Code:
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
          <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
          <property name="username"  value="root"/>
          <property name="password" value="password"/>
    </bean>
        
    <bean id="productDao" class="springapp.repository.JdbcProductDao">
       <property name="dataSource" ref="dataSource" />
    </bean>
    Here is DWR call from JSP:

    Code:
    <script type='text/javascript' src='/springdwr/dwr/interface/ProductManager.js'></script>
    <script type='text/javascript' src='/springdwr/dwr/engine.js'></script>
    <script type='text/javascript' src='/springdwr/dwr/util.js'></script>
    
    <script>
    	function handleGetData(str) {
      		alert(str);
    	}
    	ProductManager.getProductDesc(handleGetData);	
    </script>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •