Springmvc Converter Converter issu

learn the problems encountered when the converter: now the time to convert the String format sent from the foreground into the date format, the converter only specifies the format before conversion string and the format after conversion date, but the foreground is an object, the transmission is not the string type, then the converter will convert each field again? Or how to know that the String to be converted is date and not other String
package com.demo.domain;

import java.util.Date;

public class User {

private Integer id;

private String username;

private Date birthday;

private String sex;

private String address;

..getter setter

}

String to java.util.Date converter:

public class DateConveter implements Converter < String, Date > {

public Date convert(String source) {
    // TODO Auto-generated method stub
    try {
        if(null != source){
            DateFormat df = new SimpleDateFormat("yyyy:MM:dd HH-mm-ss");
            return df.parse(source);
        }
    } catch (Exception e) {
        // TODO: handle exception
    }
    return null;
}

}
controller:

public ModelAndView updateitem (User u) {

}


< H2 > description of principle < / H2 >

Converter < String, Date > interface must have a sourceType and targetType, here are String and Date , and then these two types form a key, that is logically similar to String_Date , and the corresponding value is your custom converter. So whenever a variable is converted from String to Date, it will be converted with this converter.

< H2 > Source code < / H2 >

GenericConversionService.getConverter method:

    protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
        ConverterCacheKey key = new ConverterCacheKey(sourceType, targetType);
        GenericConverter converter = this.converterCache.get(key);
        if (converter != null) {
            return (converter != NO_MATCH ? converter : null);
        }

        converter = this.converters.find(sourceType, targetType);
        if (converter == null) {
            converter = getDefaultConverter(sourceType, targetType);
        }

        if (converter != null) {
            this.converterCache.put(key, converter);
            return converter;
        }

        this.converterCache.put(key, NO_MATCH);
        return null;
    }
< H2 > answer questions < / H2 >
isn't it true that all the transmissions are of string type, so the converter will convert each field once?

the first half of the sentence is true, and the second half is slightly modified: the converter converts all fields of type Date once

. < H2 > expand < / H2 >

in fact, the conversion of front-end variables is not necessarily through converter. There are two ways to recommend it:

  • use @ DateTimeFormat to annotate
public class Person {
    private String name;
    private Integer age;
    @DateTimeFormat(pattern = "yyyyMMdd")
    private Date birthDay;
    private Float salary;
    private Integer version;
}
  • use PropertyEditor
public class DemoController {

    @InitBinder
    public void intDate(WebDataBinder dataBinder) {
        dataBinder.addCustomFormatter(new DateFormatter("yyyyMMdd"));
    }
    
    ....

}

in this way, you can also specify that the transformation applies only to a specified field

. Doesn't the name property of

input correspond to the field of User?
formatting date fields with @ DateTimeFormat annotations is also possible.

Menu