java - 如何通过关键变量百里香从模型中获取模型图
问题描述
> 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");
}
}
> 使用 thymealeaf 模板引擎
>> 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>
解决方案
推荐阅读
- c# - 使用正确加载的列表对象时接收到 LINQ 表达式 x 无法翻译错误
- javascript - 我正在尝试将多个图像下载为 zip 文件,但不幸的是出现错误 Loading 'phpOWyJmt.png' failed Loading meta information failed
- python - 我想将特定列保存到python中的新excel表中
- postgresql - TLS 加密的 PostgreSQL 连接不可能
- firebase - VueJS 和 Firebase - 以正确的方式导入 firebase 包
- c - 如何保存C程序输出?
- c++ - 挣扎于CreatWindow函数
- java - Selenium - Chromeoptions - JsonException
- icons - Inno Setup UninstallIcon 消失
- arrays - 数组或未定义或默认数组作为函数的参数