Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring MVC__ Custom date Type Converter

2025-04-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

The WEB layer uses the Spring MVC framework to pass the queried data to the app side or the client, which is fine, but it is a trick to have date-type attributes in the entity class, but you must format them in advance and return them. To tell you the truth, I've never done this before. I used to query the data in one breath, then format it on the jsp page, and finally show it to the user. But this time is different, this time I just manipulate the data, there are no pages. Take the data directly from the database to them to return the data, they send me the data I persist the data, at this point a small problem comes silently.

First of all, let's restore the problem (this is a data export function). The data circled in red boxes in the following picture are all data obtained directly from the database, but unfortunately their objects contain attributes of date types. Then I directly use com.alibaba.fastjson.JSONObject to convert them into json strings and return them to the app side and the client, but the values of the time types in the data they get are all time stamps, which are very impersonal. I also thought it was very impersonal, so I promised to format it for them, but unexpectedly, the problem came, and then it was set up again after it was formatted. As a result, it was useless and the important things were said three times!

To resolve the problem above, format the property value of the date type in the object and return it to the requestor. Remember: this action is done on the server side, and the type in the entity class and data table remains the same, so it is still a date type.

Step 1: first customize a date type converter (with code)

1 package com.tgsit.cjd.utils; 3 / * * 4 * date type converter: 5 * date types in automatic formatting objects 6 * / 8 import java.text.SimpleDateFormat; 9 import java.util.Date;10 import java.util.Locale;12 import net.sf.json.JsonConfig;13 import net.sf.json.processors.JsonValueProcessor;15 public class JsonDateValueProcessor implements JsonValueProcessor {17 private String format = "yyyy-MM-dd" 19 public JsonDateValueProcessor () {20 super (); 21} 23 public JsonDateValueProcessor (String format) {24 super (); 25 this.format = format; 26} 28 @ Override 29 public Object processArrayValue (Object paramObject, 30 JsonConfig paramJsonConfig) {31 return process (paramObject) 32} 34 @ Override 35 public Object processObjectValue (String paramString, Object paramObject, 36 JsonConfig paramJsonConfig) {37 return process (paramObject); 38} 41 private Object process (Object value) {42 if (value instanceof Date) {43 SimpleDateFormat sdf = new SimpleDateFormat (format,Locale.CHINA); 44 return sdf.format (value) 45} 46 return value = = null? "": value.toString (); 47} 49}

Step 2: if the json-lib package is not introduced in the project, introduce

1 2 net.sf.json-lib3 json-lib4 2.45 jdk156

Step 3: use the date type converter to format the date data before returning it

You must first create the JsonConfig object, then call the registerJsonValueProcessor (Date.class, new JsonDateValueProcessor ()); method to inject the converter, and finally call the JsonArray's formObject (data, new JsonConfig ()) method to process it.

Note: there is also a small hole, that is, when you are dealing with a simple physical object, you can directly jsonObject.formObject (data, new JsonConfig ()).

But when you are dealing with a list collection of data, remember to use jsonArray.formObject (data, new JsonConfig ()); otherwise, an error is reported.

1 @ RequestMapping (value = "/ exportInfo", method = RequestMethod.GET) 2 @ ResponseBody 3 public String exportInfo (HttpServletResponse response) throws Exception {4 List exportInfoList = queryVoService.exportInfo (); 5 if (exportInfoList! = null & & exportInfoList.size () > 0) {6 List result = new ArrayList (); 7 JsonResult model = null 8 for (QueryVo queryVo: exportInfoList) {9 OwnerInfo ownerInfo = ownerInfoService.selectOwnerInfoByVIN (queryVo.getVin ()); 10 List morInfoList = mortgagerInfoService.selectByVIN (queryVo.getVin ()); 11 List traInfoList = transferInfoService.selectByVIN (queryVo.getVin ()); 12 model = new JsonResult (); 13 model.setOwnerInfo (ownerInfo) 14 model.setMortgagerInfo (morInfoList); 15 model.setTransferInfo (traInfoList); 16 result.add (model); 17} 18 / / call a custom date type converter to automatically format the field value of the date type in the object 19 JsonConfig jsonConfig = new JsonConfig (); 20 jsonConfig.registerJsonValueProcessor (Date.class, new JsonDateValueProcessor ()) 21 return CGSConstants.returnJson (CGSConstants.SUCCESS, "Export successful", JSONArray.fromObject (result, jsonConfig)); 22} 23 return CGSConstants.returnJson (CGSConstants.SUCCESS, "No data yet", null); 24}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report