两个方向:

一、前台至后台:

  Spring可以自动封装Bean,也就是说可以前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等)。如果传递过来的是特殊对象,则需要手动进行封装。

  Spring提供了@initBinder(初始化绑定封装)注解和WebDataBinder工具。用户只需要向WebDataBinder注册自己需要的类型的属性编辑器即可。

/*
前台传递过来的String类型时间,通过下面的初始化绑定,转换成Date类型
*/
@initBinder
public void initBinder(WebDataBinder binder){
SimpleDateFormate sdf=new SimpleDateFormate("yyyy-MM-dd HH:mm");
binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));//true表示允许空值,false不允许
}

  更多详细内容转载链接

  1,http://blog.csdn.net/hongxingxiaonan/article/details/50282001(前台传递过来非Date类型,及WebDataBinder的其它方法)

  2,http://www.cnblogs.com/AloneSword/p/3998943.html(前台传递过来两个不同名和不同格式的Date类型)

二、后台到前台:

  1,可以直接在后台将值存入session或request或page中,后台就可以直接通过EL表达式¥{key.value}取值

  2,主要记录后台将List<javaBean>值存入json中,前台如何取值及转换。

  我写的项目前台运用的是easyui-datagrid显示值

  (1),前台封装的值不包含特殊类型,如java.util.Date

  easyui前台显示数据可以使用JSONObject,也可以使用JSONArray。但是如果需要在datagrid表格中进行数据显示,只能使用JSONObject,这是easyui的规范。

  一般后台会将查询出的List使用JSONArray.fromObject()方法将List转换成JSONArray,但如果是在datagrid的表格中显示,则需要将JSONArray  put到JSONobject中;如果不用在datagrid表格中显示,那么JSONObject和JSONArray都可以传递给前台值。

  如combobox,后台只需要将List放入JSONArray中即可传向前台。

  先给个工具类,用于前台显示:

  

 public class ResponseUtil {
public static void write(HttpServletResponse response, Object o) throws Exception {
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println(o.toString());
writer.flush();
writer.close();
}
}

ResponseUtil.java

  Controller层方法,PageBean也是自己创建的实体类,是用来给sql语句的limit提供数据而已:

  

 public class PageBean {
private int page;// 页数
private int rows;// 每页行数
private int start;// 起始页 public PageBean(int page, int rows) {
super();
this.page = page;
this.rows = rows;
} public int getPage() {
return page;
} public void setPage(int page) {
this.page = page;
} public int getRows() {
return rows;
} public void setRows(int rows) {
this.rows = rows;
} public int getStart() {
start = (page - 1) * rows;
return start;
} }

PageBean.java

@RequestMapping("/getProductList")
public void getProductList(@RequestParam(value = "page", required = false) String page,@RequestParam(value = "rows") String rows, HttpServletResponse response, Product product) throws Exception {
  PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
  Map<String, Object> map = new HashMap<>();
  map.put("start", pageBean.getStart());
  map.put("size", pageBean.getRows());
  map.put("productName", product.getProductName());
  List<Product> productList = productService.getProductList(map);
  int total = productService.getTotal(map);
  JSONObject jsonObject = new JSONObject();
  JSONArray jsonArray = JSONArray.fromObject(productList);
//将查询出的List先转换成JSONArray
  jsonObject.put("rows", jsonArray);
  jsonObject.put("total"
, total);
  ResponseUtil.write(response, jsonObject);
}

  (2),前台封装的值包含如java.util.Date的特殊类型

  用Json-lib日期处理:将后台的传递的时间类型转换成前台可以显示的Json格式,用类JsonConfig和接口JsonValueProcessor处理,用法也很简单:

  源码JsonValueProcessor接口中只包含两个方法,我定义一个类JavaDateObjectToJsonUtil继承该接口,定义新类是为了可以在类中添加我需要的时间格式

  

 public class JavaDateObjectToJsonUtil implements JsonValueProcessor {

     private String format = null;

     public JavaDateObjectToJsonUtil(String format) {
super();
this.format = format;
} @Override
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return null;
} @Override
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
if (value == null) {
return "";
}
if (value instanceof Date) {
return new SimpleDateFormat(format).format((Date) value);
}
return value.toString();
} }

JavaDataObjectToJsonUtil.java

  Controller层方法,实体类SaleChance类中包含有属性java.util.Date createTime;并且有createTime的get/set方法:

@RequestMapping("/getSaleChanceList")
public void getSaleChanceList(@RequestParam(value = "page", required = false) String page,@RequestParam(value = "rows") String rows, HttpServletResponse response, SaleChance saleChance)throws Exception {
  PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
  Map<String, Object> map = new HashMap<>();
  map.put("start", pageBean.getStart());
  map.put("size", pageBean.getRows());
  map.put("createMan", saleChance.getCreateMan());
  List<SaleChance> saleChanceList = saleChanceService.getSaleChanceList(map);
  int total = saleChanceService.getTotal(map);
  JSONObject jsonObject = new JSONObject();
  JsonConfig jsonConfig = new JsonConfig();
  jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JavaDateObjectToJsonUtil("yyyy:MM:dd hh:mm"));
//只需将时间类型和新类通过JsonConfig注册即可
  JSONArray jsonArray = JSONArray.fromObject(saleChanceList, jsonConfig);
//转换成JSONArray时多带上刚注册的JsonConfig
  jsonObject.put("rows", jsonArray);
  jsonObject.put("total", total);
  ResponseUtil.write(response, jsonObject);
}

  

  

SpringMVC前后台数据传递中Json格式的相互转换(前台显示格式、Json-lib日期处理)及Spring中的WebDataBinder浅析的更多相关文章

  1. Json部分知识(前台显示格式、Json-lib日期处理)

    1,Json格式用于datagrid数据显示 easyui前台显示数据可以使用JSONObject,也可以使用JSONArray.但是如果需要在datagrid表格中进行数据显示,只能使用JSONOb ...

  2. C#曲线分析平台的制作(一,ajax+json前后台数据传递)

    在最近的项目学习中,需要建立一个实时数据的曲线分析平台,这其中的关键在于前后台数据传递过程的学习,经过一天的前辈资料整理,大概有了一定的思路,现总结如下: 1.利用jquery下ajax函数实现: & ...

  3. JavaScript中,JSON格式的字符串与JSON格式的对象相互转化

    前言:JSON是一个独立于任何语言的数据格式,因此,严格来说,没有“JSON对象”和“JSON字符串”这个说法(然而”菜鸟教程“和”W3school“使用了“JSON对象”和“JSON字符串”这个说法 ...

  4. json格式的字符串转为json对象遇到特殊字符问题解决

    中午做后台发过来的json的时候转为对象,可是有几条数据一直出不来,检查发现json里包含了换行符,造成这种情况的原因可能是编辑部门在编辑的时候打的回车造成的 假设有这样一段json格式的字符串 va ...

  5. Python mysql表数据和json格式的相互转换

    功能: 1.Python 脚本将mysql表数据转换成json格式 2.Python 脚本将json数据转成SQL插入数据库 表数据: SQL查询:SELECT id,NAME,LOCAL,mobil ...

  6. SpringMVC之数据传递一

    之前的博客中也说了,mvc中数据传递是最主要的一部分,从url到Controller.从view到Controller.Controller到view以及Controller之间的数据传递.今天主要学 ...

  7. WP8解析JSON格式(使用Newtonsoft.Json包)

    DOTA2 WebAPI请求返回的格式有两种,一种是XML,一种是JSON,默认是返回JSON格式. 这里举一个简单的解析JSON格式的例子(更多JSON操作): { "response&q ...

  8. angularJS入门小Demo2 【包含不用数据库而用data.json格式响应前台的ajax请求方式测试】

    事件绑定: <html> <head> <title>angularJS入门小demo-5 事件指令</title> <script src=&q ...

  9. 关于new Function使用以及将json格式字符串转化为json对象方法介绍

    一直对Function()一知半解,今日就Function()的使用做一下总结 一.函数实际是功能完整的对象,用Fucntion()直接创建函数. 语法规则:    var 函数名 = new Fun ...

随机推荐

  1. eclipse如何通过git把项目上传到码云上

    转载:原文链接:https://www.cnblogs.com/yixtx/p/8310311.html 1.eclipse安装git插件 具体我也做过,因为我下载的eclipse版本以及由git插件 ...

  2. (function($){...})(jQuery)与$(function(){...})区别

    $(function(){...}) 这种写法和jQuery(function(){...}),$(document).ready(function(){...})作用一样,表示文档载入后需要运行的代 ...

  3. BZOJ3998 TJOI2015 弦论 【后缀自动机】【贪心】

    Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个. ...

  4. Codeforces 25E Test 【Hash】

    Codeforces 25E Test E. Test Sometimes it is hard to prepare tests for programming problems. Now Bob ...

  5. BW建模开发入门

    本文档主要指导具体操作步骤,一些技术名称和描述可能在各步骤中不对应,可以忽略 一.模型建立 1.建立信息区和信息对象目录 1)进入BW工作台 2)创建信息区 输入技术名称和描述 3)创建特性和关键值的 ...

  6. Numpy, Pandas, Matplotlib, Scipy 初步

    Numpy: 计算基础,  以类似于matlab的矩阵计算为基础.  底层以C实现, 速度快. Pandas: 以numpy为基础, 扩充了很多统计工具. 重点是数据统计分析. Matplotlib: ...

  7. 安装Zookeeper(单机版)

    一.解压和重命名 tar -zxvf zookeeper-3.4.8.tar.gz -C /usr/ cd /usr mv zookeeper-3.4.8 zookeeper 二.设置配置文件 cd ...

  8. LNMP.ORG 安装LNMP

    参考:http://lnmp.org/install.html cd /usr/local/src wget -c http://soft.vpser.net/lnmp/lnmp1.3-full.ta ...

  9. 轻量级封装DbUtils&Mybatis之三MyBatis分页

    MyBatis假分页 参考DefaultResultSetHandler的skipRows方法. 温馨提示:部分代码请参考轻量级封装DbUtils&Mybatis之一概要 解决方案 1)之前公 ...

  10. sed命令n,N,d,D,p,P,h,H,g,G,x解析

    1.sed执行模板=sed '模式{命令1;命令2}'即逐行读入模式空间,执行命令,最后输出打印出来2.为方便下面,先说下p和P,p打印当前模式空间内容,追加到默认输出之后,P打印当前模式空间开端至\ ...