Spring MVC / @InitBinder没有效果

时间:2012-03-08 00:09:49

标签: java binding spring-mvc jodatime

我现在面临一个奇怪的问题,JodaTime DateTimes和一个Spring MVC控制器。虽然我看到调用了InitBinder-annotated方法,但它没有效果,测试请求的字符串没有绑定到我的域对象,因为可以声明以下错误消息:

org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'eventCommand' on field 'endDate': rejected value [29/03/2015 12:13]; codes      [typeMismatch.eventCommand.endDate,typeMismatch.endDate,typeMismatch.org.joda.time.DateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [eventCommand.endDate,endDate]; arguments []; default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull @javax.persistence.Column @org.hibernate.annotations.Type org.joda.time.DateTime for value '29/03/2015 12:13'; nested exception is java.lang.IllegalArgumentException: Invalid format: "29/03/2015 12:13" is too short]
Field error in object 'eventCommand' on field 'startDate': rejected value [28/03/2015 12:13]; codes [typeMismatch.eventCommand.startDate,typeMismatch.startDate,typeMismatch.org.joda.time.DateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [eventCommand.startDate,startDate]; arguments []; default message [startDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'startDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull @javax.persistence.Column @org.hibernate.annotations.Type org.joda.time.DateTime for value '28/03/2015 12:13'; nested exception is java.lang.IllegalArgumentException: Invalid format: "28/03/2015 12:13" is too short]

这是控制器的有意义部分:

@Controller
@RequestMapping("/***/event")
public class EventController {

    private static final String COMMAND = "eventCommand";
    @Autowired
    private EventDao eventDao;

    @InitBinder(value = COMMAND)
    public void customizeConversions(WebDataBinder binder) {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        df.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }

    @RequestMapping(value = {
            "/new",
            "/edit/{id}"
    }, method = POST)
    public ModelAndView save(@ModelAttribute(COMMAND) @Valid final Event event, final BindingResult result) {
        if (result.hasErrors()) {
            return populatedEventForm(event);
        }
        eventDao.saveOrUpdate(event);
        return successfulRedirectionView();
    }

    private ModelAndView successfulRedirectionView() {
        return new ModelAndView("redirect:index.jsp");
    }

    private ModelAndView populatedEventForm(final Event event) {
        ModelMap model = new ModelMap(COMMAND, event);
        return new ModelAndView("event/form.jsp", model);
    }
}

以及测试请求(由spring-test-mvc驱动):

@Test
public void when_saving_valid_event_then_routed_to_home() throws Exception {
    mvc.perform(post("/***/event/new").
            param("title", "zuper title").
            param("description", "zuper description").
            param("startDate", "28/03/2015 12:13").
            param("endDate", "29/03/2015 12:13")).
            andExpect(status().isOk()).
            andExpect(view().name("redirect:index.jsp"));
}

实体:

@Entity
public class Event {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id")
    private long id;
    @NotBlank
    @Length(max = 1000)
    @Column(name = "description", nullable = false)
    private String description = "";
    @NotNull
    @Column(name = "start_date", nullable = false)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    private DateTime startDate;
    @NotNull
    @Column(name = "end_date", nullable = false)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    private DateTime endDate;
    @NotBlank
    @Length(max = 255)
    @Column(name = "title", nullable = false, unique = true)
    private String title = "";

    /* setters & getters */
}

如果我提交了@ InitBinder-annotated方法,结果是一样的...... 我为另一个实体(有1个DateTime成员)提供了相同类型的数据绑定,并且工作正常。

知道出了什么问题吗?

提前致谢,

罗尔夫

1 个答案:

答案 0 :(得分:2)

好吧我发现了问题,我直接将我的属性公开为DateTime而不是我为其他实体做的日期。

曝光日期就可以了。