繁体   English   中英

如何从thymeleaf文本框调用带有两个参数的构造函数?

[英]How can I call a constructor with two parameters from thymeleaf textbox?

我有一个文本框

<input type="text" class="form-control" id="name" th:value="*{name}" th:field="*{name}"/>

还有一个实体Costumer,它有两个构造函数

Costumer(String name)
Costumer(Locale locale, String name)

在上面的文本框中,只有第一个构造函数被调用! 如何调用具有两个参数的构造函数,以及如何将语言环境传递给它?

在您的位置,我将使用表格。

测试表格:

public class TestForm {

    private String name;
    private String locale;

    public TestForm() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    @Override
    public String toString() {
        return "TestForm [name=" + name + ", locale=" + locale + "]";
    }

}

控制器:

@RequestMapping(value = "/test", method = GET)
public String test(final ModelMap modelMap) {
    modelMap.put("testForm", new TestForm());
return "/test";
}

@RequestMapping(value = "/test", method = POST)
public String posttest(@ModelAttribute("testForm") final TestForm testForm) {
    System.out.println(testForm);
return "redirect:/somewhere/else";
}

test.html:

    <form th:object="${testForm}" method="post">
        <input type="text" class="form-control" id="name" th:value="*{name}" th:field="*{name}"/>
        <input type="text" class="form-control" id="locale" th:value="*{locale}" th:field="*{locale}"/>

        <button type="submit">Save</button>
    </form>

暂无
暂无

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

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