1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > SpringMVC前后台数据传递中Json格式的相互转换(前台显示格式 Json-lib日期处理)及Spr

SpringMVC前后台数据传递中Json格式的相互转换(前台显示格式 Json-lib日期处理)及Spr

时间:2023-07-02 10:56:00

相关推荐

SpringMVC前后台数据传递中Json格式的相互转换(前台显示格式 Json-lib日期处理)及Spr

两个方向:

一、前台至后台:

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

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

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

更多详细内容转载链接

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

2,/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中即可传向前台。

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

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

ResponseUtil.java

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

1 public class PageBean { 2private int page;// 页数 3private int rows;// 每页行数 4private int start;// 起始页 5 6public PageBean(int page, int rows) { 7 super(); 8 this.page = page; 9 this.rows = rows;10}11 12public int getPage() {13 return page;14}15 16public void setPage(int page) {17 this.page = page;18}19 20public int getRows() {21 return rows;22}23 24public void setRows(int rows) {25 this.rows = rows;26}27 28public int getStart() {29 start = (page - 1) * rows;30 return start;31}32 33 }

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先转换成JSONArrayjsonObject.put("rows", jsonArray);jsonObject.put("total", total);ResponseUtil.write(response, jsonObject);}

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

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

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

1 public class JavaDateObjectToJsonUtil implements JsonValueProcessor { 2 3private String format = null; 4 5public JavaDateObjectToJsonUtil(String format) { 6 super(); 7 this.format = format; 8} 9 10@Override11public Object processArrayValue(Object value, JsonConfig jsonConfig) {12 return null;13}14 15@Override16public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {17 if (value == null) {18 return "";19 }20 if (value instanceof Date) {21 return new SimpleDateFormat(format).format((Date) value);22 }23 return value.toString();24}25 26 }

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时多带上刚注册的JsonConfigjsonObject.put("rows", jsonArray);jsonObject.put("total", total);ResponseUtil.write(response, jsonObject);

}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。