你要知道Spring是什么,并且要知道Spring已經(jīng)出了2.0的版本了。
你要知道DWR是什么,并且要知道DWR也要出2.0的版本了。
呵呵,開(kāi)個(gè)玩笑……。這年頭在BlogJava上混的,估計(jì)沒(méi)幾個(gè)不知道上面這兩個(gè)項(xiàng)了。
好了,現(xiàn)在言歸正傳。DWR本身就提供對(duì)Spring的支持,可以直接把Spring中的Bean暴露給客戶端瀏覽器的Javascript調(diào)用。在dwr.xml中的寫(xiě)法是這樣的:
????<allow>
????????<create?creator="spring"?javascript="AjaxPortalService">
????????????<param?name="beanName"?value="AjaxPortalService"/>
????????????<include?method="changeWondowState"/>
????????????<include?method="changeWorkbenchState"/>
????????????<include?method="changeWindowOrder"/>
????????</create>
????</allow>
</dwr>
這樣寫(xiě)到也沒(méi)什么,只是把項(xiàng)目分模塊以后,雖有的配置都寫(xiě)到一個(gè)dwr.xml文件里面維護(hù)起來(lái)比較麻煩,尤其再遇到版本管理中的代碼合并。所以曾經(jīng)我擴(kuò)展了DWR,讓它可以從多個(gè)文件讀取配置信息。然后每個(gè)模塊自己一個(gè)配置文件,跟著source一起走。這樣做也有問(wèn)題,就是現(xiàn)在Java應(yīng)用程序的配置文件太多啦!Spring的配置,WebWork的配置,Hibernate的配置,DWR的配置,再加上一些雜七雜八的xml和properties。看著這些配置文件簡(jiǎn)直要瘋掉了。
正在此時(shí),spring2橫空出世,現(xiàn)在你可以把一些配置文件合并到一起了。正好趁此機(jī)會(huì)體驗(yàn)一下Spring2的自定義schema特性。
目標(biāo):
把DWR的配置寫(xiě)成這樣:
<beans?xmlns="http://www.springframework.org/schema/beans"
????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
????xmlns:tx="http://www.springframework.org/schema/tx"
????xmlns:aop="http://www.springframework.org/schema/aop"
????xmlns:dwr="http://www.devside.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.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
???????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
???????????http://www.devside.org/schema/spring/dwr?http://www.devside.org/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>
重點(diǎn)在這里:
????<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>
好了現(xiàn)在動(dòng)手開(kāi)始做。
原理其實(shí)很簡(jiǎn)單,現(xiàn)在看張圖。畫(huà)的不好,敬請(qǐng)?jiān)彛?br />
從這樣圖中我們可以看出我們要做如下工作:
1、做一個(gè)dwr.xsd,定義spring配置文件中的dwr這部分配置的schema。
???要做這個(gè)需要有一定xml和xsd的知識(shí),我也是現(xiàn)學(xué)現(xiàn)用啦!
















































2、我們要做一個(gè)DWRNamespaceHandler來(lái)處理DWR的配置信息,其實(shí)里面就做一件事把AllowBeanDefinitionParser注冊(cè)給allow節(jié)點(diǎn)。因?yàn)槲覀僤wr的配置部分根節(jié)點(diǎn)就一個(gè)標(biāo)簽allow,所以我們就做一個(gè)用于解析allow標(biāo)簽的AllowBeanDefinitionParser解析器就行。如果我們的根節(jié)點(diǎn)還有其他的標(biāo)簽,同樣也要做相應(yīng)的解析器。
????public?void?init()?{
????????//把AllowBeanDefinitionParser注冊(cè)到allow節(jié)點(diǎn)
????????registerBeanDefinitionParser("allow",?new?AllowBeanDefinitionParser());
????}
}
其實(shí)難點(diǎn)是做AllowBeanDefinitionParser,這里你需要一些DOM模型的知識(shí),來(lái)操作配置節(jié)點(diǎn)的內(nèi)容。然后根據(jù)內(nèi)容進(jìn)行處理。在這里我們,需要做三件事:
?? [1]? 把配置節(jié)點(diǎn)的內(nèi)容轉(zhuǎn)換成對(duì)象模型,即AllowBean(其實(shí)就是一些POJO罷了)。
?? [2]? 把這個(gè)AllowBean注冊(cè)給Spring的Context,這一步是可選的。因?yàn)槲覀冎饕前堰@個(gè)Bean給DWR,當(dāng)然順道給Spring一份也沒(méi)什么問(wèn)題。
?? [3] 把這個(gè)AllowBean注冊(cè)到AllowBeanHolder。
3、AllowBeanHolder。
???其實(shí)這就是個(gè)簡(jiǎn)單的singleton類,整個(gè)運(yùn)行期只有一個(gè)實(shí)例。它就像一個(gè)容器,AllowBeanDefinitionParser往里放,DWR的Configuration再?gòu)倪@里取。
4、擴(kuò)展DWR的DefaultConfiguration,我們做一個(gè)SpringConfiguration。DWR的DefaultConfiguration是負(fù)責(zé)讀取配置信息的。我們?cè)谄渲屑尤霃腁llowBeanHolder讀取配置信息的功能即可。
5、擴(kuò)展DWR的DWRServlet,我們做一個(gè)SpringDWRServlet,原來(lái)的DWRServlet加載的是DefaultConfiguration,我們的Serlvet加載我們自己的SpringConfiguration即可。
6、萬(wàn)事俱備,只欠東風(fēng)啦。就是讓Spring知道我們干了這些!
???在META-INF目錄下(如果沒(méi)有在src目錄下創(chuàng)建一個(gè))加入spring.handlers和spring.schemas兩個(gè)文件。
???spring.handlers中的內(nèi)容:
???http\://www.devside.org/schema/spring/dwr=org.devside.core.support.dwr.DWRNamespaceHandler
???spring.schemas中的內(nèi)容:
?? http\://www.devside.org/schema/spring/dwr.xsd=org/devside/core/support/dwr/dwr.xsd
???
?? 注意不要寫(xiě)錯(cuò)字哦,我一開(kāi)始就寫(xiě)錯(cuò)一個(gè)字母,結(jié)果怎么調(diào)試都不能成功,還以為Spring2有問(wèn)題呢。
ok了,下面我們就可以寫(xiě)一個(gè)spring的配置文件來(lái)試試嘍。
下面提供兩個(gè)源碼包,一個(gè)是spring-dwr的源碼,一個(gè)是hellosturts2,一個(gè)struts2+spring2+hibernate3.2的例子,其中有用到spring-dwr的例子。
為了減小容量,我把jar都去掉了,lib目錄下提供了一個(gè)列表,你可以自己去找相關(guān)的jar包。
http://www.tkk7.com/Files/mstar/HelloStruts2.zip
http://www.tkk7.com/Files/mstar/spring-dwr.zip
不好意思說(shuō),用的是D版
很想請(qǐng)教一下Struts2+Spring2的問(wèn)題
因?yàn)橐恢庇玫氖荢truts 1.2.7和 Spring 1.2