Java 数据库中组合字段操作解决方案
两个复选框分别对应数据库中SHOW_CATEGORY中的值:
1:国家级
2:市级
1,2 国家级 和 市级
前端代码(使用Freemarker )(虽然是操作同一字段但name要定义不同,方便后台存储返回)
<input name="query_show_category" type="checkbox" value="1" <#if query_show_category?? && query_show_category=="1"> checked="checked"</#if>>国家级</input>
<input name="query_show_category_s" type="checkbox" value="2" <#if query_show_category_s?? && query_show_category_s=="2"> checked="checked"</#if>>市级</input>
<input type="submit" value="<@s.m "global.query"/>" />
由于CMS项目 框架及接口以设定好,不易更改后台
show_category=RequestUtils.getQueryParam(request,"query_show_category");
show_category_s=RequestUtils.getQueryParam(request,"query_show_category_s");
分别 只能接受单个CheckBox 的值 如 “1” 或 “2” 无法得到组合的值 如“1,2”
那这种组合值如何得到?
方案:根据后台接收字符串拼接形成组合值 在根据条件 过滤 就能得到 "1" ,"2","1,2" ,null几种情况
后台代码:
String show_category=""; //定义接收国家级checkbox
String show_category_s="";//定义接收市级级checkbox
String show_compose=""; //定义接收拼接值如 (1,2)
String Comma=","; //逗号
if(show_category!=null ){
//这里只能接收一个参数!!需要接受组合参数
show_category=RequestUtils.getQueryParam(request,"query_show_category");
show_category_s=RequestUtils.getQueryParam(request,"query_show_category_s");
根据条件过滤逗号(单个值要过滤逗号 两个值要用逗号拼接)
if (RequestUtils.getQueryParam(request, "query_show_category") != null) {
show_category = RequestUtils.getQueryParam(request,"query_show_category");
show_compose = show_category;
}
if (RequestUtils.getQueryParam(request, "query_show_category_s") != null) {
show_category_s = RequestUtils.getQueryParam(request,"query_show_category_s");
show_compose = show_category_s;
}
/*这里判断不够充分的化前面checkbox 单选会把show_compose重置 效果
if(show_category!=null && show_category_s!=null){
show_compose=show_category+Comma+show_category_s;
}
*/
//如果出现条件影响变量重置时 应改为 大范围 排除掉条件来 控制小范围
if((show_category!=null &&!show_category.equals("")) && (show_category_s!=null) && !show_category_s.equals("")){
show_compose=show_category+Comma+show_category_s;
}else if(show_category!=null &&!show_category.equals(""))
{
show_compose=show_category;
}else if(show_category_s!=null && !show_category_s.equals("")){
show_compose=show_category_s;
}
model.addAttribute("query_show_category", show_category);
model.addAttribute("query_show_category_s", show_category_s);
}
Pagination p = manager.getPageByRight(query_Area_id, query_code_name,
recommend_flag, show_compose, cpn(pageNo), CookieUtils
.getPageSize(request));
model.addAttribute("pagination", p);
另外补充一点 Freemaker中 在dom节点中的变量判断范围只限定在当前文本域中
<input name="query_show_category" type="checkbox" value="1" <#if query_show_category?? && query_show_category=="1"> checked="checked"</#if>>国家级</input>