本文主要是介绍Spring3+Hibernate4+SpringMVC整合Ext:JSON数据格式传输,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前台界面
下面大家看到这一张图片,这张图片的内容是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'
- }
- }
- });
- }
- });
后台实现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);
- }
- }
- 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;
- }
- {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更灵活更方便,不很繁琐的配置。并且能够处理输出、输入两个方向。开发起来应该会更方便。
这篇关于Spring3+Hibernate4+SpringMVC整合Ext:JSON数据格式传输的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!