Spring注解:使用百里香叶对Bean内部对象属性的形式验证 [英] Spring annotations : form validation of a bean internal object attribute using thymeleaf
问题描述
在Thymeleaf中有没有一种方法可以验证bean的对象属性中的属性? 考虑我们确实有一个Departement类,如下所示:
is there a way in Thymeleaf to validate an attribute in object property of a bean? Consider that we do have a Departement class as below :
public class Departement {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idDept;
@NotEmpty
private String name;
}
以及另一个如下的Employee类
And another Employee class as follow
public class Employee{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idEmp;
@NotEmpty
@Size(min = 5, message="At least five characters needed")
private String employeeName;
@NotNull
private Departement departement;
}
在上面的代码中,以雇员形式使用百里香叶,由于注释,只有'employeeName'会在spring之前得到验证. 让我们在这里看看 在我的控制器中
With the code above using thymeleaf in the employee form only 'employeeName' will be validated by spring because of annotations. Let's take a look here In my controller
@GetMapping( value = "/emp" )
public String save(Model model){
Employee emp = new Employee();
emp.setDepartement(new Departement());
model.addAttribute('employee', emp);
return 'view';
}
//------------- Form in PostMapping
@PostMapping( value = "/save", @Valid Emp emp, BindingResult bindingResult )
public String savePost(Model model){
if( ! bindingResult.hasErrors() )
{
/* Even if departement has not been choosen, my code always goes here
and print "Form Ok. Departement : 0" instead of reaching the 'else' block, but if departement choosen,
it prints the correct value of departemnt
*/
System.out.println( "Form Ok.\n Departement : " + emp.getDepartement().getIdDept() );
}else{
System.out.println( "Missing attributes." );
}
return 'view';
}
这是员工表格
<form th:action="@{save}" th:object="${emp}" th:method="POST" >
<span th:if="${#fields.hasErrors('employeeName') }"th:errors="*{employeeName}"></span>
<input th:field="*{employeeName}" th:value="${employeeName}" />
//--------
<div th:object="${emp.departement}">
<span th:if="${#fields.hasErrors('idDept') }"th:errors="*{idDept}"></span>
<input th:field="*{idDept}" th:value="${idDept}" />
</div>
</form>
**这是我的问题: 如何在不使用emplpoyee表单中的javacript的情况下验证员工部门标识符(idDept字段)? **
**Here is my question : How can I validate the employee departement identifier (idDept field) without using javacript in the emplpoyee form? **
NB:我不使用drowpdownlist来显示部门,而是希望使用具有所选部门ID的自动完成字段和隐藏字段.
推荐答案
JSR-303强制使用@Valid
批注来递归验证嵌套组件,如
JSR-303 mandates the use of a @Valid
annotation to recursively validate nested components as mentioned in the Hibernate Validator docs.
因此,只需将@Valid
放在您的嵌套组件上,在您的情况下,放在雇员类的部门字段中即可:
So just put @Valid
on your nested components, in your case on the department field within the employee class:
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idEmp;
@NotEmpty
@Size(min = 5, message="At least five characters needed")
private String employeeName;
@NotNull
@Valid
private Departement departement;
}
这篇关于Spring注解:使用百里香叶对Bean内部对象属性的形式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!