Reputation: 428
I am using JSTL and multiple check-boxes, i just want to checked some check-boxes by default
<form:checkboxes path="userName" items="${UserList}" id="userName" class="check-margin-top"/>
and I am doing like this
<form:checkboxes path="userName" items="${UserList}" id="userName"class="check-margin-top"
<c:forEach var='list' items="${UserList}">
<c:if test="${list == '1'}"checked="checked"></c:if>
</c:forEach>
></form:checkboxes>
And i am getting UserList like this { 4 = A,11 = DUMMY,9 = Test,5 = John Smith,6 = kp } i want to keep checked to 9 and 5 now what should i do ?
Upvotes: 0
Views: 1968
Reputation: 531
Here is the answer to your question.
Generate a runtime list for the checkboxes value, and link it to Spring’s form tag
<form:checkboxes>
//SimpleFormController...
protected Map referenceData(HttpServletRequest request) throws Exception {
Map referenceData = new HashMap();
List<String> userList= new ArrayList<String>();
webFrameworkList.add("John");
webFrameworkList.add("Smith");
webFrameworkList.add("Doe");
webFrameworkList.add("Peter");
referenceData.put("userList", userList);
return referenceData;
}
Checked by default… If you want to make 2 checkboxes with value “John” and “Smith” are checked by default, you can initialize the “favUser” property with value “John” and “Smith”. For example :
//SimpleFormController...
@Override
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
User user = new User();
user .setFavUser(new String []{"John","Smith"});
return user ;
}
User.java
public class User{
String [] favUser;
//getter & setter
}
your checkboxes should be
<form:checkboxes items="${userList}" path="favUser" />
Note
<form:checkboxes items="${dynamic-list}" path="property-to-store" />
For multiple checkboxes, as long as the “path” or “property” value is equal to any of the “checkbox values – ${dynamic-list}“, the matched checkbox will be checked automatically.
from https://www.mkyong.com/spring-mvc/spring-mvc-checkbox-and-checkboxes-example/
Upvotes: 2