在春靴百里香中使用枚举

问题描述 投票:0回答:0

我是初学者后端开发人员。我正在构建一个应用程序,使用 Spring、Hibernate,当然还有 Thymeleaf 来获取视图。我遇到了这样的问题。我想将数据从 edit.html 发送到另一个类。这是我的页面。请帮助我。

edit.html(这里是问题所在)。我想在 PhoneList 对象中放入一个枚举并将其发送到控制器

                <div class="form-group"  th:each="phoneType, itemStat : ${form.phoneTypeList}">
                    <select name="type">
                        <option th:each="typeOpt : ${T(ru.murik.phone_book3.models.Type).values()}"
                                th:value="${typeOpt}"
                                th:text="${typeOpt}"
                                th:selected="${phoneType.getType()} == ${typeOpt.toString()}"
                                th:field="*{phoneType[__${itemStat.index}__].type}" >
                        </option>
                    </select>
                </div>
                <br>
                <br>
                <button type="submit" class="btn btn-primary">Update</button>
            </div>

电话列表

public class PhoneList {
    private List<Phone> phones;
    private List<PhoneType> phoneTypeList;

    public PhoneList(List<Phone> phones, List<PhoneType> phoneTypeList) {
        this.phones = phones;
        this.phoneTypeList = phoneTypeList;
    }

    public PhoneList() {
    }

    public List<Phone> getPhones() {
        return phones;
    }

    public void setPhones(List<Phone> phones) {
        this.phones = phones;
    }

    public List<PhoneType> getPhoneTypeList() {
        return phoneTypeList;
    }

    public void setPhoneTypeList(List<PhoneType> phoneTypeList) {
        this.phoneTypeList = phoneTypeList;
    }
}

控制器

    @GetMapping("/{id}/edit")
    public ModelAndView edit(@PathVariable("id")long id) {
        Contact contact = contactService.get(id);
        ModelAndView modelAndView = new ModelAndView("edit.html");
        List<PhoneType>typeList = new ArrayList<>();
        contact.getPhones().forEach(phone -> typeList.add(phone.getPhoneType()));
        PhoneList phoneList = new PhoneList(contact.getPhones(), typeList);



        modelAndView.addObject("contact", contact);
        modelAndView.addObject("form", phoneList);
        return modelAndView;
    }

    @PostMapping("/{id}")
    public ModelAndView update(@PathVariable("id")long id,
                               @ModelAttribute("contact")Contact contact,
                               @ModelAttribute("phoneList")PhoneList phoneList){
        ModelAndView modelAndView = new ModelAndView("redirect:/contact");

        for (PhoneType phoneType : phoneList.getPhoneTypeList()) {
            System.out.println(phoneType.getType());
        }

        Contact contact1 = contactService.get(id);

        for (int i = 0; i < phoneList.getPhones().size(); i++) {
            contact1.getPhones().get(i).setPhoneNumber(phoneList.getPhones().get(i).getPhoneNumber());
        }

        contact1.setName(contact.getName());
        contact1.setSurname(contact.getSurname());


        contactService.save(contact1);
        return modelAndView;
    }
java spring-boot thymeleaf
© www.soinside.com 2019 - 2024. All rights reserved.