我为此表单对象解析创建了一个DTO:
public class UserDTO {
//todo hardcoded
@NotNull(message = "Ad gerekiyor")
private String firstname;
@NotNull(message = "Soyad gerekiyor")
private String lastname;
@NotNull(message = "tc cuzdan no gerekiyor")
private String tcCuzdanNo;
@NotNull(message = "tc cuzdan seri gerekiyor")
private String tcCuzdanSeri;
@NotNull(message = "dogum gunu gerekiyor")
@Temporal(TemporalType.DATE)
private Date birthdate;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getTcCuzdanNo() {
return tcCuzdanNo;
}
public void setTcCuzdanNo(String tcCuzdanNo) {
this.tcCuzdanNo = tcCuzdanNo;
}
public String getTcCuzdanSeri() {
return tcCuzdanSeri;
}
public void setTcCuzdanSeri(String tcCuzdanSeri) {
this.tcCuzdanSeri = tcCuzdanSeri;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
}
这是表格:
<form id="formAccount" method="post" name="formAccount" th:action="@{/account-update}"
th:object="${user}"> //tried this later
//i am only putting one field
<input class="form-control" id="birthdate" name="birthdate"
th:value="${birthdate}" type="date">
<input type="submit" value="Submit" />
</form>
这是端点:
@PostMapping(value = "/account-update")
public String submit(
@Valid @ModelAttribute("user") UserDTO user
当我单击提交时,它将转到getbirthdate
类的userdto
方法,并显示 null 。我在调试中看到,所有字段均为 null 。
但是在浏览器上,我可以看到这样的表单数据:
firstname=xx&lastname=qq&tcCuzdanSeri=&tcCuzdanNo=&birthdate=
我也尝试过代替th:object="${user}"
modelAttribute="user"
但仍然相同。
字段“生日”上的对象“用户”中的字段错误:拒绝的值[]; 码 [typeMismatch.user.birthdate,typeMismatch.birthdate,typeMismatch.java.util.Date,typeMismatch]; 论点 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码[user.birthdate,birthdate];参数[];预设讯息 [生日]];默认消息[无法转换属性值 将'java.lang.String'键入为属性的必需类型'java.util.Date' '生日';嵌套异常为 org.springframework.core.convert.ConversionFailedException:失败 从类型[java.lang.String]转换为类型 [@ javax.validation.constraints.NotNull @ javax.persistence.Temporal java.util.Date]的值“;嵌套异常为 java.lang.IllegalArgumentException]]
我不认为这与字符串迄今为止的错误有关,因为我也尝试将此作为生日:
th:value="${#dates.format(birthdate, 'yyyy-MM-dd')}"
,尽管其他字段不为空,但它们对端点也为null。而且notnull
注释不起作用,我看不到我在userdto
类上设置的消息。