Spring MVC custom date type converter instance details, springmvc

Source: Internet
Author: User

Spring MVC custom date type converter instance details, springmvc

Spring MVC custom date type converter instance details

The WEB layer uses the Spring MVC Framework to pass the queried data to the APP or client. This is not the case, but the object class has a date type attribute, however, you must format them in advance and return them to them. To be honest, I have never done this before. I found the data in one breath, formatted it on the jsp page, and finally presented it to the user. However, this time is different. This time I only operate on data and there is no page. I took the data directly from the database to return the data to them, and they passed the data to me. I am stuck with the data, but here is a small problem.

First, let's restore the problem (this is a data export function). The data circled in red boxes is directly obtained from the database, however, unfortunately, all their objects contain date-type attributes, and then I directly use com. alibaba. fastjson. JSONObject converts it into a json string and returns it to the APP and client. However, all the time-type values in the data they get are timestamps, which are very unfriendly. I also feel very unfriendly, I promised to format them. No problem. After formatting, I set it again. The result is useless, useless, and useless. The important thing is said three times!

Solve the problem above. format the date type attribute value in the object and return it to the requester. Remember: this action is completed on the server side, and the types in the Entity class and data table remain unchanged. It is still of the date type.

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

Package com. tgsit. cjd. utils;/*** date type converter: * date type in the automatically formatted object */import java. text. simpleDateFormat; import java. util. date; import java. util. locale; import net. sf. json. jsonConfig; import net. sf. json. processors. jsonValueProcessor; public class JsonDateValueProcessor implements JsonValueProcessor {private String format = "yyyy-MM-dd"; public JsonDateValueProcessor () {super ();} public JsonDateValueProcessor (St Ring format) {super (); this. format = format ;}@ Override public Object processArrayValue (Object paramObject, JsonConfig paramJsonConfig) {return process (paramObject) ;}@ Override public Object processObjectValue (String paramString, Object paramObject, JsonConfig paramJsonConfig) {return process (paramObject);} private Object process (Object value) {if (value instanceof Date) {SimpleDateFormat sdf = New SimpleDateFormat (format, Locale. CHINA); return sdf. format (value);} return value = null? "": Value. toString ();}}

Step 2: If the json-lib package is not introduced in the project

<dependency>   <groupId>net.sf.json-lib</groupId>   <artifactId>json-lib</artifactId>   <version>2.4</version>   <classifier>jdk15</classifier></dependency>

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

Here, you must first create a JsonConfig object and then callRegisterJsonValueProcessor (Date. class, new JsonDateValueProcessor (); The method injects the converter into it, and finally calls the JsonArray formObject (data, new JsonConfig () method for processing.

Note: There is also a small pitfall.That is, when you are processing a pure object, you can directly use jsonObject. formObject (data, new JsonConfig ());

However, when you process a list set of data, remember to use jsonArray. formObject (data, new JsonConfig (); otherwise, an error is returned.

@ RequestMapping (value = "/exportInfo", method = RequestMethod. GET) @ ResponseBody public String exportInfo (HttpServletResponse response) throws Exception {List <QueryVo> exportInfoList = queryVoService. exportInfo (); if (exportInfoList! = Null & exportInfoList. size ()> 0) {List <Object> result = new ArrayList <Object> (); JsonResult model = null; for (QueryVo queryVo: exportInfoList) {OwnerInfo ownerInfo = ownerInfoService. selectOwnerInfoByVIN (queryVo. getVin (); List <MortgagerInfo> morInfoList = mortgagerInfoService. selectByVIN (queryVo. getVin (); List <TransferInfo> traInfoList = transferInfoService. selectByVIN (queryVo. getVin (); model = new JsonResult (); model. setOwnerInfo (ownerInfo); model. setMortgagerInfo (morInfoList); model. setTransferInfo (traInfoList); result. add (model);} // call the custom date type converter to automatically format the Date Field Value JsonConfig jsonConfig = new JsonConfig (); jsonConfig. registerJsonValueProcessor (Date. class, new JsonDateValueProcessor (); return CGSConstants. returnJson (CGSConstants. SUCCESS, "exported successfully", JSONArray. fromObject (result, jsonConfig);} return CGSConstants. returnJson (CGSConstants. SUCCESS, "No data available", null );}

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.