我正在尝试通过在Validator
- Bean中注入CDI
来尝试Bean验证。
这就是为什么我编写了一个注入validator
属性的servlet。问题是我得到了错误的结果。例如,它表示name
和surname
属性不允许为空,尽管我已使用常规名称和姓氏设置它们。
我做错了吗?
这是我的Servlet:
@WebServlet
public class BeanValidationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
private Validator validator;
@Inject
private WorkEmployee workEmployee;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
workEmployee.getAdresse().setPostalCode("8888");
workEmployee.getAdresse().setStreet("Washington Street");
workEmployee.getAdresse().setStreetNumber(98);
workEmployee.getAdresse().setHome("Philadeplphia");
workEmployee.setEmployeeId(12);
workEmployee.setName("John");
workEmployee.setSurname("Doeuuu");
writer.print("<h1> Surname:" + workEmployee.getSurname() + "<h1>");
Set<ConstraintViolation<WorkEmployee>> constraintViolations = validator
.validate(workEmployee);
for (ConstraintViolation<WorkEmployee> violation : constraintViolations) {
writer.print("<h1>" + violation.getPropertyPath() + ": "
+ violation.getMessage() + "<h1>");
writer.print("<h1>" + violation.getRootBean() + "<h1>");
writer.print("<h1>-------------------------------<h1>");
}
Set<ConstraintViolation<Adress>> constraintViolations2 = validator
.validate(workEmployee.getAdresse());
for (ConstraintViolation<Adress> violation : constraintViolations2) {
writer.print("<h1>" + violation.getPropertyPath() + ": "
+ violation.getMessage() + "<h1>");
writer.print("<h1>" + violation.getRootBean() + "<h1>");
writer.print("<h1>-------------------------------<h1>");
}
}
}
那是我的CDI-Beans:
Worker
- 班级:
@RequestScoped
public class Worker implements WorkEmployee{
@NotNull
private String name;
@NotNull
@Size(min=5,max=15)
@Pattern(regexp="Doe")
private String surname;
@Min(5)
@Max(12)
private int employeeId;
@Inject
@Valid
private Adress adresse;
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getSurname() {
return surname;
}
@Override
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public int getEmployeeId() {
return employeeId;
}
@Override
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
@Override
public Adress getAdresse() {
return adresse;
}
@Override
public void setAdresse(Adress adresse) {
this.adresse = adresse;
}
}
Adress
- 班级:
@RequestScoped
public class Adress {
@Pattern(regexp="^47269$")
private String postalCode;
@NotNull
private String street;
@NotNull
@Min(15)
@Max(99)
private int streetNumber;
@NotNull
@Size(max=25)
private String home;
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public int getStreetNumber() {
return streetNumber;
}
public void setStreetNumber(int streetNumber) {
this.streetNumber = streetNumber;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
}