java - Spring MVC / @InitBinder is without effects -
i facing weird problem right jodatime datetimes , spring mvc controller. although see initbinder-annotated method invoked, without effects, strings of test request not bound domain object, can state following error message:
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 convert property value of type 'java.lang.string' required type 'org.joda.time.datetime' property 'enddate'; nested exception org.springframework.core.convert.conversionfailedexception: failed convert type java.lang.string type @javax.validation.constraints.notnull @javax.persistence.column @org.hibernate.annotations.type org.joda.time.datetime value '29/03/2015 12:13'; nested exception java.lang.illegalargumentexception: invalid format: "29/03/2015 12:13" 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 convert property value of type 'java.lang.string' required type 'org.joda.time.datetime' property 'startdate'; nested exception org.springframework.core.convert.conversionfailedexception: failed convert type java.lang.string type @javax.validation.constraints.notnull @javax.persistence.column @org.hibernate.annotations.type org.joda.time.datetime value '28/03/2015 12:13'; nested exception java.lang.illegalargumentexception: invalid format: "28/03/2015 12:13" short]
here meaningful part of controller:
@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); } }
as test request (driven 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")); }
and entity:
@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 */ }
if commit @initbinder-annotated method, result same... i've got same kind of data binding entity (with 1 datetime member) , works fine.
any idea wrong ?
thanks in advance,
rolf
alright found problem, exposing properties directly datetime , not date doing other entity.
exposing dates did trick.
Comments
Post a Comment