The Web layer uses the Spring MVC framework to pass the queried data to the app side or client, which is fine, but the pit is a property of the date type in the entity class, but you have to return it to it before you format it well. Honestly, I've never done this before, I've been in a breath. Query the data, then format on the JSP page, and finally show to the user. But this time, I am purely operational data, no page. Get the data directly from the database, they send me data, I persist the data, when it comes to a small problem here silently.
First of all, restore the problem (this is a data export function), in the red box is directly from the database to get the data, but unfortunately their object contains a date type of properties, Then I directly use Com.alibaba.fastjson.JSONObject to turn it into a JSON string back to the app side and the client, but they get the value of the time type of data is a timestamp, very impersonal, I also feel very impersonal, promised to give them a format, and behold, the problem, the format of the good And then set it in, the result is useless, useless, useless, important things to say three times!
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/855612/201704/855612-20170414163212720-860428084. PNG "style=" margin:0px;padding:0px;border:0px; "/>
Resolve the problem above, format the property value of the date type in the object, and then return it to the requestor. Remember: This action is done on the server side, and the type in both the entity class and the data table is the same, so is the date type.
First step: Customize a Date type converter (with code) first
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
1 package com.tgsit.cjd.utils; 3 /** 4 * Date type converter: 5 * date types in auto-formatted 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 &nbsP; 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 }
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
Step two: If the Json-lib package is not introduced in the project, the introduction
1 <dependency>2 <groupid>net.sf.json-lib</groupid>3 <ARTIFACTID>JSON-LIB</ARTIFACTID&G T;4 <version>2.4</version>5 <classifier>jdk15</classifier>6</dependency>
Step Three: Use Date type converters to format date data before returning data
< Strong style= "margin:0px;padding:0px;" >registerjsonvalueprocessor (Date.class, New Jsondatevalueprocessor ()); method injects the converter in, and finally calls the Jsonarray formobject (data, The new Jsonconfig ()) method is processed.
< Strong style= "margin:0px;padding:0px;" That is, when you are dealing with a purely entity object, you can directly jsonobject. Formobject (data, New Jsonconfig ());
But when you are working with a list collection, remember to use Jsonarray. Formobject (data, New Jsonconfig ());
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
1 @RequestMapping (value = "/exportinfo", method = requestmethod.get) 2 @ResponseBody 3 public String Exportinfo (Httpservletresponse response) throws Exception{ 4 list<queryvo> exportinfolist = queryvoservice.exportinfo (); 5 if (exportinfolist != null && exportinfolist.size () > 0) { 6 List<Object> result = new ArrayList<Object> (&NBSP;7); jsonresult model = null; 8 for ( Queryvo queryvo :&nbsP;exportinfolist) { 9 ownerinfo ownerinfo = ownerinfoservice.selectownerinfobyvin ( Queryvo.getvin ());10 list<mortgagerinfo> morinfolist = mortgagerinfoservice.selectbyvin ( Queryvo.getvin ());11 list<transferinfo> trainfolist = transferinfoservice.selectbyvin ( Queryvo.getvin ());12 model = new jsonresult ();13 model.setownerinfo (Ownerinfo);14 &nbsP; model.setmortgagerinfo (morinfolist);15 model.settransferinfo (traInfoList);16 result.add (model); 17 }18 //calls a custom date type converter to automatically format the field value of a date type in an object 19 JsonConfig jsonConfig = new Jsonconfig (); 20 Jsonconfig.registerjsonvalueprocessor (Date.class, new jsondatevalueprocessor ());21 return cgsconstants.returnjson ( Cgsconstants.success, "Export succeeded", Jsonarray.fromobjECT (result, jsonconfig));22 }23 return cgsconstants.returnjson (cgsconstants.success, "No data Yet", NULL); 24 }
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
Spring mvc__ Custom Date type converter