Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring ModelAttribute中未调用PropertyEditor_Spring_Spring Mvc - Fatal编程技术网

Spring ModelAttribute中未调用PropertyEditor

Spring ModelAttribute中未调用PropertyEditor,spring,spring-mvc,Spring,Spring Mvc,作为我的头衔,我有问题,我不明白为什么不能。所以,我有一个Person类,PersonPropertyEditor类和控制器 这是: 人员类别: public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }

作为我的头衔,我有问题,我不明白为什么不能。所以,我有一个Person类,PersonPropertyEditor类和控制器

这是:

人员类别:

public class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
PersonPropertyEditor类:

public class PersonPropertyEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Person person = new Person();
        person.setName(text);

        setValue(person);
    }
}
控制器:

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        webDataBinder.registerCustomEditor(Person.class,
                new PersonPropertyEditor());
    }

    @RequestMapping(value = "/addPerson", method = RequestMethod.POST)
    public String homePost(
            @ModelAttribute("personConverted") Person personConverted) {
        System.out.println(personConverted.getName());
        return "redirect:/home";
    }
JSP文件:

    <form action="addPerson" method="post">
        <input type="text" name="personConverted" />
        <input type="submit" value="Submit" />
    <form>
所以,我注册了一个自定义属性编辑器,在controller中,我将从字符串转换为person对象,但我不知道为什么当我使用ModelAttribute时,Spring不会调用我的自定义属性编辑器,但如果我使用RequestParam,它就可以了

我知道我可以使用转换器来实现这一点,但我只是想了解为什么Spring不这么叫,这是Spring的bug


提前谢谢

我使用@JsonDeserialize解决了这个问题

@JsonDeserialize(using=DateTimeDeserializer.class)
public DateTime getPublishedUntil() {
    return publishedUntil;
}
我必须实现自定义反序列化程序

public class DateTimeDeserializer extends StdDeserializer<DateTime> {

private DateTimeFormatter formatter = DateTimeFormat.forPattern(Constants.DATE_TIME_FORMAT);

public DateTimeDeserializer(){
    super(DateTime.class);
}

@Override
public DateTime deserialize(JsonParser json, DeserializationContext context) throws IOException, JsonProcessingException {
        try {
            if(StringUtils.isBlank(json.getText())){
                return null;
            }
            return formatter.parseDateTime(json.getText());
        } catch (ParseException e) {
            return null;
        }
}

}

您是否尝试过使用spring表单标记?不,我没有,因为我只是作为示例来做,我想从字符串转换为Person。如果我使用表单标签,我将使用纯个人模型属性。请解释您的问题