<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    細心!用心!耐心!

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

    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.tkk7.com/Files/mstar/HelloStruts2.zip

    http://www.tkk7.com/Files/mstar/spring-dwr.zip
    posted on 2007-04-16 15:33 張金鵬 閱讀(327) 評論(0)  編輯  收藏 所屬分類: AJAX技術
    主站蜘蛛池模板: 免费精品视频在线| 亚洲粉嫩美白在线| 一本久久免费视频| 亚洲高清免费视频| 高清免费久久午夜精品| 日韩精品电影一区亚洲| 一级毛片a女人刺激视频免费| 亚洲AV无码不卡在线观看下载| 国产精品亚洲天堂| 亚洲AV无码专区日韩| 久久久久久久久久久免费精品 | 九九久久精品国产免费看小说 | 亚洲视频在线观看网址| 91老湿机福利免费体验| 亚洲综合精品香蕉久久网97| 2019中文字幕免费电影在线播放| 亚洲视频国产视频| 女人18毛片a级毛片免费| 成人亚洲国产精品久久| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲精品无码成人| 亚洲国产成人久久综合一区77| 九九综合VA免费看| 中文字幕亚洲综合精品一区| 美女视频黄的全免费视频| 亚洲av午夜国产精品无码中文字| 亚洲第一区精品观看| 在线看片免费人成视频播| 亚洲国产成人手机在线电影bd| 日本一道综合久久aⅴ免费| 国产免费AV片在线观看播放| 日产亚洲一区二区三区| 午夜成人免费视频| 伊人久久国产免费观看视频| 亚洲视频精品在线| 国产成人免费片在线观看| 暖暖免费日本在线中文| 亚洲 日韩经典 中文字幕| 国产亚洲精品线观看动态图| 国产精品1024永久免费视频| 又黄又大的激情视频在线观看免费视频社区在线 |