Java Spring MVC@ModelAttribute绑定子类

Java Spring MVC@ModelAttribute绑定子类,java,forms,spring-mvc,thymeleaf,Java,Forms,Spring Mvc,Thymeleaf,我有两个名为District和Street的实体类,在视图中,要形成的是指定的Street对象。如何绑定Street.district.id的值?关于提交,我得到NullPointerException 查看实体和表单: <form class="form-inline" action="#" th:action="@{/direct/api/v1/newStreet}" th:object="${street}" method="POST"> <di

我有两个名为District和Street的实体类,在视图中,要形成的是指定的Street对象。如何绑定Street.district.id的值?关于提交,我得到NullPointerException

查看实体和表单:

 <form class="form-inline" action="#" th:action="@{/direct/api/v1/newStreet}" th:object="${street}" method="POST">
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" th:field="*{name}" class="form-control" id="name" placeholder="Street name"/>
            </div>
            <div class="form-group">
                <label for="district">District</label>
                <select class="form-control" id="district">
                    <option th:each="d : ${districts}" th:field="*{district.id}" th:value="${d.id}" th:text="${d.name}"></option>
                </select>
            </div>
            <button type="submit" class="btn btn-default">Save</button>

在提交表单时,我从输入和空区中获取街道名称。

您的错误在选择中

属性

th:field=“*{district.id}”

应在“选择”中,而不是在“选项”标记中

结果:

 <select class="form-control" id="district" th:field="*{district.id}">
                    <option th:each="d : ${districts}"  th:value="${d.id}" th:text="${d.name}"></option>
                </select>

 <select class="form-control" id="district" th:field="*{district.id}">
                    <option th:each="d : ${districts}"  th:value="${d.id}" th:text="${d.name}"></option>
                </select>