在Spring 2中整合DWR 2

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

众所周知,Spring 2通过XML Schema配置方式极大地简化的其配置,而且使得第三方扩展变为可能。配置如下代码所示:
Xml代码 复制代码
  1. <? xml version="1.0" encoding="UTF-8" ?>  
  2. < beans xmlns ="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >       
  5.        
  6.     <!-- <bean /> definitions here -->  
  7.        
  8. </ beans >  

清单1 applicationContext.xml

不知大家有没有想过spring-beans-2.0.xsd位置在那里?其实,大家可以用Eclipse打开Spring的jar包,展开META-INF,并双击打开其中的spring.schemas文件,内容如下:
Xml代码 复制代码
  1. http/://www.springframework.org/schema/beans/spring-beans- 2.0 .xsd = org/springframework/beans/factory/xml/spring-beans- 2.0 .xsd   
  2. http/://www.springframework.org/schema/tool/spring-tool- 2.0 .xsd = org/springframework/beans/factory/xml/spring-tool- 2.0 .xsd   
  3. http/://www.springframework.org/schema/util/spring-util- 2.0 .xsd = org/springframework/beans/factory/xml/spring-util- 2.0 .xsd   
  4. http/://www.springframework.org/schema/aop/spring-aop- 2.0 .xsd = org/springframework/aop/config/spring-aop- 2.0 .xsd   
  5. http/://www.springframework.org/schema/lang/spring-lang- 2.0 .xsd = org/springframework/scripting/config/spring-lang- 2.0 .xsd   
  6. http/://www.springframework.org/schema/tx/spring-tx- 2.0 .xsd = org/springframework/transaction/config/spring-tx- 2.0 .xsd   
  7. http/://www.springframework.org/schema/jee/spring-jee- 2.0 .xsd = org/springframework/ejb/config/spring-jee- 2.0 .xsd   
  8.   
  9. http/://www.springframework.org/schema/beans/spring-beans.xsd = org/springframework/beans/factory/xml/spring-beans- 2.0 .xsd   
  10. http/://www.springframework.org/schema/tool/spring-tool.xsd = org/springframework/beans/factory/xml/spring-tool- 2.0 .xsd   
  11. http/://www.springframework.org/schema/util/spring-util.xsd = org/springframework/beans/factory/xml/spring-util- 2.0 .xsd   
  12. http/://www.springframework.org/schema/aop/spring-aop.xsd = org/springframework/aop/config/spring-aop- 2.0 .xsd   
  13. http/://www.springframework.org/schema/lang/spring-lang.xsd = org/springframework/scripting/config/spring-lang- 2.0 .xsd   
  14. http/://www.springframework.org/schema/tx/spring-tx.xsd = org/springframework/transaction/config/spring-tx- 2.0 .xsd   
  15. 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文件,内容如下:
Xml代码 复制代码
  1. http/://www.directwebremoting.org/schema/spring-dwr- 2.0 .xsd = org/directwebremoting/spring/spring-dwr- 2.0 .xsd  


然后,按照上面的路径打开spring-dwr-2.0.xsd文件,内容如下:
Xml代码 复制代码
  1. <? xml version="1.0" encoding="UTF-8" standalone="no" ?>  
  2.   
  3. <!-- 省略了版权信息 -->  
  4.   
  5. < xsd:schema xmlns ="http://www.directwebremoting.org/schema/spring-dwr"  
  6.             xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  
  7.             targetNamespace ="http://www.directwebremoting.org/schema/spring-dwr"  
  8.             elementFormDefault ="qualified"  
  9.             attributeFormDefault ="unqualified" >  
  10.   
  11. <!-- 省略了具体的定义 -->  
  12. </ xsd:schema >  

清单3 spring-dwr-2.0.xsd

文件spring-dwr-2.0.xsd告诉我们,其名称空间应为“http://www.directwebremoting.org/schema/spring-dwr”,所以我们在配置Spring 2时,应使用以上的名称空间,如下面的代码片段所示:
Xml代码 复制代码
  1. <? xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. < beans xmlns ="http://www.springframework.org/schema/beans"  
  4.     xmlns:dwr ="http://www.directwebremoting.org/schema/spring-dwr"  
  5.     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  7.        http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd" >  
  8.   
  9.     < dwr:configuration >  
  10.         < dwr:convert class ="net.blogjava.max.pws.domain.Album" type ="bean" >  
  11.             < dwr:exclude method ="photos" />  
  12.         </ dwr:convert >  
  13.         < dwr:convert class ="net.blogjava.max.pws.domain.Photo" type ="bean" >  
  14.             < dwr:exclude method ="original, poster, thumb, full, album" />  
  15.         </ dwr:convert >  
  16.     </ dwr:configuration >  
  17.   
  18.     < bean id ="ajaxFacade"  
  19.         class ="net.blogjava.max.pws.web.ajax.AjaxFacade" >  
  20.         < dwr:remote javascript ="AjaxFacade" />  
  21.         < property name ="personalWebSite" ref ="personalWebSite" />  
  22.     </ bean >  
  23.   
  24. </ beans >  

清单3 ajaxContext.xml
WEB-INF/web.xml配置

通过上面的配置,我们可以省去dwr.xml配置,不过在web.xml配置dwr的Servlet时,要使用新的Servlet类。配置代码片段如下:
Xml代码 复制代码
  1. < servlet >  
  2.     < servlet-name > dwr </ servlet-name >  
  3.     < servlet-class > org.directwebremoting.spring.DwrSpringServlet </ servlet-class >  
  4.     < init-param >  
  5.         < param-name > debug </ param-name >  
  6.         < param-value > true </ param-value >  
  7.     </ init-param >  
  8.     < load-on-startup > 1 </ load-on-startup >  
  9. </ servlet >  
  10.   
  11. < servlet-mapping >  
  12.     < servlet-name > dwr </ servlet-name >  
  13.     < url-pattern > /dwr/* </ url-pattern >  
  14. </ servlet-mapping >  

清单4 web.xml
总结

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值