as an experiment out of interest
after some investigation, i would also conclude as with others that the wrapping object would be the most straightforward implementation, especially if you are using spring 2 form tags.
However, there is something so deceptively attractive about adelinor's method to take advantage of spring automatic comma seperated binding, and there is a way to implement the view in a single line, using the function tag library. (only applies to spring bind tags)
The key is this: fn: split(status.value, '/')[0]
When getAsText() of your CustomDateEditor is called, it will return the formatted string, which you can split out.
Example code:
For completeness, here is the java code:
You will notice that these dropdowns do not have the ability to be blank. This is because it would lead to a bug where if day was not selected, and month and year were selected, after submit, the custom editor cannot set a valid date, hence you would lose the values you selected for month and year. I don't think there is a way around this. As nekoval pointed out, there is no binding error support in custom editors.
If you are binding composite string fields though, you can hack around this, see my next post below.
after some investigation, i would also conclude as with others that the wrapping object would be the most straightforward implementation, especially if you are using spring 2 form tags.
However, there is something so deceptively attractive about adelinor's method to take advantage of spring automatic comma seperated binding, and there is a way to implement the view in a single line, using the function tag library. (only applies to spring bind tags)
The key is this: fn: split(status.value, '/')[0]
When getAsText() of your CustomDateEditor is called, it will return the formatted string, which you can split out.
Example code:
Code:
<spring:bind path="command.dob"> <select name="${status.expression }" id="day"> <c:forEach items="${dayMap}" var="elem"> <option value="${elem.key}" <c:if test="${fn:split(status.value, '/')[0] eq elem.key}" >selected="selected"</c:if>>${elem.value }</option> </c:forEach> </select> <select name="${status.expression }" id="month"> <c:forEach items="${monthMap}" var="elem"> <option value="${elem.key}" <c:if test="${fn:split(status.value, '/')[1] eq elem.key}" >selected="selected"</c:if>>${elem.value }</option> </c:forEach> </select> <select name="${status.expression }" id="year"> <c:forEach items="${yearMap}" var="elem"> <option value="${elem.key}" <c:if test="${fn:split(status.value, '/')[2] eq elem.key}" >selected="selected"</c:if>>${elem.value }</option> </c:forEach> </select> </spring:bind>
Code:
class CompositeCustomDateEditor extends CustomDateEditor{ public CompositeCustomDateEditor(DateFormat dateFormat, boolean allowEmpty) { super(dateFormat, allowEmpty); } public void setAsText(String text) { if (text != null) { super.setAsText(text.replace(',', '/')); } } } public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); binder.registerCustomEditor( Date.class, "dob", new CompositeCustomDateEditor(dateFormat, true)); }
If you are binding composite string fields though, you can hack around this, see my next post below.
Comment