細心!用心!耐心!

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

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

          呵呵,開個玩笑……。這年頭在BlogJava上混的,估計沒幾個不知道上面這兩個項了。

          好了,現在言歸正傳。DWR本身就提供對Spring的支持,可以直接把Spring中的Bean暴露給客戶端瀏覽器的Javascript調用。在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文件里面維護起來比較麻煩,尤其再遇到版本管理中的代碼合并。所以曾經我擴展了DWR,讓它可以從多個文件讀取配置信息。然后每個模塊自己一個配置文件,跟著source一起走。這樣做也有問題,就是現在Java應用程序的配置文件太多啦!Spring的配置,WebWork的配置,Hibernate的配置,DWR的配置,再加上一些雜七雜八的xml和properties。看著這些配置文件簡直要瘋掉了。

          正在此時,spring2橫空出世,現在你可以把一些配置文件合并到一起了。正好趁此機會體驗一下Spring2的自定義schema特性。

          目標:
          把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>


          重點在這里:
              <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>

          好了現在動手開始做。

          原理其實很簡單,現在看張圖。畫的不好,敬請原諒!



          spring-dwr.png

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

          <?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、我們要做一個DWRNamespaceHandler來處理DWR的配置信息,其實里面就做一件事把AllowBeanDefinitionParser注冊給allow節點。因為我們dwr的配置部分根節點就一個標簽allow,所以我們就做一個用于解析allow標簽的AllowBeanDefinitionParser解析器就行。如果我們的根節點還有其他的標簽,同樣也要做相應的解析器。

          public class DWRNamespaceHandler extends NamespaceHandlerSupport {

              
          public void init() {
                  
          //把AllowBeanDefinitionParser注冊到allow節點
                  registerBeanDefinitionParser("allow"new AllowBeanDefinitionParser());
              }

          }

          其實難點是做AllowBeanDefinitionParser,這里你需要一些DOM模型的知識,來操作配置節點的內容。然后根據內容進行處理。在這里我們,需要做三件事:
             [1]  把配置節點的內容轉換成對象模型,即AllowBean(其實就是一些POJO罷了)。
             [2]  把這個AllowBean注冊給Spring的Context,這一步是可選的。因為我們主要是把這個Bean給DWR,當然順道給Spring一份也沒什么問題。
             [3] 把這個AllowBean注冊到AllowBeanHolder。

          3、AllowBeanHolder。
             其實這就是個簡單的singleton類,整個運行期只有一個實例。它就像一個容器,AllowBeanDefinitionParser往里放,DWR的Configuration再從這里取。

          4、擴展DWR的DefaultConfiguration,我們做一個SpringConfiguration。DWR的DefaultConfiguration是負責讀取配置信息的。我們在其中加入從AllowBeanHolder讀取配置信息的功能即可。

          5、擴展DWR的DWRServlet,我們做一個SpringDWRServlet,原來的DWRServlet加載的是DefaultConfiguration,我們的Serlvet加載我們自己的SpringConfiguration即可。

          6、萬事俱備,只欠東風啦。就是讓Spring知道我們干了這些!
             在META-INF目錄下(如果沒有在src目錄下創建一個)加入spring.handlers和spring.schemas兩個文件。
             spring.handlers中的內容:
             http\://www.devside.org/schema/spring/dwr=org.devside.core.support.dwr.DWRNamespaceHandler
             spring.schemas中的內容:
             http\://www.devside.org/schema/spring/dwr.xsd=org/devside/core/support/dwr/dwr.xsd
             
            
          注意不要寫錯字哦,我一開始就寫錯一個字母,結果怎么調試都不能成功,還以為Spring2有問題呢。

          ok了,下面我們就可以寫一個spring的配置文件來試試嘍。

          下面提供兩個源碼包,一個是spring-dwr的源碼,一個是hellosturts2,一個struts2+spring2+hibernate3.2的例子,其中有用到spring-dwr的例子。

          為了減小容量,我把jar都去掉了,lib目錄下提供了一個列表,你可以自己去找相關的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 張金鵬 閱讀(323) 評論(0)  編輯  收藏 所屬分類: AJAX技術
          主站蜘蛛池模板: 西充县| 陇西县| 长沙县| 芦山县| 门源| 海伦市| 常熟市| 广饶县| 鹿泉市| 海淀区| 罗平县| 巴中市| 丹阳市| 巩留县| 封开县| 克山县| 资源县| 临猗县| 垫江县| 阜南县| 富宁县| 台安县| 聊城市| 梓潼县| 类乌齐县| 滁州市| 石棉县| 林芝县| 舟曲县| 财经| 乌拉特中旗| 确山县| 茌平县| 五莲县| 莱州市| 抚远县| 东乌| 长泰县| 沁阳市| 杭锦旗| 专栏|