spring - 如何使用 Spring 4 Boot + ThymeLeaf + Hibernate 根据另一个选择框的选择填充选择框

标签 spring thymeleaf

我正在创建一个注册表单,我必须在其中创建三个选择框。第一个选择框包含所有国家/地区,根据选择状态选择框变得可见,并且仅显示特定所选国家/地区可用的州。还有城市选择框,默认情况下城市下拉列表不会显示,直到选择状态,并且根据选择仅显示属于在状态选择框中选择的特定状态的城市。

我能够生成所有选择框,但我真的不知道如何根据选择进行过滤。

努力:

<div class="form-group">
    <label th:text="#{register.label.country}" `class="col-md-4 control-label">Country:</label>
    <div class="col-md-6">
        <select class="form-control" th:field="*{address.country}">
            <option value="-1">--- Select Country ---</option>
            <option th:each="country : *{countries}" th:value="${country.configId}" th:text="${country.configName}"></option>
        </select>
        <strong th:if="${#fields.hasErrors('address.country')}" th:errors="*{address.country}">Country Error</strong>
    </div>
</div>
<div class="form-group">
    <label th:text="#{register.label.state}" class="col-md-4 control-label">State:</label>
    <div class="col-md-6">
        <select class="form-control" th:field="*{address.state}">
            <option value="-1">--- Select State ---</option>
            <option th:each="state : *{states.?[parentid = 1]}" th:value="${state.configId}" th:text="${state.configName}"></option>
        </select>
        <strong th:if="${#fields.hasErrors('address.state')}" th:errors="*{address.state}">Country Error</strong>
    </div>
</div>
<div class="form-group">
    <label th:text="#{register.label.city}" class="col-md-4 control-label">City:</label>
    <div class="col-md-6">
        <select class="form-control" th:field="*{address.city}">
            <option value="-1">--- Select City ---</option>
            <option th:each="city : *{cities}" th:value="${city.configId}" th:text="${city.configName}"></option>
        </select>
        <strong th:if="${#fields.hasErrors('address.city')}" th:errors="*{address.city}">Country Error</strong>
    </div>
</div>

我能够生成所有的选择框,但我真的不知道如何使用 Spring boot 在 thymeleaf 中基于选择生成。

请帮帮我。我知道我可以通过使用 jquery ajax 来做到这一点,但我想避免,我想 thymeleaf 本身是否可能。

最佳答案

Thymeleaf 是一个服务器端 Web 模板引擎。 一旦它被渲染并显示给客户,Thymeleaf 就不再存在了。 处理它的唯一方法是通过 javascript;或者通过发送新请求来呈现新的过滤选项。

关于spring - 如何使用 Spring 4 Boot + ThymeLeaf + Hibernate 根据另一个选择框的选择填充选择框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404274/

相关文章:

java - 自动将 "headers"和 "produces"属性添加到我的 @RequestMapping

java - 将 mailx 设置为 Java SMTP 配置

java - jackson 没工作?

java - 如何在 Spring 中将值连接到字段?

java - 将对象的变量传递给 thymeleaf 中的 Controller

html - Thymeleaf:附加到 html 元素/避免重复迭代

java - ClassPathResource 没有得到类路径

spring-mvc - 如何在没有xml的情况下配置thymeleaf-extras-springsecurity4?

spring-mvc - 更新表格 : Spring mvc + thymeleaf

ajax - 如何将参数传递给 Thymeleaf Ajax 片段