spring mvc date type conversion @initBinder solving foreground and background is not uniform issue date format

Format may not find what we want in the background with a framework for developing web applications ssm when the date passed to the front desk
can use the following solutions
1. Add a method to the controller inside

/ * * 
* By adding the following solutions to problems spring not convert date data 
* @param Request 
* @param Binder 
* / 
@InitBinder 
public  void initBinder (the HttpServletRequest Request, 
ServletRequestDatabinder Binder) { 
// Do not delete the downside comment future !!! "yyyy -MM-dd "configuration file properties into
 // the SimpleDateFormat the dateFormat = new new
 // the SimpleDateFormat (getText (" date.format ", request.getLocale ())); 
the SimpleDateFormat the dateFormat = new new the SimpleDateFormat (
 " YYYY the MM-dd-HH : mm: SS " ); 
dateFormat.setLenient ( to false ); 
binder.registerCustomEditor (a Date. class ,null, new CustomDateEditor(
dateFormat, true));
}

Here, if the project often involved this format conversion, this method can be placed inside baseController, inheritance can, avoid each have to write a controller inside this method.

2. Use JsonFormat put annotations on the date the entity attributes

@Temporal(TemporalType.TIMESTAMP)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")



Guess you like

Origin www.cnblogs.com/guanxiaohe/p/11727524.html