两个方向:

一、前台至后台:

  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. BZOJ3444 最后的晚餐【细节题+组合数学】*

    BZOJ3444 最后的晚餐 Description [问题背景] 高三的学长们就要离开学校,各奔东西了.某班n人在举行最后的离别晚餐时,饭店老板觉得十分纠结.因为有m名学生偷偷找他,要求和自己暗恋的 ...

  2. 微软 Windows 系统检测网络连通性(用于显示感叹号)竟然是通过访问一个特殊网址来实现的

    一次我走到了弱网环境中,意外地发现浏览器打开了 http://www.msftconnecttest.com/redirect 网址,随后右下角的网络图标出现了一枚“感叹号”.   吹水的推断 从直观 ...

  3. C#中 this关键字 四种用法

    /// <summary> /// 主程序入口 /// </summary> /// <param name="args"></param ...

  4. ballerina 学习八 Parallel( 并行处理)

    实际上就是并行进行任务的处理 简单例子 代码 import ballerina/io; function main (string… args) { worker first { io:println ...

  5. smarty基本用法,循环,判断

    require './smarty/Smarty.class.php'; $sm = new Smarty; $sm->setTemplateDir("./dir");//设 ...

  6. FastAdmin 的 API 可以分级吗?

    FastAdmin 的 API 可以分级吗? 有小伙伴问 FastAdmin 的API 可以分别吗,使用 / 出现错误. Karson 的说明是: 完全支持的,默认是使用.进行分隔的,如果需要/,请开 ...

  7. python中format函数学习笔记

    简而言之,format函数就是用{}来代替之前的输出字符时使用的% print('my name is %s and I am %d years old' % ('porsche',23)) 下面详细 ...

  8. qt下用启动图

    void showSplash(void) { QSplashScreen*splash=newQSplashScreen; splash->setPixmap(QPixmap(":/ ...

  9. 初识C++模板元编程(Template Mega Programming)

    前言:毕设时在开源库上做的程序,但是源码看得很晕(当时导师告诉我这是模板元编程,可以不用太在乎),最近自己造轮子时想学习STL的源码,但也是一样的感觉,大致了解他这么做要干什么,但是不知道里面的机制. ...

  10. redis数据结构对象

    redis的数据结构对象包括 字符串 列表 哈希 集合 有序集合五种数据结构对象,由底层的8种数据结构组成这五种对象,每种对象的实现不同的数据都是不一样的. 结构 typedef struct red ...