随笔-34  评论-1965  文章-0  trackbacks-0

前些日子看了一篇关于在Spring 2中整合DWR 2的文章《AJAX, DWR and Spring》。最近,想动手试一下,就下载其源代码回来看看,依葫芦画瓢做了一遍。在运行时,得到XML验证错误。经过一翻折腾,终于把问题解决。

Spring 2基于XML Schema的配置

众所周知,Spring 2通过XML Schema配置方式极大地简化的其配置,而且使得第三方扩展变为可能。配置如下代码所示:

<? xml version="1.0" encoding="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >    
    
   
<!-- <bean /> definitions here -->
    
</ beans >
清单1 applicationContext.xml

不知大家有没有想过spring-beans-2.0.xsd位置在那里?其实,大家可以用Eclipse打开Spring的jar包,展开META-INF,并双击打开其中的spring.schemas文件,内容如下:

http\://www.springframework.org/schema/beans/spring-beans- 2.0 .xsd = org/springframework/beans/factory/xml/spring-beans- 2.0 .xsd
http\://www.springframework.org/schema/tool/spring-tool-
2.0 .xsd = org/springframework/beans/factory/xml/spring-tool- 2.0 .xsd
http\://www.springframework.org/schema/util/spring-util-
2.0 .xsd = org/springframework/beans/factory/xml/spring-util- 2.0 .xsd
http\://www.springframework.org/schema/aop/spring-aop-
2.0 .xsd = org/springframework/aop/config/spring-aop- 2.0 .xsd
http\://www.springframework.org/schema/lang/spring-lang-
2.0 .xsd = org/springframework/scripting/config/spring-lang- 2.0 .xsd
http\://www.springframework.org/schema/tx/spring-tx-
2.0 .xsd = org/springframework/transaction/config/spring-tx- 2.0 .xsd
http\://www.springframework.org/schema/jee/spring-jee-
2.0 .xsd = org/springframework/ejb/config/spring-jee- 2.0 .xsd

http\://www.springframework.org/schema/beans/spring-beans.xsd
= org/springframework/beans/factory/xml/spring-beans- 2.0 .xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd
= org/springframework/beans/factory/xml/spring-tool- 2.0 .xsd
http\://www.springframework.org/schema/util/spring-util.xsd
= org/springframework/beans/factory/xml/spring-util- 2.0 .xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd
= org/springframework/aop/config/spring-aop- 2.0 .xsd
http\://www.springframework.org/schema/lang/spring-lang.xsd
= org/springframework/scripting/config/spring-lang- 2.0 .xsd
http\://www.springframework.org/schema/tx/spring-tx.xsd
= org/springframework/transaction/config/spring-tx- 2.0 .xsd
http\://www.springframework.org/schema/jee/spring-jee.xsd
= org/springframework/ejb/config/spring-jee- 2.0 .xsd
清单2 spring.schemas

从以上的文件中,可以看出XML Sechema文件在类包中位置。

DWR 2.0 RC 2中的XML Schema文件

根据上面的描述,我打开DWR的jar包中spring.schemas文件,内容如下:

http\://www.directwebremoting.org/schema/spring-dwr- 2.0 .xsd = org/directwebremoting/spring/spring-dwr- 2.0 .xsd

然后,按照上面的路径打开spring-dwr-2.0.xsd文件,内容如下:

<? xml version="1.0" encoding="UTF-8" standalone="no" ?>

<!-- 省略了版权信息 -->

< xsd:schema xmlns ="http://www.directwebremoting.org/schema/spring-dwr"
            xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
            targetNamespace
="http://www.directwebremoting.org/schema/spring-dwr"
            elementFormDefault
="qualified"
            attributeFormDefault
="unqualified" >

<!-- 省略了具体的定义 -->
</ xsd:schema >
清单3 spring-dwr-2.0.xsd

文件spring-dwr-2.0.xsd告诉我们,其名称空间应为“http://www.directwebremoting.org/schema/spring-dwr”,所以我们在配置Spring 2时,应使用以上的名称空间,如下面的代码片段所示:

<? xml version="1.0" encoding="UTF-8" ?>

< beans xmlns ="http://www.springframework.org/schema/beans"
    xmlns:dwr
="http://www.directwebremoting.org/schema/spring-dwr"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd"
>

   
< dwr:configuration >
       
< dwr:convert class ="net.blogjava.max.pws.domain.Album" type ="bean" >
           
