我尝试使用Maven和Vaadin Bean验证来创建一个应用程序来验证我的bean,但现在对我没有用。
我正在尝试这个。
@Entity
public class Person{
@Id
@GeneratedValue
private Integer id;
@NotNull
@NotEmpty
@Size(min=5, max=50, message="insert first name")
private String firstName;
@NotNull
@NotEmpty
@Email
private String email;
}
//my app
public class LoginView extends VerticalLayout{
private TextField firstName, email;
private BeanFieldGroup<Person> binder;
private FormLayout form;
public LoginView(){
form = new FormLayout();
binder = new BeanFieldGroup<Person>(Person.class);
Field<?> field = null;
field = binder.buildAndBind("Firstname", "firstName");
firstName = (TextField)binder.getField("firstName");
form.addComponent(firstName);
field = binder.buildAndBind("Email", "email");
email = (TextField)binder.getField("email");
form.addComponent(email);
this.addComponent(form);
}
}
我添加了hibernate-validator作为依赖,但仍然无法正常工作。在vaadin bean验证器文档中说要使用Hibernate Validator或agimatec-validation。
我需要为此工作添加哪个依赖项?
方法isBeanValidationImplementationAvailable()返回true,Debug低于。
11:26:54,112 DEBUG [org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver] - Found javax.persistence.Persistence on classpath containing 'getPersistenceUtil'. Assuming JPA 2 environment. Trying to instantiate JPA aware TraversableResolver
11:26:54,113 DEBUG [org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver] - Instantiated JPA aware TraversableResolver of type org.hibernate.validator.internal.engine.resolver.JPATraversableResolver.
11:26:54,113 DEBUG [org.hibernate.validator.internal.xml.ValidationXmlParser] - Trying to load META-INF/validation.xml for XML based Validator configuration.
11:26:54,113 DEBUG [org.hibernate.validator.internal.xml.ValidationXmlParser] - No META-INF/validation.xml found. Using annotation based configuration only.
Is bean validation implementation available: true
答案 0 :(得分:2)
它应该在您致电binder.commit()
时进行验证。检查类路径上是否有javax.validation.Validation
类。
如果不是这样,请调试isBeanValidationImplementationAvailable()
上的BeanFieldGroup
静态方法并发布您发现的任何进一步详情。
答案 1 :(得分:0)
您需要使用随Vaadin Bean Validation插件一起提供的BeanValidationForm和BeanValidationValidator。 您可以在svn为此插件找到一些演示代码。
答案 2 :(得分:0)
我有同样的问题,决定是否包含在依赖项<artifactId>javaee-api</artifactId>