一、简单概述:
<form:checkboxes items="${list}" path="path" />
items要求为集合,是所有选择框的选项
path也要求为集合,是所有已选择框的选项
checkboxes 得到的数据是集合,数据库保存的是字符串,所以需要手动转
二、例如:
1、model.java
/**
*对应数据库中需要的真实字段
*/
private String proGnvq;/**
* 对应数据库中需要的真实字段 */ private String cyInnovationFunds;/**
* 用于前端checkboxes展示省级项目资质集合 伪字段数据库不存在
*/
private List<String> gavqList;/**
* 用于前端checkboxes展示国家级项目资质集合 伪字段数据库不存在
*/
private List<String> cyList;
public List<String> getGavqList() {
return gavqList;
}
public void setGavqList(List<String> gavqList) {
this.gavqList = gavqList;
}
public List<String> getCyList() {
return cyList;
}
2、ctl层
表单查看与编辑页面方法
public String form(GovQualification govQualification, Model model) {
String gavq = govQualification.getProGnvq();
String cy = govQualification.getCyInnovationFunds();
//把复选框String值转行成list 页面展示
List<String> gavqList = CommonMethods.getCheckBoxsList(gavq);
List<String> cyList = CommonMethods.getCheckBoxsList(cy);
govQualification.setGavqList(gavqList);
govQualification.setCyList(cyList);
model.addAttribute("govQualification", govQualification);
return "modules/cust/govQualificationForm";
}
编辑保存与新增保存方法
public String save(GovQualification govQualification, Model model, RedirectAttributes redirectAttributes) {
if (!beanValidator(model, govQualification)) { return form(govQualification, model); } //获取表单复选框值解析list拼成String 保存到数据库 govQualification.setProGnvq(CommonMethods.getCheckBoxsString(govQualification.getGavqList())); govQualification.setCyInnovationFunds(CommonMethods.getCheckBoxsString(govQualification.getCyList())); govQualification.setStatisticalTime(new Date()); govQualificationService.save(govQualification); addMessage(redirectAttributes, "保存政府资质成功"); return "redirect:" + Global.getAdminPath() + "/cust/govQualification/?repage"; }集合与String转换工具方法 /**
* Description:获取复选框String转化成list
*
* @Param checkBoxs 要解析的字符串
* Created by syz on 2018/1/4 0004 12:55
*/
public static List<String> getCheckBoxsList(String checkBoxs) {
List<String> list = new ArrayList<>();
if (StringUtils.isNotEmpty(checkBoxs)) {
if (checkBoxs.indexOf(",") != -1) {
String[] arr = checkBoxs.split(",");
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
} else {
list.add(checkBoxs);
}
}
return list;
}
/**
* Description:获取复选框list转化成String
*
* @Param checkBoxs 要解析的字符串
* Created by syz on 2018/1/4 0004 12:55
*/
public static String getCheckBoxsString(List<String> list) {
StringBuffer sb = new StringBuffer();
if (list != null) {
if (list.size() > 0 && list.size() == 1) {
return list.get(0);
} else {
sb.append(list.get(0));
for (int i = 1; i < list.size(); i++) {
sb.append("," + list.get(i));
}
return sb.toString();
}
}
return "";
}
}
3.前端jsp
<divclass="form-group">
<label class="col-sm-2 control-label">省级项目资质</label>
<div class="col-sm-10">
<form:checkboxes path="gavqList" items="${fns:getDictList('proGnvq')}" itemLabel="label" itemValue="value" htmlEscape="false" class=""/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">承担国家项目</label>
<div class="col-sm-10">
<form:checkboxes path="cyList" items="${fns:getDictList('canGnvq')}" itemLabel="label" itemValue="value" htmlEscape="false" class=""/>
</div>
</div>
这里的path对应的就是实体类中集合伪字段(也就是获取数据库中已选的值) items对应的是从字典变获取的所有选项(这里个人用自定义标签方式获取的,可根据自己的方式,
只要是集合或map即可。具体传list还是map 可根据
http://blog.csdn.net/linhaiyun_ytdx/article/details/65449134点击打开链接有具体案例
)
参看文献:http://blog.csdn.net/qq_18783269/article/details/64440599
http://blog.csdn.net/linhaiyun_ytdx/article/details/65449134