Spring et DWR

Direct Web remoting est un framework Ajax permettant un lien direct entre Javascript et Java coté serveur.

D'apparence assez simple, son intégration avec Spring peut rapidement devenir compliquée, notamment à cause de la gestion des namespaces dans le fichier XML de configuration. En effet, les versions récentes de DWR fournissent un namespace facilitant l'écriture des éléments spécifiques :


<dwr:controller id="dwrController" debug="true"/>
<dwr:configuration>...</dwr:configuration>

Mais puisque tout n'est pas rose, j'ai été confronté à une erreur récurrente au démarrage du contexte Spring : No bean named '__dwrConfiguration' found...

Quelque soit la version du namespace utilisé, pas moyen de m'en sortir... Voici donc comment revenir à une configuration plus simple basée sur des éléments XML plus classiques :

application-context-dwr.xml :


<?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: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.directwebremoting.org/schema/spring-dwr
                           http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
    
    <dwr:url-mapping />

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

    <bean id="myDwrServerBean" class="my.package.MyDwrServerBean">
        <dwr:remote javascript="serverBridge"></dwr:remote> <!-- nom de l'objet javascript utilisable -->
    </bean>


    
    <bean id="__dwrController" class="org.directwebremoting.spring.DwrController"> 
        <property name="configurators">
         <list>
            <ref bean="__dwrConfiguration"/>
         </list>
      </property>
      <property name="debug" value="true"/>
    </bean>
    
   <bean id="__dwrConfiguration" class="org.directwebremoting.spring.SpringConfigurator">
        <property name="creators">
            <map>
                <entry key="firstBean">
                    <bean class="org.directwebremoting.spring.CreatorConfig">
                        <property name="creator">
                            <bean class="org.directwebremoting.spring.BeanCreator">
                                <property name="bean" ref="myDwrServerBean"/>
                                <property name="javascript"><value>serverBridge</value></property>
                            </bean>
                        </property>
                    </bean>
                </entry>
            </map>
        </property>
        <property name="converters">
            <map>
                <entry key="my.package.MyDwrServerBean">
                    <bean class="org.directwebremoting.spring.ConverterConfig">
                        <property name="type">
                            <value>bean</value>
                        </property>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>
</beans>

Il est également possible de configurer DWR via un bean dans le contexte Spring en utilisant le FluentConfigurator.

En espérant vous avoir fait économiser quelques heures de recherches!


Fichier(s) joint(s) :

0 commentaires: