如何在 thymeleaf 和 Spring MVC 中使用输入单选按钮 [英] How to use input radio button with thymeleaf and Spring MVC

查看:92
本文介绍了如何在 thymeleaf 和 Spring MVC 中使用输入单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

处理get和post的控制器如下:

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

html页面如下:

<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 控制器方法中获取.整数还是目标地址?我找不到任何谷歌搜索,只有一小段代码,没有任何解释.有什么建议么?

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?

推荐答案

我找到了解决问题的方法.我更改了 html 页面,现在看起来像这样:

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>

我更改了模型内部对象的名称,因为它与循环的临时目标地址的名称相同.我还替换了 '{#ids.prev(' 因为它给了我一个错误:

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

现在一切正常.

这篇关于如何在 thymeleaf 和 Spring MVC 中使用输入单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