我尝试用不同类型的对象构建表单。
像String或Integer这样的简单对象,使用HTML中的thymeleaf“选择”标签都不会出现问题,但是当我想获取一些更复杂的对象时,在请求后总是得到null对象。
这是我的代码:
表格:
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class PersonForm {
@NotNull
@Size(min=2, max=30)
private String name;
@NotNull
@Min(18)
private Integer age;
private String type;
@NotNull
private Address address;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String toString() {
return "Person(Name: " + this.name + ", Age: " + this.age + ")";
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
地址:
public class Address {
String city;
Integer code;
public Address(String city, Integer code) {
this.city = city;
this.code = code;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
Controller:
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
@Controller
public class PersonController implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/results").setViewName("results");
}
@GetMapping("/")
public String showForm(PersonForm personForm, Model model) {
List<String> types = new ArrayList<>();
types.add("Male");
types.add("Female");
types.add("Alian");
types.add("Non Human");
List<Address> addresses = new ArrayList<>();
addresses.add(new Address("Kyiv",01000));
addresses.add(new Address("Hayvoron",26300));
model.addAttribute("cities",addresses);
model.addAttribute("allTypes",types);
return "form";
}
@PostMapping("/")
public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult) {
PersonForm person = personForm;
if (bindingResult.hasErrors()) {
return "form";
}
return "redirect:/results";
}
}
一个简单的HTML文件,带有两个下拉列表,带有“字符串类型”参数的列表效果很好,但是参数“地址地址”却无法正常工作。
<html xmlns:th="http://www.thymeleaf.org">
<body>
<!--/*@thymesVar id="personForm" type="com.yurets_y.weblibrary.entity.PersonForm"*/-->
<form action="#" th:action="@{/}" th:object="${personForm}" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{name}" /></td>
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" th:field="*{age}" /></td>
<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</td>
</tr>
<tr>
<td>
Gender
</td>
<td>
<select th:field="*{type}">
<option th:each="type : ${allTypes}"
th:value="${type}"
th:text="${type}">Wireframe</option>
</select>
</td>
</tr>
<tr>
<td>
Address
</td>
<td>
<select th:field="*{address}">
<option th:each="city : ${cities}"
th:value="city"
th:text="${city.city}"
>Address</option>
</select>
</td>
<!--</tr>-->
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
</body>
</html>
有什么方法可以选择“选择”形式的复杂对象?
答案 0 :(得分:1)
这是不可能的,因为Thymeleaf将调用.toString()方法来设置值。
您可以检查元素。
因此,最好的策略是为Controller返回元素的ID。
<select th:field="*{addressId}">
<option th:each="city : ${cities}"
th:value="${city.code}"
th:text="${city.city}">
Address
</option>
</select>