java - Spring MVC + Thymeleaf - 保存关系@ManyToOne

标签 java spring forms spring-mvc thymeleaf

我正在开发一个用于管理客户端及其机器的应用程序。 我已经创建了所有必要的表、架构、 Controller 等。

客户端实体具有列表,机器具有客户端关系(双向,非可选)。

我遇到的问题与向现有客户端添加全新机器有关(前提是该机器已经存在)。

这里是一些简短的代码片段:

@Controller
public class MachineController {

...
    @GetMapping("/machines/add/{clientId}")
    public String addMachine(@PathVariable("clientId") int clientId, Model model) throws ClientNotFoundException {
        model.addAttribute("machineTypes", MachineType.values());
        model.addAttribute("machine", new Machine());
        model.addAttribute("client", clientService.find(clientId));
        return "machines/form";
    }
}

    @PostMapping("/machines/save")
    public String saveMachine(@ModelAttribute @Valid Machine machine, BindingResult bindingResult, Model model)
            throws ClientNotFoundException {

        model.addAttribute("machineTypes", MachineType.values());

        int clientId = machine.getClient().getId();
        LOG.debug("ClientId:{}", clientId);
        // Client object is not filled here ! clientId is 0 (new client).
}

问题在于保存功能 - 我不知道如何将现有的客户端对象传递给机器对象,该对象是通过 HTTP POST 发送的。 我的 Controller 提示客户端未发送并且 BindingResult 抛出错误:

Field error in object 'machine' on field 'client.address.city: rejected value [null]; Field error in object 'machine' on field 'client.address.zipCode: rejected value [null]; Field error in object 'machine' on field 'client.name': rejected value [null];

我期待任何帮助。

