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

时间:2017-02-24 14:38:21

标签: java spring-boot iteration thymeleaf spring-el

我正在尝试使用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>

1 个答案:

答案 0 :(得分:1)

你的问题对我来说没有意义。如果要从数据库中检查单个字段,则无法传递项目列表。例如,在你的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'}">