How to use input radio button with thymeleaf and Spring MVC

放肆的年华 提交于 2021-01-29 14:34:13

问题


I would like to get a destination address from a input radio button list. The DestinationAddress class is the following:

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

}

The controller who handles the get and post is the following:

@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();
    }

And the html page is the following:

<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>

The error message is the following:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'destinationAddressId' available as request attribute

I don't know what's the problem here. I don't know which type will the form return to the controller. So I don't know which variable pass to the model and which one to get from the post controller method. Integer or DestinationAddress? I cannot find anything googling it, just small pieces of code without any explanations. Any suggestions?


回答1:


I found a solution to my problem. I changed the html page, now it looks like this:

<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>

I changed the name of the object inside the model because it was the same as the name of the temp destinationAddress of the loop. I also replaced '{#ids.prev(' because it was giving me an error:

Cannot obtain previous ID count for ID ...

Now it works fine.



来源:https://stackoverflow.com/questions/53951501/how-to-use-input-radio-button-with-thymeleaf-and-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!