使用ExtDirectSpring整合Spring3和ExtJs4

ExtDirectSpring是一個用於ExtJs4直接調用遠程Spring方法的第三方庫。我們不再需要在spring方法中封裝json對象供外界調用,ExtJs4也不再需要手動解析遠程服務器返回過來的Json對象,所有這些操作都由ExtDirectSpring去處理,ExtJs4只需要象調用本地方法一樣去操作遠程資源。

 

ExtDirectSpring主頁地址:

https://github.com/ralscha/extdirectspring

 

以下是一個簡要的工程搭建過程(我們假設你已經創建了一個Spring MVC的工程)

1: 在pom.xml中添加相關依賴

  1. <dependency>  
  2.     <groupId>ch.ralscha</groupId>  
  3.     <artifactId>extdirectspring</artifactId>  
  4.     <version>1.2.0</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>commons-fileupload</groupId>  
  8.     <artifactId>commons-fileupload</artifactId>  
  9.     <version>1.2.2</version>  
  10. </dependency>  
  11. <dependency>  
  12.     <groupId>commons-io</groupId>  
  13.     <artifactId>commons-io</artifactId>  
  14.     <version>2.1</version>  
  15. </dependency> 

2: 將含有@ExtDirectMethod標籤的類所在包添加到Spring組件管理中

注:所有被暴露出來允許遠程訪問的方法都需要添加@ExtDirectMethod註解

<context:component-scan base-package="com.train.extdirectspring,ch.ralscha.extdirectspring" /> 
3: 在Spring配置文件中添加對ExtDirectSpring的全局配置信息
  1. <bean id="extDirectSpringConfiguration" class="ch.ralscha.extdirectspring.controller.Configuration" p:timeout="12000"  
  2.     p:maxRetries="10" p:enableBuffer="false">  
  3.     <property name="exceptionToMessage">  
  4.         <map>  
  5.             <entry key="java.lang.IllegalArgumentException" value="illegal argument" />  
  6.             <entry key="org.springframework.beans.factory.NoSuchBeanDefinitionException">  
  7.                 <null />  
  8.             </entry>  
  9.         </map>  
  10.     </property>  
  11. </bean>  
 到此爲止,服務器端的配置基本完成,下面開始講述頁面前端如何配置調用遠程後端方法。

4: 下載並複製ExtJs4的關聯文件到工程中去

  • 從官網下載並解壓http://www.sencha.com/products/extjs
  • 複製文件夾locale, resources及文件ext-all-debug.js and ext-all.js到WEB工程目錄中    

5: 引入ExtJs4的CSS和JS文件到你的頁面中
  1. <link rel="stylesheet" type="text/css" href="/resources/extjs/resources/css/ext-all.css" />  
  2. <script type="text/javascript" src="/resources/extjs/ext-all-debug.js" ></script>  
 6: 引入ExtDirectSpring提供的api.js/api-debug.js文件到你的頁面中

  1. <
    script type="text/javascript" src="/spring/api-debug.js"></script>  
注:這裏的/spring/前綴取決於你的web.xml中的Spring Dispatcher的配置。並且該api-debug.js是由系統生成而非一個靜態文件。
7: 引入用於當前頁面佈局的js文件
Html代碼  收藏代碼
  1. <script type="text/javascript" src="/SpringMVC/resources/app/welcome.js"></script>  
  注:該文件由用戶自定義
 從下面開始我們就要開始編寫Extjs代碼,我們採用Extjs的MVC框架,編寫的一般過程爲:編寫頁面佈局js(上面的welcome.js) -> 編寫model(MVC) -> 編寫store(這個對象主要用於數據集的展示,如實現分頁排序等功能) -> 編寫視圖層(MVC)  -> 編寫控制層(MVC) -> 編寫SpringMVC方法與ExtJs控制層交互

8: 編寫上面定義的welcome.js
 
  1. Ext.Loader.setConfig({  
  2.     enabled: true  
  3. });  
  4.   
  5. Ext.require('Ext.direct.*'function() {  
  6.     Ext.direct.Manager.addProvider(Ext.app.REMOTING_API);  
  7. });  
  8.   
  9. Ext.application({  
  10.     controllers : [ 'sample'], //與第12個步驟中文件名一致  
  11. //    autoCreateViewport : true,  
  12.     name : 'Mtx'//應用名,可以任取,不重複即可  
  13.     appFolder: 'resources/app'//這裏配置當前應用下你的ExtJs MVC代碼的存放路徑  
  14.           
  15.     launch : function() {  
  16.         Ext.create('Ext.container.Viewport', {  
  17.             items : [ {  
  18.                 xtype : 'sampleList'//參考第11步中的視圖別名  
  19.                 layout : 'fit',  
  20.                 margins: 5  
  21.             } ]  
  22.         });  
  23.     }  
  24. });  
9:  編寫模型Model
按照ExtJs的規範,我們默認將model文件存放在resources/app/model目錄下,其中'resources/app'由第8個步驟中的appFolder配置所決定,model是默認的模型存放文件夾名。我們在這裏創建一個路徑爲resources/app/model/sample.js的用於描述model的文件。我們只簡單定義id和name兩個字段(實際開發中,一般需要根據數據庫中的表結構來定義)
  1. Ext.define('Mtx.model.Sample', {  
  2.     extend : 'Ext.data.Model',//所有定義的model對象都需要繼承該父對象  
  3.   
  4.     fields : [ {  
  5.         name : 'id',  
  6.         type : 'int'  
  7.     }, {  
  8.         name : 'name',  
  9.         type : 'string',  
  10.     } ],  
  11.   
  12.     proxy : {  
  13.         type : 'direct',  
  14.         api : {  
  15.             read : helloController.read, //這裏的四個方法是ExtJs框架默認集成好的,一般我們只需要實現對應的方法就可以輕鬆實現增刪改查操作  
  16.             create : helloController.save,  
  17.             update : helloController.save,  
  18.             destroy : helloController.destroy  
  19.         },  
  20.         reader : {  
  21.             root : 'records'  
  22.         }  
  23.     }  
  24. });  
10: 編寫store(這個對象主要用於數據集的展示,如實現分頁排序等功能)
和model一樣,store的默認存放路徑爲$appFolder+'/store', 這裏對應路徑爲resources/app/store/sample.js
  1. Ext.define('Mtx.store.Skus', {  
  2.     extend: 'Ext.data.Store',  
  3.     model: 'Mtx.model.Sample'//參考第9步中定義的model  
  4.     autoLoad: true,  
  5.     pageSize: 25,   
  6.     remoteSort: true,  
  7.     autoSync: true,  
  8.     sorters: [ {  
  9.         property: 'name',  
  10.         direction: 'ASC'  
  11.     } ]  
  12. });  
 
 11: 編寫視圖層View
與上面一樣,store的默認存放路徑爲$appFolder+'/view', 這裏對應路徑爲resources/app/view/sample.js
  1. Ext.define('Mtx.view.Sample', {  
  2.     extend : 'Ext.grid.Panel',  
  3.     alias : 'widget.sampleList'//別處如果需要引用該View,可以使用別名sampleList  
  4.   
  5.     store : 'sample',//必須與第9步中創建的model文件名一致  
  6.   
  7.     initComponent : function() {  
  8.         var me = this;  
  9.   
  10.         Ext.applyIf(me, {  
  11.             columns : [ {  
  12.                 xtype : 'gridcolumn',   
  13.                 dataIndex : 'id'//列中顯示數據在model中與之對應的變量名  
  14.                 text : 'ID'//表中的列名  
  15.                 flex : 1  
  16.             }, {  
  17.                 xtype : 'gridcolumn',  
  18.                 dataIndex : 'name',  
  19.                 text : 'Name',  
  20.                 flex : 1  
  21.             }],  
  22.   
  23.             dockedItems : [ {  
  24.                 xtype : 'toolbar',  
  25.                 dock : 'top',  
  26.                 items : [ {  
  27.                     fieldLabel : 'Filter'//增加過濾功能,根據輸入的字符與name字段進行模糊匹配  
  28.                     labelWidth : 40,  
  29.                     xtype : 'textfield',  
  30.                     itemId : 'filtertextfield'  
  31.                 }, '->', {  
  32.                     xtype : 'pagingtoolbar'//加上分頁功能  
  33.                     store : me.getStore(),  
  34.                     displayInfo : true  
  35.                 } ]  
  36.             }, ]  
  37.         });  
  38.   
  39.         me.callParent(arguments);  
  40.     }  
  41.   
  42. });  
12: 編寫控制層Controller
還是和上面一樣,store的默認存放路徑爲$appFolder+'/controller', 這裏對應路徑爲resources/app/controller/sample.js。控制層主要用於爲視圖中的組件綁定不同的事件。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章