HTML 表单如下所示:

                                <form class="form-horizontal" th:action="@{/machines/save}" th:object="${machine}" method="post" id="machineForm">
                                <input type="hidden" th:field="*{id}"/>

                                    <!-- Panel for machine -->
                                <div th:class="${#fields.hasErrors('machineType')}? 'form-group has-error has-feedback' : 'form-group'">
                                    <label class="control-label col-sm-4" for="manufacturer">Rodzaj:*</label>
                                    <div class="col-sm-8">

                                        <select th:field="${machine.machineType}" class="form-control" id="machineTypeSelect">
                                            <option value="" disabled="disabled" selected="selected">Wybierz rodzaj</option>
                                            <option th:each="type: ${machineTypes}" th:value="${type.name()}" th:text="${type}" th:attr="data-has-car=${type.hasCar()}"></option>
                                        </select>

                                        <div class="help-block" th:if="${#fields.hasErrors('machineType')}"
                                             th:errors="*{machineType}"></div>
                                    </div>
                                </div>

                                <div th:class="${#fields.hasErrors('manufacturer')}? 'form-group has-error has-feedback' : 'form-group'">
                                    <label class="control-label col-sm-4" for="manufacturer">Producent:*</label>
                                    <div class="col-sm-8">
                                        <input type="text"
                                               class="form-control"
                                               id="manufacturer"
                                               placeholder="Podaj producenta"
                                               th:field="*{manufacturer}" />
                                        <div class="help-block" th:if="${#fields.hasErrors('manufacturer')}"
                                             th:errors="*{manufacturer}"></div>
                                    </div>
                                </div>

                                <div th:class="${#fields.hasErrors('model')}? 'form-group has-error has-feedback' : 'form-group'">
                                    <label class="control-label col-sm-4" for="model">Model:</label>
                                    <div class="col-sm-8">
                                        <input type="text"
                                               class="form-control"
                                               id="model"
                                               placeholder="Podaj model"
                                               th:field="*{model}"/>
                                        <div class="help-block" th:if="${#fields.hasErrors('model')}"
                                             th:errors="*{model}"></div>
                                    </div>
                                </div>

                                <div th:class="${#fields.hasErrors('productionYear')}? 'form-group has-error has-feedback' : 'form-group'">
                                    <label class="control-label col-sm-4" for="productionYear">Rok produkcji:*</label>
                                    <div class="col-sm-8">
                                        <input type="number"
                                               class="form-control"
                                               id="productionYear"
                                               placeholder="Podaj rok produkcji"
                                               th:field="*{productionYear}"/>
                                        <div class="help-block" th:if="${#fields.hasErrors('productionYear')}"
                                             th:errors="*{productionYear}"></div>
                                    </div>
                                </div>

                                <div th:class="${#fields.hasErrors('factoryNo')}? 'form-group has-error has-feedback' : 'form-group'">
                                    <label class="control-label col-sm-4" for="factoryNo">Numer fabryczny:</label>
                                    <div class="col-sm-8">
                                            <input type="number"
                                                   class="form-control"
                                                   id="factoryNo"
                                                   placeholder="Podaj numer fabryczny"
                                                   th:field="*{factoryNo}"/>
                                        <div class="help-block" th:if="${#fields.hasErrors('factoryNo')}"
                                             th:errors="*{factoryNo}"></div>
                                    </div>
                                </div>

                                <div th:class="${#fields.hasErrors('maxLoad')}? 'form-group has-error has-feedback' : 'form-group'">
                                    <label class="control-label col-sm-4" for="maxLoad">Max udżwig:</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                        <input type="number"
                                               class="form-control"
                                               id="maxLoad"
                                               placeholder="Max. udźwig"
                                               aria-describedby="measure"
                                               th:field="*{maxLoad}"/>
                                            <span class="input-group-addon" id="measure">kg</span>
                                        </div>
                                        <div class="help-block" th:if="${#fields.hasErrors('maxLoad')}"
                                             th:errors="*{maxLoad}"></div>
                                    </div>
                                </div>

                                <div th:object="${machine.client}">
                                    <div th:class="${#fields.hasErrors('id')}? 'form-group has-error has-feedback' : 'form-group'">
                                        <label class="control-label col-sm-4" for="clientId">Wybrany klient:</label>
                                        <div class="col-sm-8">
                                            <span class="form-control-static" id="selectedClient">Nie wybrano! Wyszukaj po prawej</span>
                                            <input type="hidden" th:field="${machine.client.id}" id="clientId"  />
                                            <div class="help-block" th:if="${#fields.hasErrors('id')}"
                                                 th:errors="${machine.client.id}"></div>
                                        </div>
                                    </div>
                                </div>

                                <div id="machineCar" th:object="${machine.car}">

                                    <div th:class="${#fields.hasErrors('make')}? 'form-group has-error has-feedback' : 'form-group'">
                                        <label class="control-label col-sm-4" for="carMake">Marka pojazdu:*</label>
                                        <div class="col-sm-8">
                                            <input type="text"
                                                   class="form-control"
                                                   id="carMake"
                                                   placeholder="Podaj markę pojazdu"
                                                   th:field="*{make}"/>
                                            <div class="help-block" th:if="${#fields.hasErrors('make')}"
                                                 th:errors="*{make}"></div>
                                        </div>
                                    </div>

                                    <div th:class="${#fields.hasErrors('vin')}? 'form-group has-error has-feedback' : 'form-group'">
                                        <label class="control-label col-sm-4" for="factoryNo">VIN:</label>
                                        <div class="col-sm-8">
                                            <input type="number"
                                                   class="form-control"
                                                   id="vin"
                                                   placeholder="Podaj numer VIN"
                                                   th:field="*{vin}"/>
                                            <div class="help-block" th:if="${#fields.hasErrors('vin')}"
                                                 th:errors="*{vin}"></div>
                                        </div>
                                    </div>
                                </div>
                                    <div class="form-group">
                                        <div class="col-xs-12">
                                            <button type="submit" class="btn btn-primary">Zapisz dane</button>
                                        </div>
                                    </div>
                                </form>

最佳答案

尝试添加

    <input type="hidden" name="client.id" value="${client.id}" />

在 HTML 表单中,那些具有 id 值的客户端对象将被创建,然后将其余的留给存储库,它只需要 id 来关联记录。

关于java - Spring MVC + Thymeleaf - 保存关系@ManyToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42270489/

相关文章:

java - 尝试在空对象引用上调用虚拟方法

java - 我不知道如何在停止时显示计时器

java - 在 Spring 中定义 @ManyToOne 关系后,如何访问底层列?

java - Spring MessageSource 多次解析/插入字符串

ruby-on-rails - Rails 4嵌套属性和has_many:通过表单关联

java - Shiro 自定义 JDBC 领域

java 。 GUI WindowBuilder 通过单击按钮从 JTextField 读取

java - Hibernate/Spring 4 升级后,ClassCastException Proxy36 无法转换为 SessionImplementor

javascript - 如何使用 Jasmine 测试表单

servlets - 如何在servlet中获取表单参数? request.getAttribute 不起作用