Spring mvc 如何在thymeleaf和Spring MVC中使用输入单选按钮

Spring mvc 如何在thymeleaf和Spring MVC中使用输入单选按钮,spring-mvc,radio-button,thymeleaf,Spring Mvc,Radio Button,Thymeleaf,我想从输入单选按钮列表中获取目的地地址。DestinationAddress类如下所示: public class DestinationAddress { private Integer destinationAddressId; private String name; private Location location; private User owner; public DestinationAddress(String name,

我想从输入单选按钮列表中获取目的地地址。DestinationAddress类如下所示:

public class DestinationAddress {

    private Integer destinationAddressId;

    private String name;

    private Location location;

    private User owner;


    public DestinationAddress(String name, Location location, User owner) {
        this.name = name;
        this.location = location;
        this.owner = owner;
    }

    public DestinationAddress() {
    }
// getter and setter

}
@PreAuthorize("hasRole('USER')")
    @GetMapping(value = "/select-address")
    public String selectAddress(Principal principal, Model model) {
        List<DestinationAddress> addresses = destinationAddressService.getAllByUsername(principal.getName());
        model.addAttribute("destinationAddresses", addresses);
        model.addAttribute("destinationAddress", new DestinationAddress());
        return "purchase/select-address";
    }


    @PreAuthorize("hasRole('USER')")
    @PostMapping(value = "/select-address")
    public String selectAddress(@ModelAttribute DestinationAddress destinationAddress, Principal principal) {
        Purchase purchase = purchaseService.addPurchase(principal.getName(), destinationAddress);
        return "redirect:/purchases/pay/" + purchase.getPurchaseId();
    }
<form th:object="${destinationAddress}" method="post">
    <fieldset>
    <legend>Your addresses</legend>
    <ul>
        <li th:each="destinationAddress : ${destinationAddresses}">
            <input type="radio" th:field="*{destinationAddressId}" th:value="${destinationAddress.destinationAddressId}" />
            <label th:for="${#ids.prev('destinationAddress.destinationAddressId')}" th:text="${destinationAddress}"></label>
        </li>
    </ul>
</fieldset>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'destinationAddressId' available as request attribute
处理get和post的控制器如下所示:

public class DestinationAddress {

    private Integer destinationAddressId;

    private String name;

    private Location location;

    private User owner;


    public DestinationAddress(String name, Location location, User owner) {
        this.name = name;
        this.location = location;
        this.owner = owner;
    }

    public DestinationAddress() {
    }
// getter and setter

}
@PreAuthorize("hasRole('USER')")
    @GetMapping(value = "/select-address")
    public String selectAddress(Principal principal, Model model) {
        List<DestinationAddress> addresses = destinationAddressService.getAllByUsername(principal.getName());
        model.addAttribute("destinationAddresses", addresses);
        model.addAttribute("destinationAddress", new DestinationAddress());
        return "purchase/select-address";
    }


    @PreAuthorize("hasRole('USER')")
    @PostMapping(value = "/select-address")
    public String selectAddress(@ModelAttribute DestinationAddress destinationAddress, Principal principal) {
        Purchase purchase = purchaseService.addPurchase(principal.getName(), destinationAddress);
        return "redirect:/purchases/pay/" + purchase.getPurchaseId();
    }
<form th:object="${destinationAddress}" method="post">
    <fieldset>
    <legend>Your addresses</legend>
    <ul>
        <li th:each="destinationAddress : ${destinationAddresses}">
            <input type="radio" th:field="*{destinationAddressId}" th:value="${destinationAddress.destinationAddressId}" />
            <label th:for="${#ids.prev('destinationAddress.destinationAddressId')}" th:text="${destinationAddress}"></label>
        </li>
    </ul>
</fieldset>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'destinationAddressId' available as request attribute

我不知道这里有什么问题。我不知道表单将返回到控制器的类型。所以我不知道哪个变量传递给模型,哪个变量从post控制器方法中得到。整数还是目标地址?我在谷歌上找不到任何东西,只是一些没有任何解释的小代码。有什么建议吗

我找到了解决问题的办法。我更改了html页面,现在看起来如下:

<form th:object="${address}" method="post">
    <fieldset>
        <legend>Your addresses</legend>
        <ul>
            <li th:each="destinationAddress : ${destinationAddresses}">
                <input type="radio" th:field="${address.destinationAddressId}" th:value="${destinationAddress.destinationAddressId}" />
                <label th:for="${destinationAddress.destinationAddressId}" th:text="${destinationAddress}"></label>
            </li>
        </ul>
    </fieldset>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

现在工作正常。

谢谢您的更新。它帮助我完成了我的上传表单:)