http://localhost:[PORT]/[YOUR-WEBAPP]/dwr/index.html
to check that your spring beans appear.Spring 2.x includes a new feature named XML Namespace Handlers. This allows DWR and Spring MVC to remote Spring beans easily with a custom syntax.
If you're not using the MVC module, you can still leverage the namespace by mapping an org.directwebremoting.spring.DwrSpringServlet in your web.xml.
<servlet> <servlet-name>dwr</servlet-name> <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
The first task you need to accomplish is adding the following lines (in bold, bellow) 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: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-2.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
Once you have added the namespace declaration any modern IDE (NetBeans, Eclipse, Idea) will automatically help you to write the tags with its code completion features.
You must declare one <dwr:configuration>
tag. This tag is mandatory even if empty. It may have nested tags (init, creator, signatures,..). These nested tags mimic the behavior of those available in dwr.xml. Understand their functioning first and then use your IDE to translate to the correct syntax.
Again you must declare one <dwr:controller id="dwrController" debug="true" />
tag. This tag does not allow inner tags. In Spring MVC, for each controller you have to map the URLs that it will handle. The simplest way is probably declaring a SimpleUrlHandlerMapping. DWR needs mappings for the following URLs: /engine.js, /interface.js, /call/**, /interface/**
. Remember that a DWR Controller is just needed (and useful) in Spring MVC environments (use the servlet in other cases) and this way you get the number of other related Spring services (ie localization).
Inside each bean you want to remote include a <dwr:remote javascript="Fred">
tag. There you can specify the methods that are going to be proxied and those that won't. For example:<bean id="timeConvert" class="com.mycompany.ui.util.TimeConvert">
<dwr:remote javascript="AjaxTimeConvert">
<dwr:include method="convert" />
</dwr:remote>
</bean>
One of the common pitfalls when integrating Spring and DWR are scoped beans (session, request, ...). In practice is easy to get them going, just remember two basic rules.
<aop:scoped-proxy proxy-target-class="false" />
in the bean declarationHere's an example:
<bean id="calc" class="...CalculatorImpl" scope=session>
<dwr:remote javascript="Calculator">
<dwr:include method="add"/>
</dwr:remote>
<aop:scoped-proxy proxy-target-class="false" />
</bean>
If you're receiving the dreaded object is not an instance of declaring class
error always check these things:
<aop:aspectj-autoproxy proxy-target-class="false" />
in your Spring XML<aop:scoped-proxy />
This kind of integration has been discussed in depth some times by now. Try reading:
New configuration options are already committed in CVS. They're available today for those who like living on the edge (development). The most important are the automatic mapping of URLs (<dwr:url-mapping />
)and the use of annotations to remote Spring beans (<dwr:annotation-config />
). As you can see, they make even further use of the namespace feature. They will be properly documented here with the new release. This new features require Spring 2.5 at least.
If you feel comfortable using dwr.xml, you may use the Spring creator. This creator will lookup beans in your Spring <beans>.xml file and rely on Spring to instantiate them. This creator will be useful to you if you already use Spring and totally useless if you don't.
You allow DWR to use the spring creator to create and remote your beans as follows:
<allow> ... <create creator="spring" javascript="Fred"> <param name="beanName" value="Shiela"/> </create> ... </allow>
There are several ways to find your spring configuration files:
<allow> ... <create creator="spring" javascript="Fred"> <param name="beanName" value="Shiela"/> <param name=location value="beans.xml"/> </create> ... </allow>
setOverrideBeanFactory(BeanFactory)
method that provides a way to programatically override any BeanFactories found by other means (if any).Please, take into account that not all methods are equally easy in practice. Probably, unless you know what you're doing you're better served using one of the first two.