細(xì)心!用心!耐心!

          吾非文人,乃市井一俗人也,讀百卷書,跨江河千里,故申城一游; 一兩滴辛酸,三四年學(xué)業(yè),五六點(diǎn)粗墨,七八筆買賣,九十道人情。

          BlogJava 聯(lián)系 聚合 管理
            1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks
          要讀懂我這篇文章估計(jì)需要一些前期知識(shí):
          你要知道Spring是什么,并且要知道Spring已經(jīng)出了2.0的版本了。
          你要知道DWR是什么,并且要知道DWR也要出2.0的版本了。

          呵呵,開個(gè)玩笑……。這年頭在BlogJava上混的,估計(jì)沒幾個(gè)不知道上面這兩個(gè)項(xiàng)了。

          好了,現(xiàn)在言歸正傳。DWR本身就提供對(duì)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>

          這樣寫到也沒什么,只是把項(xiàng)目分模塊以后,雖有的配置都寫到一個(gè)dwr.xml文件里面維護(hù)起來(lái)比較麻煩,尤其再遇到版本管理中的代碼合并。所以曾經(jīng)我擴(kuò)展了DWR,讓它可以從多個(gè)文件讀取配置信息。然后每個(gè)模塊自己一個(gè)配置文件,跟著source一起走。這樣做也有問題,就是現(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的配置寫成這樣:
          <?xml version="1.0" encoding="GBK"?>
          <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)手開始做。

          原理其實(shí)很簡(jiǎn)單,現(xiàn)在看張圖。畫的不好,敬請(qǐng)?jiān)彛?br>


          spring-dwr.png

          從這樣圖中我們可以看出我們要做如下工作:
          1、做一個(gè)dwr.xsd,定義spring配置文件中的dwr這部分配置的schema。
             要做這個(gè)需要有一定xml和xsd的知識(shí),我也是現(xiàn)學(xué)現(xiàn)用啦!

          <?xml version="1.0" encoding="UTF-8"?>
          <xsd:schema xmlns="http://www.devside.org/schema/spring/dwr"
              xmlns:xsd
          ="http://www.w3.org/2001/XMLSchema"
              xmlns:beans
          ="http://www.springframework.org/schema/beans"
              targetNamespace
          ="http://www.devside.org/schema/spring/dwr" 
              elementFormDefault
          ="qualified"    
              attributeFormDefault
          ="unqualified">

              
          <xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/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、我們要做一個(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 class DWRNamespaceHandler extends NamespaceHandlerSupport {

              
          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一份也沒什么問題。
             [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目錄下(如果沒有在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
             
            
          注意不要寫錯(cuò)字哦,我一開始就寫錯(cuò)一個(gè)字母,結(jié)果怎么調(diào)試都不能成功,還以為Spring2有問題呢。

          ok了,下面我們就可以寫一個(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.aygfsteel.com/Files/mstar/HelloStruts2.zip

          http://www.aygfsteel.com/Files/mstar/spring-dwr.zip
          posted on 2007-04-16 15:33 張金鵬 閱讀(325) 評(píng)論(0)  編輯  收藏 所屬分類: AJAX技術(shù)
          主站蜘蛛池模板: 巴塘县| 三穗县| 桐梓县| 陆河县| 孙吴县| 启东市| 合水县| 临沭县| 页游| 叙永县| 安庆市| 临夏县| 昌乐县| 昆明市| 嘉义县| 永年县| 万载县| 枣庄市| 广州市| 涪陵区| 丰都县| 遵义县| 丘北县| 宜阳县| 长寿区| 平湖市| 科尔| 阿坝县| 邹平县| 内丘县| 武冈市| 化德县| 金川县| 泰顺县| 格尔木市| 大新县| 兴化市| 湘潭市| 文山县| 平凉市| 石城县|