Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a calender object to 3 dropdown boxes in Spring 3

I recently started porting my website to Spring 3. My model has Author objects in which I store some information which includes an object of the Calendar class.

To get rid of parsing dates I use 3 dropdown boxes for setting the Calendar. In Spring 2.5 I did the translation in the onBind method.

@Override
protected void onBind(HttpServletRequest request, Object command) throws Exception {
    Auteur auteur = (Auteur) command;
    Calendar geboorteDatum = getCompositeDate(request, "geboortedatum.time.date", "geboortedatum.time.month", "geboortedatum.time.year");
    auteur.setGeboortedatum(geboorteDatum);
}

getCompositeDate would return the Calendar object using the ServletRequestUtils. With in my JSP page:

<form:select path="geboortedatum.time.date">
    <c:forEach var="i" begin="1" end="31" step="1">
        <form:option value="${i}" label="${i}" />
    </c:forEach>
</form:select>
<form:select path="geboortedatum.time.month">
    <c:forEach var="i" begin="1" end="12" step="1">
        <form:option value="${i - 1}" label="${i}" />
    </c:forEach>
</form:select>
<form:select path="geboortedatum.time.year">
    <c:forEach var="i" begin="1900" end="2013" step="1">
        <form:option value="${i}" label="${i}" />
    </c:forEach>
</form:select>

I wonder how it would be possible to convert this code to Spring 3, or if there are alternatives if not possible. Thanks

like image 512
Jurgen R Avatar asked Dec 03 '25 06:12

Jurgen R


1 Answers

In Spring 3 @InitBinder can be used to bind an object

@InitBinder
protected void initBinder(WebDataBinder binder, WebRequest request) throws Exception {
//implementation
}

You can get request attributes in the same way by using the WebRequest object.

like image 66
drei01 Avatar answered Dec 07 '25 01:12

drei01