> Java控制器代码
import com.springboot.app.controllers.GlobalController;
import com.springboot.app.entities.Branch;
import com.springboot.app.services.ReportParamsAccessListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.persistence.Tuple;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@Controller
public class GlobalReportController extends GlobalController {
@Autowired
ReportParamsAccessListService reportParamsAccessListService;
protected String branchParam(Model model,String paramName,String type, String name){
ModelMap modelMap = new ModelMap();
List<Branch> branchList = branchService.findAll();
modelMap.put("branchList",branchList);
modelMap.put("attr_name",name);
modelMap.put("attr_type",type);
model.addAttribute("branch",modelMap);
return "branch_param";
}
protected String dateParam(Model model,String paramName,String type,String name){
ModelMap modelMap = new ModelMap();
modelMap.put("attr_name",name);
modelMap.put("attr_type",type);
model.addAttribute("date",modelMap);
return "date_param";
}
@RequestMapping(value = "/report/test",method = RequestMethod.GET)
public String reportParams(Model model,String reportLink) {
LinkedHashMap<String,String> params = new LinkedHashMap<String, String>();
List<Tuple> reportParamAccess = reportParamsAccessListService.getReportParamAccess("reports/remittance_rub");
for (int i = 0; i<reportParamAccess.size(); i++){
try {
String name = reportParamAccess.get(i).get("name").toString();
String paramName = reportParamAccess.get(i).get("param_name").toString();
String attrType = reportParamAccess.get(i).get("attr_type").toString();
String attrName = reportParamAccess.get(i).get("attr_name").toString();
switch (name){
case "branch":
params.put(paramName,branchParam(model,paramName,attrType,attrName));
break;
case "date":
params.put(paramName,dateParam(model,paramName,attrType,attrName));
default:
}
} catch (Exception e) {
e.printStackTrace();
}
}
//System.out.println("params = " + params);
model.addAttribute("params",params);
//System.out.println(" dataAlling = " + model);
//return "report_params";
return pageContent(model,"report_params");
}
}
>使用百里香叶模板引擎
> HTML代码模板分支参数片段
<div class="col-3" xmlns:th="http://www.thymeleaf.org" th:fragment="branch_param">
<div class="form-group" >
<button type="button" id="dropdownUl" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select Branch
</button>
<div class="dropdown hide">
<ul>
<li th:each="branchList : ${branch.branchList}">
<input th:id = "'md_checkbox_' + ${branchList.id}" th:type="${branch.attr_type}" th:value="${branchList.ip}" th:name="${branch.attr_name}" class="filled-in chk-col-light-blue" />
<label th:for="'md_checkbox_' + ${branchList.id}" th:text="${branchList.name}"></label>
</li>
</ul>
</div>
</div>
</div>
> HTML代码模板报告参数片段
<ul xmlns:th="http://www.thymeleaf.org" th:fragment="report_params">
<li th:each="paramEntry : ${params}">
<div th:include="${paramEntry.value} :: ${paramEntry.value}" th:with="ParamName=${paramEntry.key}">
</div>
</li>
</ul>
> HTML代码模板日期参数片段
<div class="col-2" xmlns:th="http://www.thymeleaf.org" th:fragment="date_param">
<div class="form-group" >
<input th:type="${date.attr_type}" th:name="${date.attr_name}" class="form-control" />
</div>
</div>
我想将变量ParamName从报表参数传递到分支参数和日期参数,然后在片段中使用ParamName
例如在分支参数中这样
<div class="col-3" xmlns:th="http://www.thymeleaf.org" th:fragment="branch_param">
<div class="form-group" >
<button type="button" id="dropdownUl" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select Branch
</button>
<div class="dropdown hide">
<ul>
<li th:each="branchList : ${ParamName.branchList}">
<input th:id = "'md_checkbox_' + ${branchList.id}" th:type="${branch.attr_type}" th:value="${branchList.ip}" th:name="${branch.attr_name}" class="filled-in chk-col-light-blue" />
<label th:for="'md_checkbox_' + ${branchList.id}" th:text="${branchList.name}"></label>
</li>
</ul>
</div>
</div>
</div>
最佳答案
这是通过Thymeleaf中的preprocessing机制完成的
以下是您想做什么的摘要:
<li th:each="branchList : ${__${paramName}__.branchList}">