spring mvc time format conversion

Time format conversion errors often occur when passing values ​​from the front to the back.

Solution: The problem occurred when the jsp page passed the value to the action.

The reason is that the time format of the jsp page is a string format, and the background is the DATE format, and there will be a format mismatch.

1. Find a place to create a class and let him implement the Converter interface, two generic representatives, String type is converted into date type

//Converter<S, T>
//S:source, the type of the source to be converted
//T:target, the target type that needs to be converted
public class DateConverter implements Converter<String, Date> {

	@Override
	public Date convert(String source) {
		try {
			// convert string to date type
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
			Date date = simpleDateFormat.parse(source);

			return date;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		// return null if conversion exception
		return null;
	}
}
2. Configure  Converter

Add configuration in spring mvc configuration file

<!-- Configure annotation driver -->
<!-- If you configure this tag, you don't need to configure it... -->
<mvc:annotation-driven conversion-service="conversionService" />

<!-- Converter Configuration -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	<property name="converters">
		<set>
                        <!-- This class name is the full path of the class just created -->
	        	<bean class="cn.itcast.springmvc.converter.DateConverter" />
</set></property></bean>

Solution: The problem occurred when the action passed the value to the jsp page.

The reason is that the time format of the jsp page is a string format, and the background is the DATE format, and there will be a format mismatch.

1. Also find a place to create a class

Inherit JsonSerializer<Date>

package com.cn.config;

import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

public class viewObjectMapper extends JsonSerializer<Date>{

	  @Override  
	    public void serialize(Date value, JsonGenerator jgen, SerializerProvider arg2)  
	            throws IOException, JsonProcessingException {  
	        // TODO Auto-generated method stub  
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	        String formattedDate = formatter.format(value);  
	        jgen.writeString(formattedDate);  
	          
	    }  

	
	 
	
}

2. Add an annotation to the get method of the entity class parameter that needs to be passed and use the class just written

The annotation must be written on the get method

public class UserInfo {  
    private String name;  
    private int id;  
    private Date birthday;  
      
    @JsonSerialize(using = viewObjectMapper.class)  
    public Date getBirthday() {  
        return birthday;  
    }  
  
    public void setBirthday(Date birthday) {  
        this.birthday = birthday;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
      
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734140&siteId=291194637