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 ?
答案 0 :(得分:1)
以下是您问题的答案。
为复选框值生成运行时列表,并将其链接到Spring的表单标记
<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;
}
默认选中... 如果要在默认情况下选中值为“John”和“Smith”的2个复选框,则可以使用值“John”和“Smith”初始化“favUser”属性。例如:
//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
}
你的复选框应该是
<form:checkboxes items="${userList}" path="favUser" />
请注意
<form:checkboxes items="${dynamic-list}" path="property-to-store" />
对于多个复选框,只要“path”或“property”值为 等于任何“复选框值 - $ {dynamic-list}”,匹配 复选框将自动检查。
来自https://www.mkyong.com/spring-mvc/spring-mvc-checkbox-and-checkboxes-example/