jsoup操作模板代码,给radio select input 赋值,以及select回显

来源:互联网 发布:沈阳淘宝代运营公司 编辑:程序博客网 时间:2024/06/06 14:14

 以下代码仅作参考


LoanSign loanSign = loanSignService.getById(loanSignId);
   BorrowTemplate borrowTemplate = borrowTemplateService.getById(loanSign.getBorrowTemplate().getId());
   String json = loanSign.getParamsJson();  // 获取json格式字符串
   String template = borrowTemplate.getTempHtml();  // 获取模板Html
   Document doc = Jsoup.parse(template);  // 开始进行操作
   // 1.给input[type=text]赋值
   Element content = null;
   String k ="";
   List<String> v = new ArrayList<String>();
   
   //1.1获取select标签的name集合
   List<String> selectNames = new ArrayList<String>();
   Elements selects = doc.select("select");
   for (Element select : selects) {
     selectNames.add(select.attr("name"));
   }
   //1.2获取input[type=radio]标签的name集合
   List<String> radioNames = new ArrayList<String>();
   Elements radios = doc.select("input[type=radio]");
   for (Element radio : radios) {
    String name = radio.attr("name");
    if(!radioNames.contains(name)){
     radioNames.add(name);
    }
   }
   //1.3声明selectMap  和 radioMap
   Map<String,List<String>> selectMap = new HashMap<String,List<String>>();
   Map<String,List<String>> radioMap = new HashMap<String,List<String>>(); 
   
   if(StringUtils.hasLength(json)){
    Map<String, Object> map = JsonUtil.jsonToMap(json);
    for(Entry<String, Object> e: map.entrySet()){
     k = e.getKey();
     v = (List<String>) e.getValue();
     // 拦截select标签
     if(selectNames.contains(k)){
      selectMap.put(k, v);
      continue;
     }
     // 拦截input[type=radio]标签
     if(radioNames.contains(k)){
      radioMap.put(k, v);
      continue;
     }
     // 给input[type=text]赋值
     content = doc.getElementById(k);
     if(content!=null){
      content.val(v.get(0));
     }
     
    }
   }
   
   //2.给select进行赋值
   List<DictValue> educationList = this.dictValueService.findDictValueListByKey(DictValueEnum.EDUCATIONBACK);
   List<DictValue> comDicList = this.dictValueService.findDictValueListByKey(DictValueEnum.COMPANYTYPE);
   for (String name : selectNames) {
    //
    List<String> value = selectMap.get(name);
    String val = "";
    if(value!=null && value.size()>0){
     val = value.get(0);
    }
    StringBuffer option = new StringBuffer();
    // 学历
    if("education".equals(name)){
     for (DictValue d : educationList) {
      if(val.equals(d.getName())){
       option.append("<option selected='selected' value='"+d.getName()+"'>"+d.getName()+"</option>");
      }else{
       option.append("<option value='"+d.getName()+"'>"+d.getName()+"</option>");
      }
     }
    }
    // 企业类型
    if("companyType".equals(name)){
     for (DictValue d : comDicList) {
      if(val.equals(d.getName())){
       option.append("<option selected='selected' value='"+d.getName()+"'>"+d.getName()+"</option>");
      }else{
       option.append("<option value='"+d.getName()+"'>"+d.getName()+"</option>");
      }
     }
    }
    Elements e = doc.select("select[name="+name+"]");
    e.html(option.toString());
   }
   //3.给input[type=raio]选中
   for (String name : radioNames) {
    Elements elements = doc.select("input[name="+name+"]");
    List<String> value = radioMap.get(name);
    for (Element e : elements) {
     String val = e.val();
     if(value!=null && value.size()>0 && StringUtils.hasLength(val)){
      if(val.equals(value.get(0))){
       e.attr("checked","checked");
      }
     }
    }
   }
   model.put("loanSignId", loanSignId);
   model.put("templateHtml", doc.toString());


操作结果

0 0