Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make spring checkboxes checked by default?

I'm using Spring 3.1.0.RELEASE. I have this field in my command object ...

public Set<EventFeed> getUserEventFeeds() {
    return this.userEventFeeds;
}

On my Spring JSP page, I want to display a checkbox list of all possible event feeds, and then have checked checkboxes if the user has one in his set. I want to have some special HTML formatting around each checkbox, so I'm trying ...

<form:form method="Post" action="eventfeeds.jsp" commandName="user">
    ...
        <c:forEach var="eventFeed" items="${eventFeeds}">
        <tr>
            <td><form:checkbox path="userEventFeeds" value="${eventFeed}"/></td>
            <td>${eventFeed.title}</td>
        </tr>
        </c:forEach>
    ...

However, the items aren't getting checked by default if one is in the set. How do I do this? Here is the binder I'm using in my controller class ...

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(EventFeed.class, new EventFeedEditor());
}

private class EventFeedEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        setValue(eventFeedsDao.findById(Integer.valueOf(text)));
    }

    @Override
    public String getAsText() {
        return ((EventFeed) getValue()).getId().toString();
    }
}
like image 568
Dave Avatar asked Mar 16 '12 14:03

Dave


1 Answers

Interestingly this works:

<form:checkbox path="services" value="${type}" checked="checked"/>
like image 110
keaplogik Avatar answered Dec 02 '22 16:12

keaplogik