2012-03-08 124 views
2

我现在面对一个奇怪的问题,现在用JodaTime DateTimes和一个Spring MVC控制器。虽然我看到InitBinder-注释的方法被调用时,它是没有效果,测试请求的字符串不绑定到我的域对象,如可以陈述以下错误消息:Spring MVC/@InitBinder没有效果

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); 
    } 
} 

除了测试请求(由弹簧测试-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标注的方法,结果是一样的... 我已经得到了相同类型的数据为另一实体的结合(与1个的DateTime成员)的,它工作正常。

任何想法是什么错?

由于提前,

罗尔夫

回答

2

好吧,我发现这个问题,我是直接为DateTime和不暴露我的属性,如日期我在做我的其他实体。

揭露日期的伎俩。