How to set spring flags by default?

I am 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 show a list of all possible event feeds, and then check the boxes if the user has one of his sets. I want 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, items are not checked by default if they are in a set. How can I do it? Here is the binding I use 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(); } } 
+7
source share
5 answers

@Dave, there is something in the form: checkBoxes. You can try.

 <form:checkboxes path="userEventFeeds" items="${eventFeeds}" itemLabel="id" itemValue="value"/> 

My guess: you must have the "id" and "value" defined in the EventFeed class.

I just tried this, having String [] availableList and String [] selectedList. It works like a charm. You can also try.

+7
source

Interestingly, this works:

 <form:checkbox path="services" value="${type}" checked="checked"/> 
+6
source

You can do this by putting the selected default property true in your class

 class User { boolean userEventFeeds = true; } 
+5
source

I tried both form:checkboxes and form:checkbox with the same data, and the first one works, the second one doesn't. (You have the same Spring release

It seems that there was a mistake which, despite their claims, seems to be still there.

0
source

For my use (reacting to material on a list that has nothing to do with the object being populated), this code worked:

(Keep in mind that this is the last type of code, other solutions are more likely to work better.)

 <%@ taglib prefix="jstl" uri="http://java.sun.com/jsp/jstl/core" %> <jstl:forEach var="listObject" items="${someList}"> <jstl:if test="${listObject.active}"> <form:checkbox path="active" checked="checked"/> </jstl:if> <jstl:if test="${!listObject.active}"> <form:checkbox path="active"/> </jstl:if> </jstl:forEach> 
0
source

All Articles