简体   繁体   English

Thymeleaf-Spring MVC。 带有boolean和复选框的表单支持bean

[英]Thymeleaf - Spring MVC. Form backing bean with boolean and checkboxes

Iam trying post a simple form to a spring controller using a thymeleaf. Iam尝试使用百里香叶将简单的表格发布到弹簧控制器。 The backing bean includes a boolean value which is mapped to a checkbox in the template using th:field inside of a th:object tag. 支持bean包含一个布尔值,该布尔值使用th:object标记内的th:field映射到模板中的复选框。 When I take a look at the rendered html DOM, spring mvc is adding an hidden input field, where the name is _attributeName. 当我查看呈现的html DOM时,spring mvc添加了一个隐藏的输入字段,其名称为_attributeName。 The name of the main input field is generated as attributeName. 主输入字段的名称将作为attributeName生成。 Now when Iam trying to post the form it aborts with a 400 because the request parameter _attributeName cant be mapped to a backing bean object (simply doesnt exist). 现在,当Iam尝试发布表单时,它将中止400,因为请求参数_attributeName无法映射到支持bean对象(根本不存在)。 So in addition the request wohld include attributeName as well as _attributeName. 因此,除了请求之外,请求还包括attributeName和_attributeName。 Why is this happening? 为什么会这样呢?

So i finally solved it. 所以我终于解决了。 The Problem was, that I used boolean instead of Boolean and my getter where named like isEnabled instead of getEnabled. 问题是,我使用布尔值而不是布尔值,而我的getter的名称类似于isEnabled而不是getEnabled。 This seemed to cause the problem with the mapping between the form element and the backing bean. 这似乎引起了表单元素和支持bean之间的映射问题。

Tried solution mentioned by Vinc, but didn't work for me. 尝试了Vinc提及的解决方案,但对我不起作用。 However below worked for me. 但是下面为我工作。

Controller 调节器

@Controller
public class BaseController {

    @GetMapping("/")
    private String index(DemoDto demoDto){
        return "index";
    }

    @PostMapping("/")
    private String receiveValues(DemoDto demoDto) {
        System.out.println(demoDto);
        return "index";
    }

}

DTO DTO

public class DemoDto {
    private String name;
    private boolean global;

    //getter setter for name

    public boolean isGlobal() {
        return global;
    }
    public void setGlobal(boolean global) {
        this.global = global;
    }

    //toString()
}

HTML HTML

<body>
    <form th:action="@{/}" th:method="post" th:object="${demoDto}">
        <label>Enter Name:</label> 
            <input type="text" th:field="*{name}" name="name"> 
        <br/>
        <label>Global</label>
            <input type="checkbox" th:field="${demoDto.global}"/>
        <input type="submit" value="Submit">
    </form>

</body>

Here most important is how you define th:field="${demoDto.global}" . 这里最重要的是如何定义th:field="${demoDto.global}" Both $ as well as object name demoDto are required here. $和对象名称demoDto都是必需的。

Generated html code will be. 将生成html代码。

<body>
    <form action="/" method="post">
        <label>Enter Name:</label> 
            <input type="text" name="name" id="name" value=""> 
        <br/>
        <label>Global</label>
            <input type="checkbox" id="global1" name="global" value="true"/>
            <input type="hidden" name="_global" value="on"/>
        <input type="submit" value="Submit">
    </form>

</body>

When submitted from ui received: 从ui提交时收到:

DemoDto [name=Dev, global=true]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM