Spring3+Hibernate4+SpringMVC整合Ext:JSON数据格式传输

本文主要是介绍Spring3+Hibernate4+SpringMVC整合Ext:JSON数据格式传输,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前台界面


下面大家看到这一张图片,这张图片的内容是SpringMVC的url映射详细情况,url的映射是在后台获取的,通过对url映射数据的组织发送到前台让表格空间展示出来:
下面是这个界面的具体实现,首先看到前台界面的代码:
[javascript]  view plain copy
  1. Ext.define('Fes.system.spring.URLMapping', {  
  2.             extend : 'Ext.grid.Panel',  
  3.             alias : 'widget.rolelist',  
  4.             iconCls : 'icon-grid-list',  
  5.             rowLines : true,  
  6.             columnLines : true,  
  7.             viewConfig : {  
  8.                 loadingText : '正在加载角色列表'  
  9.             },  
  10.             columns : [{  
  11.                     xtype : 'rownumberer'  
  12.                 }, {  
  13.                     text : '映射',  
  14.                     columns : [{text : '路径',width : 200,sortable : true,dataIndex : 'url'},  
  15.                     {text : '需求',width : 100,sortable : true,dataIndex : 'consumes'},   
  16.                     {text : '自定义',width : 100,sortable : true,dataIndex : 'custom'},  
  17.                     {text : '头信息',width : 100,sortable : true,dataIndex : 'headers'},   
  18.                     {text : '参数值',width : 100,sortable : true,dataIndex : 'params'},   
  19.                     {text : '请求方法',width : 100,sortable : true,dataIndex : 'methods'},  
  20.                     {text : '处理',width : 100,sortable : true,dataIndex : 'produces'}]  
  21.                 },   
  22.                 {text : '方法',width : 200,sortable : true,dataIndex : 'methodName'},  
  23.                 {text : '返回值',width : 350,sortable : true,dataIndex : 'returnType'},  
  24.                 {text : '注解',width : 300,sortable : true,dataIndex : 'annotationName'},  
  25.                 {text : '参数',width : 300,sortable : true,dataIndex : 'parameters'},  
  26.                 {text : '类',width : 100,sortable : true,dataIndex : 'className',width : 500}  
  27.             ],  
  28.             initComponent : function() {  
  29.                 var groupingFeature = Ext.create('Ext.grid.feature.Grouping',{  
  30.                         groupHeaderTpl: '{name}({rows.length})',  
  31.                         hideGroupedHeader: true,  
  32.                         groupByText:'对该列进行分组',  
  33.                         showGroupsText : '是否分组'  
  34.                     });  
  35.                 this.features = [groupingFeature];  
  36.                 this.createStore();  
  37.                 this.callParent();  
  38.             },  
  39.   
  40.             createStore : function() {  
  41.                 var me = this;  
  42.                 Ext.define('Fes.system.spring.URLMappingModel', {  
  43.                             extend : 'Ext.data.Model',  
  44.                             fields : [{name : 'url',type : 'string'},   
  45.                                       {name : 'className',type : 'string'},  
  46.                                       {name : 'methodName'},   
  47.                                       {name : 'returnType'},  
  48.                                       {name : 'annotationName'},   
  49.                                       {name : 'consumes'},  
  50.                                       {name : 'custom'},  
  51.                                       {name : 'headers'},   
  52.                                       {name : 'params'},  
  53.                                       {name : 'methods'},   
  54.                                       {name : 'produces'},  
  55.                                       {name : 'parameters'}  
  56.                                      ]  
  57.                         });  
  58.                 me.store = Ext.create('Ext.data.Store', {  
  59.                             model : 'Fes.system.spring.URLMappingModel',  
  60.                             groupField: 'className',  
  61.                             autoLoad : true,  
  62.                             proxy : {  
  63.                                 type : 'ajax',  
  64.                                 url : 'spring/url-mapping',  
  65.                                 reader : {  
  66.                                     type : 'json',  
  67.                                     root : 'root'  
  68.                                 }  
  69.                             }  
  70.                         });  
  71.             }  
  72.   
  73.         });  

这个就是SpringMVC URL映射界面的代码实现,这段代码很简单就是定义了一个表格,并对数据做了简单的排序。给表格配置了一个数据源,访问的路径是"spring/url-mapping"。

后台实现JSON数据格式传输


接下来我们就可以看看后台Java代码的实现:
[java]  view plain copy
  1. package com.avicit.fes.system.spring.controller;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.springframework.core.MethodParameter;  
  9. import org.springframework.stereotype.Controller;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.ResponseBody;  
  12. import org.springframework.web.method.HandlerMethod;  
  13. import org.springframework.web.servlet.mvc.method.RequestMappingInfo;  
  14. import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;  
  15.   
  16. import com.avicit.fes.system.spring.vo.URLMapping;  
  17. import com.avicit.framework.context.spring.SpringContextBeanFactory;  
  18. import com.avicit.framework.util.ResponseUtils;  
  19.   
  20. /*** 
  21.  * Spring相关控制器 
  22.  * */  
  23. @Controller  
  24. @RequestMapping("spring")  
  25. public class SpringController {  
  26.   
  27.     /** 
  28.      * 获取Spring映射 
  29.      * **/  
  30.     @RequestMapping("url-mapping")  
  31.     public @ResponseBody Object getURLMapping() {  
  32.         List<URLMapping> list = new ArrayList<URLMapping>();  
  33.         Map<RequestMappingInfo, HandlerMethod> map2 = SpringContextBeanFactory  
  34.                 .getBean(RequestMappingHandlerMapping.class)  
  35.                 .getHandlerMethods();  
  36.         for (Iterator<RequestMappingInfo> iterator = map2.keySet().iterator(); iterator  
  37.                 .hasNext();) {  
  38.             RequestMappingInfo info = iterator.next();  
  39.             URLMapping m = new URLMapping();  
  40.             m.setConsumes(String.valueOf(info.getConsumesCondition()));  
  41.             m.setCustom(String.valueOf(info.getCustomCondition()));  
  42.             m.setHeaders(String.valueOf(info.getHeadersCondition()));  
  43.             m.setMethods(String.valueOf(info.getMethodsCondition()));  
  44.             m.setParams(String.valueOf(info.getParamsCondition()));  
  45.             m.setProduces(String.valueOf(info.getProducesCondition()));  
  46.             m.setUrl(info.getPatternsCondition().toString());  
  47.             HandlerMethod method = map2.get(info);  
  48.             m.setMethodName(method.getMethod().getName());  
  49.             m.setClassName(method.getBeanType().getName());  
  50.             m.setReturnType(method.getReturnType().getParameterType()  
  51.                     .toString());  
  52.             MethodParameter[] parameters = method.getMethodParameters();  
  53.             List<String> list2 = new ArrayList<String>();  
  54.             for (MethodParameter methodParameter : parameters) {  
  55.                 list2.add(methodParameter.getParameterType().getName());  
  56.             }  
  57.             m.setParameters(String.valueOf(list2));  
  58.             ResponseBody annotationClass = method.getMethodAnnotation(ResponseBody.class);  
  59.             if(annotationClass != null){  
  60.                 m.setAnnotationName(annotationClass.toString());  
  61.             }  
  62.             list.add(m);  
  63.         }  
  64.         return ResponseUtils.sendList(list);  
  65.     }  
  66. }  
这个就是SpringMVC Controller层的代码,首先SpringMVC拦截了spring/url-mapping的访问交给了getURLMapping方法进行处理,这个方法和一般的方法的不一样的方法在于多了一个注解:@ResponseBody,这个注解的作用是将该方法的返回者转换成JSON的数据格式。下面我们可以看看ResponseUtils.sendList这个方法返回的什么东东:
[java]  view plain copy
  1. public static <T> Map<String, Object> sendList(List<T> T) {  
  2.     Map<String, Object> map = getInstanceMap();  
  3.     map.put("root", T);  
  4.     map.put("success"true);  
  5.     return map;  
  6. }  
这个方法返回的是一个Map对象,Map中包含了两个值,一个是root : List<T>的列表数据,还有一个是success:true的状态标记。Map对象和JSON数据格式有极大的相似性,所以将Map对象转换成数据格式应该是最多的方法。@ResponseBody不但会把Map对象转换成JSON数据格式,也会把对象转换成JSON数据格式。通过断点调试的观察到这个Map的属性:
[java]  view plain copy
  1. {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数据格式传输的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/334454

相关文章

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain