【问题标题】:How to get model map from model by key variable thymeleaf如何通过关键变量百里香从模型中获取模型图
【发布时间】:2019-01-28 13:02:57
【问题描述】:

> 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>

【问题讨论】:

  • 嗨。如果你让它更容易阅读,你会得到更好的答案 - 我已经修复了代码格式以向你展示如何。我建议您还添加一个代码 sn-p 显示您想要做什么,从您的解释中不太清楚。
  • 请发布您完整的控制器/视图模板代码。至少只是整个控制器方法部分。

标签: java thymeleaf


【解决方案1】:

这是由 Thymeleaf 中的preprocessing 机制完成的

这里是您想要做的事情的 sn-p:

<li th:each="branchList : ${__${paramName}__.branchList}">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 2019-11-24
    • 2015-11-29
    • 2017-10-18
    • 2017-11-03
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多