|
要讀懂我這篇文章估計需要一些前期知識:
你要知道Spring是什么,并且要知道Spring已經(jīng)出了2.0的版本了。 你要知道DWR是什么,并且要知道DWR也要出2.0的版本了。 呵呵,開個玩笑……。這年頭在BlogJava上混的,估計沒幾個不知道上面這兩個項了。 好了,現(xiàn)在言歸正傳。DWR本身就提供對Spring的支持,可以直接把Spring中的Bean暴露給客戶端瀏覽器的Javascript調(diào)用。在dwr.xml中的寫法是這樣的: <dwr>
<allow> <create creator="spring" javascript="AjaxPortalService"> <param name="beanName" value="AjaxPortalService"/> <include method="changeWondowState"/> <include method="changeWorkbenchState"/> <include method="changeWindowOrder"/> </create> </allow> </dwr> 這樣寫到也沒什么,只是把項目分模塊以后,雖有的配置都寫到一個dwr.xml文件里面維護起來比較麻煩,尤其再遇到版本管理中的代碼合并。所以曾經(jīng)我擴展了DWR,讓它可以從多個文件讀取配置信息。然后每個模塊自己一個配置文件,跟著source一起走。這樣做也有問題,就是現(xiàn)在Java應(yīng)用程序的配置文件太多啦!Spring的配置,WebWork的配置,Hibernate的配置,DWR的配置,再加上一些雜七雜八的xml和properties。看著這些配置文件簡直要瘋掉了。 正在此時,spring2橫空出世,現(xiàn)在你可以把一些配置文件合并到一起了。正好趁此機會體驗一下Spring2的自定義schema特性。 目標(biāo): 把DWR的配置寫成這樣: <?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:tx="http://www./schema/tx" xmlns:aop="http://www./schema/aop" xmlns:dwr="http://www./schema/spring/dwr" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-2.0.xsd http://www./schema/aop http://www./schema/aop/spring-aop-2.0.xsd http://www./schema/tx http://www./schema/tx/spring-tx-2.0.xsd http://www./schema/spring/dwr http://www./schema/spring/dwr.xsd" default-autowire="byName"> <bean id="departmentDao" class="cn.com.legendapl.hellostruts2.dao.DepartmentDao"/> <bean id="employeeDao" class="cn.com.legendapl.hellostruts2.dao.EmployeeDao"/> <bean id="companyService" class="cn.com.legendapl.hellostruts2.service.CompanyService"/> <aop:config> <aop:pointcut id="companyServiceOperation" expression="execution(* cn.com.legendapl.hellostruts2.service.ICompanyService.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="companyServiceOperation" /> </aop:config> <bean id="ajaxCompanyProxy" class="cn.com.legendapl.hellostruts2.ajax.AjaxCompanyProxy"/> <dwr:allow id="ajaxCompanyProxyAllow"> <dwr:create beanName="ajaxCompanyProxy" javascript="CompanyProxy"> <dwr:include method="findEmployeeById"/> </dwr:create> <dwr:convert converter="bean" match="cn.com.legendapl.hellostruts2.entity.*"/> </dwr:allow> </beans>
好了現(xiàn)在動手開始做。 <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://www./schema/spring/dwr" xmlns:xsd="http://www./2001/XMLSchema" xmlns:beans="http://www./schema/beans" targetNamespace="http://www./schema/spring/dwr" elementFormDefault="qualified" attributeFormDefault="unqualified">![]() <xsd:import namespace="http://www./schema/beans" schemaLocation="http://www./schema/beans/spring-beans-2.0.xsd"/> <xsd:element name="allow"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:sequence> <xsd:element name="create" type="createType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="convert" type="convertType" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> <xsd:complexType name="createType"> <xsd:sequence> <xsd:element name="auth" type="authType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="include" type="includeType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="exclude" type="excludeType" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attribute name="beanName" type="xsd:string" use="required"/> <xsd:attribute name="javascript" type="xsd:string" use="required"/> </xsd:complexType> <xsd:complexType name="convertType"> <xsd:attribute name="converter" type="xsd:string" use="required"/> <xsd:attribute name="match" type="xsd:string" use="required"/> </xsd:complexType> <xsd:complexType name="authType"> <xsd:attribute name="method" type="xsd:string" use="required"/> <xsd:attribute name="role" type="xsd:string" use="required"/> </xsd:complexType> <xsd:complexType name="includeType"> <xsd:attribute name="method" type="xsd:string" use="required"/> </xsd:complexType> <xsd:complexType name="excludeType"> <xsd:attribute name="method" type="xsd:string" use="required"/> </xsd:complexType> </xsd:schema>2、我們要做一個DWRNamespaceHandler來處理DWR的配置信息,其實里面就做一件事把AllowBeanDefinitionParser注冊給allow節(jié)點。因為我們dwr的配置部分根節(jié)點就一個標(biāo)簽allow,所以我們就做一個用于解析allow標(biāo)簽的AllowBeanDefinitionParser解析器就行。如果我們的根節(jié)點還有其他的標(biāo)簽,同樣也要做相應(yīng)的解析器。 public class DWRNamespaceHandler extends NamespaceHandlerSupport {
public void init() { //把AllowBeanDefinitionParser注冊到allow節(jié)點 registerBeanDefinitionParser("allow", new AllowBeanDefinitionParser()); } } 其實難點是做AllowBeanDefinitionParser,這里你需要一些DOM模型的知識,來操作配置節(jié)點的內(nèi)容。然后根據(jù)內(nèi)容進行處理。在這里我們,需要做三件事: [1] 把配置節(jié)點的內(nèi)容轉(zhuǎn)換成對象模型,即AllowBean(其實就是一些POJO罷了)。 [2] 把這個AllowBean注冊給Spring的Context,這一步是可選的。因為我們主要是把這個Bean給DWR,當(dāng)然順道給Spring一份也沒什么問題。 [3] 把這個AllowBean注冊到AllowBeanHolder。 3、AllowBeanHolder。 其實這就是個簡單的singleton類,整個運行期只有一個實例。它就像一個容器,AllowBeanDefinitionParser往里放,DWR的Configuration再從這里取。 4、擴展DWR的DefaultConfiguration,我們做一個SpringConfiguration。DWR的DefaultConfiguration是負責(zé)讀取配置信息的。我們在其中加入從AllowBeanHolder讀取配置信息的功能即可。 5、擴展DWR的DWRServlet,我們做一個SpringDWRServlet,原來的DWRServlet加載的是DefaultConfiguration,我們的Serlvet加載我們自己的SpringConfiguration即可。 6、萬事俱備,只欠東風(fēng)啦。就是讓Spring知道我們干了這些! 在META-INF目錄下(如果沒有在src目錄下創(chuàng)建一個)加入spring.handlers和spring.schemas兩個文件。 spring.handlers中的內(nèi)容: http\://www./schema/spring/dwr=org.devside.core.support.dwr.DWRNamespaceHandler spring.schemas中的內(nèi)容: http\://www./schema/spring/dwr.xsd=org/devside/core/support/dwr/dwr.xsd 注意不要寫錯字哦,我一開始就寫錯一個字母,結(jié)果怎么調(diào)試都不能成功,還以為Spring2有問題呢。 ok了,下面我們就可以寫一個spring的配置文件來試試嘍。 下面提供兩個源碼包,一個是spring-dwr的源碼,一個是hellosturts2,一個struts2+spring2+hibernate3.2的例子,其中有用到spring-dwr的例子。 為了減小容量,我把jar都去掉了,lib目錄下提供了一個列表,你可以自己去找相關(guān)的jar包。 http://www./Files/mstar/HelloStruts2.zip http://www./Files/mstar/spring-dwr.zip FeedBack: # re: 把DWR的配置寫到Spring的配置文件里(Spring2 新特性-自定義schema初體驗)
# re: 把DWR的配置寫到Spring的配置文件里(Spring2 新特性-自定義schema初體驗)
# re: 把DWR的配置寫到Spring的配置文件里(Spring2 新特性-自定義schema初體驗)
# re: 把DWR的配置寫到Spring的配置文件里(Spring2 新特性-自定義schema初體驗)
2006-11-16 22:49 | 學(xué)習(xí)
|
|
|
來自: nbtymm > 《Ajax技術(shù)》