如何从表单中传输对象以及其他对象列表?

时间:2018-08-11 10:36:28

标签: java spring-mvc spring-data-jpa thymeleaf

我是百里香和春天的新手。有一个Person类,其中包含字段Set <'Addtess>和Set <'PhoneNumber>。编辑后,必须保存更改的数据。如何从表单转换为具有以上字段的控制器对象Person?我的控制器和表单不想保存集。

表格:

<form action="/personData/save" method="post" >
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
    <input type="text" name="id" th:value="${person.getId()}">
    <input type="text" name="lastName" th:value="${person.getLastName()}"/>
    <input type="text" name="firstName" th:value="${person.getFirstName()}">
    <input type="text" name="patronymic" th:value="${person.getPatronymic()}">
    <p th:each="phone:${person.getPhoneNumbers()}">
        <input type="text" name="phoneNumber" th:value="${phone.getType()}">
        <input type="text" name="phoneType" th:value="${phone.getNumber()}">
    </p>
    <p th:each="address:${person.getAddresses()}">
        <input type="text" name="houseNumber" th:value="${address.getHouseNumber()}">
        <input type="text" name="street" th:value="${address.getStreet()}">
        <input type="text" name="city" th:value="${address.getZipCode()}">
        <input type="text" name="region" th:value="${address.getCity()}">
        <input type="text" name="country" th:value="${address.getCountry()}">
        <input type="text" name="zipCode" th:value="${address.getZipCode()}">
    </p>
    <p>
    <button type="submit">Сохранить</button>

控制器:

@RequestMapping(value = "/save", method = RequestMethod.POST)
    public String personSave(@ModelAttribute Person person,
                             Model model) {
        personRepo.save(person);
        model.addAttribute("persons", personRepo.findAll());
        return "main";
    }

0 个答案:

没有答案