Thymeleaf:如何在不迭代的情况下从列表或数据库中检索元素?

Thymeleaf: How to retrieve elements from list or database without iterating?

我正在尝试使用 SpEl "th:if="${certs.flag == 'TRUE'}" 来检查数据库中的某个字段是真还是假。如果它设置为 "true",则不应显示该表单。我已经搜索过了到处都是,但似乎找不到任何文章有我需要的例子。每个人似乎都在使用迭代。但迭代不适用于我正在做的事情。

  @RequestMapping(value = "/cert_prog", method = RequestMethod.GET)
    public String examsList(Model model, CertificateProgramme certificateProgramme){
        Iterable<Exams> exams = examService.findAll();
        Iterable<School> schools = schoolService.findAll();
        Iterable<CertificateProgramme> certificateProgrammes = certificateService.findAll();
        Iterable<Picture> pictures = pictureService.findAll();
        CertificateProgramme mycert = certificateService.flagger(certificateProgramme);
        model.addAttribute("mycert", mycert);
        model.addAttribute("exams", exams);
    if(!model.containsAttribute("newExam")){
        model.addAttribute("newExam", new Exams());
    }

    model.addAttribute("certificateProgrammes", certificateProgrammes);
    if(!model.containsAttribute("certificate")){
        model.addAttribute("certificate",new CertificateProgramme());
    }
    model.addAttribute("grades", Grade.values());
    model.addAttribute("regions", Region.values());
    model.addAttribute("schools",schools);
    if(!model.containsAttribute("newSchool")){
        model.addAttribute("newSchool",new School());
    }

    model.addAttribute("picture", new Picture());
    return "cert_prog";
}

<form th:if="${certs.flag == 'TRUE'}" method="post" th:object="${certificate}" th:action="@{/basic}" class="form-inline inline new-item">
    <div th:replace="common/layout :: flash"></div>
    <fieldset>
        <legend> Personal Information</legend>
        <div class="row" th:classappend="${#fields.hasErrors('fullName')}? 'error' : ''" >
            <input type="text" class="form-control input-sm" th:field="*{fullName}" placeholder="Full Name example Jane Doe"/>
            <div class="error-message" th:if="${#fields.hasErrors('fullName')}" th:errors="*{fullName}"></div>
        </div>

        <div class="row" th:classappend="${#fields.hasErrors('date')}? 'error' : ''" >
            <input type="date" class="form-control input-sm datepicker" th:field="*{date}"
                   placeholder="Date Of Birth"/>
            <div class="error-message" th:if="${#fields.hasErrors('gender')}" th:errors="*{date}"></div>
        </div>

        <div class="row" th:classappend="${#fields.hasErrors('Nationality')}? 'error' : ''" >
            <input type="text" class="form-control input-sm autocomplete" th:field="*{Nationality}"
                   placeholder="Nationality"/>
            <div class="error-message" th:if="${#fields.hasErrors('Nationality')}" th:errors="*{Nationality}"></div>
        </div>
        <div class="row" th:classappend="${#fields.hasErrors('email')}? 'error' : ''" >
            <input type="text" class="form-control input-sm" th:field="*{email}"
                   placeholder="Email example jane.doe@example.com"/>
            <div class="error-message" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
        </div>
        <div class="row" th:classappend="${#fields.hasErrors('married')}? 'error' : ''" >
            <select th:field="*{married}" class="form-control input-lg">
                <option value="">[Select Martial Status]</option>
                <option value="Single">Single</option>
                <option value="Married">Married</option>
            </select>
            <div class="error-message" th:if="${#fields.hasErrors('married')}" th:errors="*{married}"></div>
        </div>
        <div class="row">
            <input type="text" class="form-control input-sm" th:field="*{guardianTelephoneNumber}"
                   placeholder="Guardian Telephone Number"/>
        </div>
        <div th:classappend="${#fields.hasErrors('courseOffered')}? 'error' : ''">
            <input type="text" th:field="*{courseOffered}" placeholder="CourseOffered"/>
            <div class="error-message" th:if="${#fields.hasErrors('courseOffered')}" th:errors="*{courseOffered}"></div>
        </div>
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-primary">Add</button>
        </div>
    </fieldset>
</form>

你的问题对我来说没有意义。如果要检查数据库中的单个字段,则不能传递项目列表。例如,在您的 html:

<div th:each="certs : ${certificateProgrammes}">
  <form th:if="${certs.flag == 'TRUE'}">
    .
    .
    .
  </form>
</div>

它遍历 ${certificateProgrammes} 的列表,并在循环中每次将其分配给 ${certs}。如果您不想循环,则不要向模型添加 Iterable<CertificateProgramme> certificateProgrammes = certificateService.findAll();,只需添加类型为 CertificateProgramme 的单个对象。然后你可以这样做:

model.addAttribute("certs", /* however you get the specific one you want to check */);

并在 html

<!-- no iterating required, because you know which object you're talking about -->
<form th:if="${certs.flag == 'TRUE'}">