0
我試圖設置驗證自定義錯誤,當錯誤輸入beeing通過。驗證錯誤,同時填寫表格
這是我的形式thymeleaf模板的幫助下寫成:
<form th:action="@{/addPet}" th:object="${petDto}" method="post">
<table>
<tr>
<td>Pet name:</td>
<td><input type="text" th:field="*{pet.petName}" /></td>
<td th:if="${#fields.hasErrors('pet.petName')}" th:errors="*{pet.petName}">fieldError</td>
</tr>
<tr>
<td>Owner First name:</td>
<td><input type="text" th:field="*{owner.firstName}" /></td>
<td th:if="${#fields.hasErrors('owner.firstName')}" th:errors="*{owner.firstName}">fieldError</td>
</tr>
<tr>
<td>Owner Last name:</td>
<td><input type="text" th:field="*{owner.lastName}" /></td>
<td th:if="${#fields.hasErrors('owner.lastName')}" th:errors="*{owner.lastName">fieldError</td>
</tr>
<tr>
<td>Owner Number:</td>
<td><input type="text" th:field="*{owner.phoneNumber}" /></td>
<td th:if="${#fields.hasErrors('owner.phoneNumber')}" th:errors="*{owner.phoneNumber}">fieldError</td>
</tr>
<tr>
<td><input type="submit" value="add" /></td>
</tr>
</table>
所以有DTO通過包含兩個領域:
public class PetDto {
private Pet pet;
private Owner owner;
現在,當我輸入錯誤輸入我得到這個:
here was an unexpected error (type=Internal Server Error, status=500).
Validation failed for classes [pl.kaczynski.model.Owner] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='Length must be between 2 and 15 characters.', propertyPath=lastName, rootBeanClass=class pl.kaczynski.model.Owner, messageTemplate='{Size}'} ConstraintViolationImpl{interpolatedMessage='Length must be between 2 and 15 characters.', propertyPath=firstName, rootBeanClass=class pl.kaczynski.model.Owner, messageTemplate='{Size}'} ConstraintViolationImpl{interpolatedMessage='numeric value out of bounds (<9 digits>.<0 digits> expected)', propertyPath=phoneNumber, rootBeanClass=class pl.kaczynski.model.Owner, messageTemplate='{javax.validation.constraints.Digits.message}'} ]
而我的目標是有錯誤信息顯示旁邊的輸入字段相同的觀點,不重定向到錯誤頁面。我爲對象Pet和Owner創建了類似的表單頁面,並且它工作正常,這使我認爲我的構圖類DTO存在問題。
編輯// 這是模型類:
@Entity
public class Pet extends BaseClass{
@Column(name = "pet_name")
@Size(min=2,max=15,message="{Size}")
private String petName;
@OneToOne()
@JoinColumn(name = "owner_Id")
private Owner owner;
@Entity
public class Owner extends BaseClass {
@Column(name = "first_name")
@Size(min = 2, max = 15, message = "{Size}")
private String firstName;
@Column(name = "last_name")
@Size(min = 2, max = 15, message = "{Size}")
private String lastName;
@Column(name = "phone")
@Digits(fraction = 0, integer = 9)
private String phoneNumber;
這裏是控制器:如果你有幾個錯誤信息
<tr>
<td>Pet name:</td>
<td><input type="text" th:field="*{pet.petName}" /></td>
<span th:if="${#fields.hasErrors('pet.petName')}">
<ul>
<li th:each="err : ${#fields.errors('pet.firstName')}" th:text="${err}">error text</li>
<ul>
</span>
</tr>
:
@RequestMapping(value = "/addPet", method = RequestMethod.POST)
public String addPetPost(@Valid @ModelAttribute(value="petDto") PetDto petDto, BindingResult result){
if(result.hasErrors())
return "/addPet";
Owner owner = petDto.getOwner();
Pet pet = petDto.getPet();
pet.setOwner(owner);
ownerService.add(owner);
petService.add(pet);
return "redirect:addPet";
}
這是行不通的,我認爲這個問題可能與BindingResult結果對象有關,因爲我調試了它,並且if(result.hasErrors())不是真的,並且流程更進一步,所以白色標籤頁錯誤從spring開機啓動。因此,目標是讓結果看到錯誤 – filemonczyk
,然後發佈更多的代碼。 – DimaSan
你想要什麼? – filemonczyk