< dwr:exclude method ="photos" />
       
</ dwr:convert >
       
< dwr:convert class ="net.blogjava.max.pws.domain.Photo" type ="bean" >
           
< dwr:exclude method ="original, poster, thumb, full, album" />
       
</ dwr:convert >
   
</ dwr:configuration >

   
< bean id ="ajaxFacade"
        class
="net.blogjava.max.pws.web.ajax.AjaxFacade" >
       
< dwr:remote javascript ="AjaxFacade" />
       
< property name ="personalWebSite" ref ="personalWebSite" />
   
</ bean >

</ beans >
清单3 ajaxContext.xml

WEB-INF/web.xml配置

通过上面的配置,我们可以省去dwr.xml配置,不过在web.xml配置dwr的Servlet时,要使用新的Servlet类。配置代码片段如下:

< 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 >
   
< load-on-startup > 1 </ load-on-startup >
</ servlet >

< servlet-mapping >
   
< servlet-name > dwr </ servlet-name >
   
< url-pattern > /dwr/* </ url-pattern >
</ servlet-mapping >
清单4 web.xml

总结

通过在Spring 2整合DWR 2配置,可以集中管理应用程序的配置,从一定程度上解决JavaEE开发中的配置文件泛滥的问题。

posted on 2007-01-31 16:45 Max 阅读(17377) 评论(6)  编辑  收藏 所属分类: 方法与技巧(Tips & tricks)

评论:
# re: 在Spring 2中整合DWR 2 2008-04-26 15:11 | yidwo
个人觉得这样把Spring的配置信息和dwr的配置信息放在一起.
认为相当之杂乱, 修改与维护时查找不方便,配置文件也显得冗长.
分开来更符合实际点.
dwr调用Spring类时可以如此:
<create javascript="jsname" creator="spring">
<param name="beanName" value="beanId" />
</create>  回复  更多评论
  
# re: 在Spring 2中整合DWR 2 2008-05-22 11:29 | xx
看不懂,乱  回复  更多评论
  
# re: 在Spring 2中整合DWR 2 2008-06-05 11:34 | elary
实际意义并不大,觉得只是浪费时间。  回复  更多评论
  
# re: 在Spring 2中整合DWR 2 2008-07-23 11:58 | 爱上对方是多方
可以在@yidwo
这样可以统一配置,并且可以随时注入需要的bean  回复  更多评论
  
# re: 在Spring 2中整合DWR 2 2009-12-17 16:47 | weiyi
哥们, 看了你的文章。 照着来, 可是结果老报错啊

(1)报错:
<dwr:configuration>
<dwr:create javascript="supPowerMenu" type="spring">
<dwr:param name="beanName" value="supPowerManager"/>
<dwr:include method="findRolePowerJDBC"/>
</dwr:create>
</dwr:configuration>

Info:
-------------------------
Exception on invoking context event listener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '__dwrConfiguration': Cannot resolve reference to bean '__supPowerMenu' while setting bean property 'creators' with key [supPowerMenu]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '__supPowerMenu': Cannot create inner bean '(inner bean)' of type [org.directwebremoting.spring.SpringCreator] while setting bean property 'creator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1': Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'beanClass' of bean class [org.directwebremoting.spring.SpringCreator]: No property 'beanClass' found
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '__supPowerMenu': Cannot create inner bean '(inner bean)' of type [org.directwebremoting.spring.SpringCreator] while setting bean property 'creator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1': Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'beanClass' of bean class [org.directwebremoting.spring.SpringCreator]: No property 'beanClass' found
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1': Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'beanClass' of bean class [org.directwebremoting.spring.SpringCreator]: No property 'beanClass' found
Caused by: org.springframework.beans.InvalidPropertyException: Invalid property 'beanClass' of bean class [org.directwebremoting.spring.SpringCreator]: No property 'beanClass' found
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:376)

-------------------------

(2)不报错:
<dwr:create javascript="supUserCode" type="new" class="com.xxx.supervision.managers.impl.UserManagerImpl">
<dwr:include method="checkUserCodeUniqueJDBC"/>
</dwr:create>
但是DWR调试下,没生成supUserCode.js  回复  更多评论
  
# re: 在Spring 2中整合DWR 2 2009-12-17 16:51 | weiyi
说明下:supPowerManager是已经定义好的beanId,没问题。

帮帮忙! 指点指点

问题出在什么地方了?
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: