Spring3+Hibernate4+SpringMVC整合Ext:JSON數據格式傳輸


前言


前兩篇文章介紹了Spring3、Hibernate4、SpringMVC的整合以及Ext界面MVC的實現,這次介紹的內容是處理Ext的分頁和前後臺的數據傳輸。
目前Ext大部分組件使用的都是JSON數據格式(如果有不知道JSON數據格式的同學可以去網上找點資料看下,在這裏就不贅述了)。包括表格控件、樹控件、報表控件以及表單空間等等,所以說對於整合Ext來說如何向前臺空間傳輸JSON數據很關鍵。
Ext框架中表格控件用的的最多的,也是用的最廣泛的。表格控件也是非常強大的,可擴展性也非常強。使用表格自然就涉及到了分頁的問題,如何將代碼降低到最小、更方便、更快捷的實現分頁也自然是非常重要的。說了這麼多下面就開始介紹下如何使用SpingMVC進行數據傳輸!

前臺界面


下面大家看到這一張圖片,這張圖片的內容是SpringMVC的url映射詳細情況,url的映射是在後臺獲取的,通過對url映射數據的組織發送到前臺讓表格空間展示出來:
下面是這個界面的具體實現,首先看到前臺界面的代碼:
Ext.define('Fes.system.spring.URLMapping', {
			extend : 'Ext.grid.Panel',
			alias : 'widget.rolelist',
			iconCls : 'icon-grid-list',
			rowLines : true,
			columnLines : true,
			viewConfig : {
				loadingText : '正在加載角色列表'
			},
			columns : [{
					xtype : 'rownumberer'
				}, {
					text : '映射',
					columns : [{text : '路徑',width : 200,sortable : true,dataIndex : 'url'},
					{text : '需求',width : 100,sortable : true,dataIndex : 'consumes'}, 
					{text : '自定義',width : 100,sortable : true,dataIndex : 'custom'},
					{text : '頭信息',width : 100,sortable : true,dataIndex : 'headers'}, 
					{text : '參數值',width : 100,sortable : true,dataIndex : 'params'}, 
					{text : '請求方法',width : 100,sortable : true,dataIndex : 'methods'},
					{text : '處理',width : 100,sortable : true,dataIndex : 'produces'}]
				}, 
				{text : '方法',width : 200,sortable : true,dataIndex : 'methodName'},
				{text : '返回值',width : 350,sortable : true,dataIndex : 'returnType'},
				{text : '註解',width : 300,sortable : true,dataIndex : 'annotationName'},
				{text : '參數',width : 300,sortable : true,dataIndex : 'parameters'},
				{text : '類',width : 100,sortable : true,dataIndex : 'className',width : 500}
			],
			initComponent : function() {
				var groupingFeature = Ext.create('Ext.grid.feature.Grouping',{
				        groupHeaderTpl: '{name}({rows.length})',
				        hideGroupedHeader: true,
				        groupByText:'對該列進行分組',
				        showGroupsText : '是否分組'
				    });
				this.features = [groupingFeature];
				this.createStore();
				this.callParent();
			},

			createStore : function() {
				var me = this;
				Ext.define('Fes.system.spring.URLMappingModel', {
							extend : 'Ext.data.Model',
							fields : [{name : 'url',type : 'string'}, 
									  {name : 'className',type : 'string'},
									  {name : 'methodName'}, 
									  {name : 'returnType'},
									  {name : 'annotationName'}, 
									  {name : 'consumes'},
									  {name : 'custom'},
									  {name : 'headers'}, 
									  {name : 'params'},
									  {name : 'methods'}, 
									  {name : 'produces'},
									  {name : 'parameters'}
									 ]
						});
				me.store = Ext.create('Ext.data.Store', {
							model : 'Fes.system.spring.URLMappingModel',
							groupField: 'className',
							autoLoad : true,
							proxy : {
								type : 'ajax',
								url : 'spring/url-mapping',
								reader : {
									type : 'json',
									root : 'root'
								}
							}
						});
			}

		});

這個就是SpringMVC URL映射界面的代碼實現,這段代碼很簡單就是定義了一個表格,並對數據做了簡單的排序。給表格配置了一個數據源,訪問的路徑是"spring/url-mapping"。

後臺實現JSON數據格式傳輸


接下來我們就可以看看後臺Java代碼的實現:
package com.avicit.fes.system.spring.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.avicit.fes.system.spring.vo.URLMapping;
import com.avicit.framework.context.spring.SpringContextBeanFactory;
import com.avicit.framework.util.ResponseUtils;

/***
 * Spring相關控制器
 * */
@Controller
@RequestMapping("spring")
public class SpringController {

	/**
	 * 獲取Spring映射
	 * **/
	@RequestMapping("url-mapping")
	public @ResponseBody Object getURLMapping() {
		List<URLMapping> list = new ArrayList<URLMapping>();
		Map<RequestMappingInfo, HandlerMethod> map2 = SpringContextBeanFactory
				.getBean(RequestMappingHandlerMapping.class)
				.getHandlerMethods();
		for (Iterator<RequestMappingInfo> iterator = map2.keySet().iterator(); iterator
				.hasNext();) {
			RequestMappingInfo info = iterator.next();
			URLMapping m = new URLMapping();
			m.setConsumes(String.valueOf(info.getConsumesCondition()));
			m.setCustom(String.valueOf(info.getCustomCondition()));
			m.setHeaders(String.valueOf(info.getHeadersCondition()));
			m.setMethods(String.valueOf(info.getMethodsCondition()));
			m.setParams(String.valueOf(info.getParamsCondition()));
			m.setProduces(String.valueOf(info.getProducesCondition()));
			m.setUrl(info.getPatternsCondition().toString());
			HandlerMethod method = map2.get(info);
			m.setMethodName(method.getMethod().getName());
			m.setClassName(method.getBeanType().getName());
			m.setReturnType(method.getReturnType().getParameterType()
					.toString());
			MethodParameter[] parameters = method.getMethodParameters();
			List<String> list2 = new ArrayList<String>();
			for (MethodParameter methodParameter : parameters) {
				list2.add(methodParameter.getParameterType().getName());
			}
			m.setParameters(String.valueOf(list2));
			ResponseBody annotationClass = method.getMethodAnnotation(ResponseBody.class);
			if(annotationClass != null){
				m.setAnnotationName(annotationClass.toString());
			}
			list.add(m);
		}
		return ResponseUtils.sendList(list);
	}
}
這個就是SpringMVC Controller層的代碼,首先SpringMVC攔截了spring/url-mapping的訪問交給了getURLMapping方法進行處理,這個方法和一般的方法的不一樣的方法在於多了一個註解:@ResponseBody,這個註解的作用是將該方法的返回者轉換成JSON的數據格式。下面我們可以看看ResponseUtils.sendList這個方法返回的什麼東東:
	public static <T> Map<String, Object> sendList(List<T> T) {
		Map<String, Object> map = getInstanceMap();
		map.put("root", T);
		map.put("success", true);
		return map;
	}
這個方法返回的是一個Map對象,Map中包含了兩個值,一個是root : List<T>的列表數據,還有一個是success:true的狀態標記。Map對象和JSON數據格式有極大的相似性,所以將Map對象轉換成數據格式應該是最多的方法。@ResponseBody不但會把Map對象轉換成JSON數據格式,也會把對象轉換成JSON數據格式。通過斷點調試的觀察到這個Map的屬性:
{root=[com.avicit.fes.system.spring.vo.URLMapping@49b700, com.avicit.fes.system.spring.vo.URLMapping@18d9055, com.avicit.fes.system.spring.vo.URLMapping@fef39d, com.avicit.fes.system.spring.vo.URLMapping@2bd643, com.avicit.fes.system.spring.vo.URLMapping@1fff5ee, com.avicit.fes.system.spring.vo.URLMapping@16aefbf, com.avicit.fes.system.spring.vo.URLMapping@1a2093a, com.avicit.fes.system.spring.vo.URLMapping@10bde72, com.avicit.fes.system.spring.vo.URLMapping@393c0a, com.avicit.fes.system.spring.vo.URLMapping@194c1f9, com.avicit.fes.system.spring.vo.URLMapping@14ad4ae, com.avicit.fes.system.spring.vo.URLMapping@1d12abe, com.avicit.fes.system.spring.vo.URLMapping@14d5845, com.avicit.fes.system.spring.vo.URLMapping@de3a7a, com.avicit.fes.system.spring.vo.URLMapping@1d15873, com.avicit.fes.system.spring.vo.URLMapping@10606c0, com.avicit.fes.system.spring.vo.URLMapping@a54cb4, com.avicit.fes.system.spring.vo.URLMapping@4ed34b, com.avicit.fes.system.spring.vo.URLMapping@1120709, com.avicit.fes.system.spring.vo.URLMapping@8c2005, com.avicit.fes.system.spring.vo.URLMapping@18a3e15, com.avicit.fes.system.spring.vo.URLMapping@f20092], success=true}
通過前臺可以看到:

從這張圖片上可以看到轉行後的JSON數據格式。Spring提供支持@ResponseBody註解的,這個是對象轉換成JSON數據的,當然也提供將頁面上傳過來的數據轉換成對象的註解@RequestBody。@RequestBody和Ext提供的RESTful方法完成CURD。個人覺得SringMVC對JSON數據格式的支持比Struts2更靈活更方便,不很繁瑣的配置。並且能夠處理輸出、輸入兩個方向。開發起來應該會更方便。

Ext還能做什麼?只有想不到的,沒有做不到了,進入http://blog.csdn.net/leecho571/article/details/8207102感受Ext帶來的新的體驗

個人對SpringMVC的學習見解,

實例下載


PS:資源分是5分,不是要大家的分,只是希望大家評論一下,給點意見!

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