In my Spring 4 driven portlet I have a data-object which contains a date-field. In the JSP-view I have two separate fields for it, date (dd/mm/yyyy) and time (hh/mm). The action handler in my controller receives the form-data by using the @ModelAttribute
-annotation:
@ActionMapping(params = "action=update")
public void onUpdate(@Valid @ModelAttribute("myObject") MyObject myObject,
BindingResult bindingResult, ActionResponse response, SessionStatus status)
{
if (!bindingResult.hasErrors())
myObjectService.save(myObject);
else
// provide 'myObject' in the view which displays validation errors
}
I need to merge date and time before the form-data is validated an onUpdate()
receives myObject
, since in MyObject
there is only one java.util.Calendar
-field for the whole date available.
Idea 1 to work around this need
Now I thought, I could also split the date into two separate fields in MyObject
and provide a getter which merges the values on demand:
@Column(name = "begin")
public Calendar getBegin()
{
// return calendar object built from this.beginDate and this.beginTime
}
but I think this is not a good idea for several reasons (see this question: Hibernate Annotations - Which is better, field or property access?) I want the model-object to be a mirror of the database-record and hence it should be validated before getting assigned.
Idea 2
Another approch would be, to create or modify the calendar-object in myObject
on demand, when setting the date or time:
@Column(name = "begin")
@Temporal(TemporalType.TIMESTAMP)
private Calendar begin;
public void setBeginDate(String value)
{
// assign a new calendar object to "begin", if "begin" is null
// set only day, month and year components on this calendar object
}
public void setBeginTime(String value)
{
// see "setBeginDate", do the same with hours and minutes
}
The problem here is, that a new calendar-object is created if only one of the fields "date" or "time" is valid. The field in the view is filled with the current date or current time (depending on which value was correct)
I can even solve this problem, by adding another private isValidDate
-flag to the model. But I think this is an unclean solution.
Conclusion
I think there is a big difference between myObject
for the controller and myObject
as an actual model-object. myObject
should be a model-object, as soon as being validated and "mapped".
So here my questions:
- Do you think that the last point reveals using
@ModelAttribute
as a bad idea generally? - Or is there a way mapping and validating form-data BEFORE the
MyObject
-instance is created? - If not, how would you recommend to solve the problem?