【问题标题】:thymeleaf attribute check contains by other attribute valuethymeleaf 属性检查包含由其他属性值
【发布时间】:2017-09-12 10:57:42
【问题描述】:

我的 spring 控制器发送对象“部门”并且我的表单得到它 并且需要检查员工检查了所有部门的多少工作 保存效果很好,但是当用户编辑员工时,他需要查看已检查的工作。

为了让它工作,我需要让它像:th:checked="${jobs.contains(${job.id})}" 但我想我不能将属性值放在其他属性值中。

<div th:each="job : ${department.jobList}" class="col-md-4">
    <fieldset class="form-group">
        <label class="custom-control custom-checkbox">
            <input name="jobs" th:checked="${jobs.contains(34L)}" th:value="${job.id}" type="checkbox" class="custom-control-input"/>
            <span th:text="${job.name}" class="custom-control-description"></span>
            <span class="custom-control-indicator"></span>
        </label>
    </fieldset>
</div>

我的控制器:

// Edit employee page
@RequestMapping("/panel/{workPlaceName}/employees/{id}")
public String editEmployeesPage(Model model, HttpSession httpSession, HttpServletRequest request, @PathVariable Long id) {
    // Auto login check
    WorkPlace workPlace = new WorkPlace();
    Employee employee = new Employee();
    Object logInTemp = httpSession.getAttribute ("loggedInUser");
    if (logInTemp != null) {
        Object tempLogIn = httpSession.getAttribute ("loggedInUser");
        LogIn logIn = (LogIn) tempLogIn;
        workPlace = workPlaceService.findById(logIn.getUserName());
        model.addAttribute("workPlace",workPlace);
        employee = employeeService.findById(id);
        if(!model.containsAttribute("employee")) {
            model.addAttribute("employee", employee);
        }
    } else {
        return "redirect:/";
    }

    // END of Auto login check
    ArrayList<Long> jobs = new ArrayList<>();
    for (int i = 0; i < employee.getJobList().size(); i++) {
        jobs.add(employee.getJobList().get(i).getId());
    }
    model.addAttribute("jobs", jobs);
    model.addAttribute("action", "/panel/" + workPlace.getName() + "/employees/" + employee.getId());
    return "workplace/editEmployee";
}

【问题讨论】:

    标签: java html spring hibernate thymeleaf


    【解决方案1】:

    好的答案是:th:checked="${jobs.contains(job.id)}"

    我的错误是:th:checked="${jobs.contains(${job.id})}"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 2021-10-30
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多