【问题标题】:How can I call a constructor with two parameters from thymeleaf textbox?如何从 thymeleaf 文本框中调用带有两个参数的构造函数?
【发布时间】:2015-05-16 18:08:17
【问题描述】:
我有一个文本字段
<input type="text" class="form-control" id="name" th:value="*{name}" th:field="*{name}"/>
和一个有两个构造函数的实体 Costumer
Costumer(String name)
Costumer(Locale locale, String name)
使用上面的文本框,只有第一个构造函数被调用!如何调用具有两个参数的构造函数,以及如何将语言环境传递给它?
【问题讨论】:
标签:
java
spring
thymeleaf
【解决方案1】:
在你的位置我会使用表单。
测试表格:
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>