Bean验证不起作用?

时间:2014-04-14 14:23:59

标签: java maven vaadin vaadin7

我正在寻找解决问题的方法,但仍然无法找到。在我的bean中,我使用注释进行验证但是没有工作,而且我正在寻找所有互联网来完成这项工作。

我正在使用:Vaadin 7和Maven

我这样做。

/** person's 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;

//get and set
}

//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);
     }
}

我如何做这项工作?

enter image description here

/** maven dependecy */
<dependency>
    <groupId>com.vaadin.addon</groupId>
    <artifactId>beanvalidation-addon</artifactId>
    <version>0.6.2</version>
</dependency>



/** exception */
java.lang.NoSuchMethodError: com.vaadin.event.EventRouter.fireEvent(Ljava/util/EventObject;Lcom/vaadin/server/ErrorHandler;)V
    at com.vaadin.server.VaadinService.onVaadinSessionStarted(VaadinService.java:783)
    at com.vaadin.server.VaadinService.createAndRegisterSession(VaadinService.java:740)
    at com.vaadin.server.VaadinService.doFindOrCreateVaadinSession(VaadinService.java:696)
    at com.vaadin.server.VaadinService.findOrCreateVaadinSession(VaadinService.java:637)
    at com.vaadin.server.VaadinService.findVaadinSession(VaadinService.java:501)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1376)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

2 个答案:

答案 0 :(得分:0)

好的,就像我说的那样,你需要使用Vaadin的Bean Validation插件中提供给你的BeanValidationForm。

以下是一个例子:

@Theme("mytheme")
public class MyVaadinUI extends UI {

@Override
public void init(VaadinRequest request){
    VerticalLayout layout = new VerticalLayout();

    Person bean = new Person();

    final BeanValidationForm<Person> form = new BeanValidationForm<Person>(
            Person.class);

    // show null values as empty strings
    form.setFormFieldFactory(new DefaultFieldFactory() {
        @Override
        public Field createField(Item item, Object propertyId,
                                 Component uiContext) {
            Field field = super.createField(item, propertyId, uiContext);
            if (field instanceof TextField) {
                ((TextField) field).setNullRepresentation("");
            }
            return field;
        }
    });

    // use a BeanItem<BeanToValidate> for full validation
    form.setItemDataSource(new BeanItem<Person>(bean));

    // validate immediately when field focus lost but use explicit commit
    form.setImmediate(true);
    form.setWriteThrough(false);

    layout.addComponent(form);

    // explicit validation button
    Button validate = new Button("Validate");
    validate.addListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            try {
                form.validate();
                Notification.show("Validation successful");
            } catch (Validator.InvalidValueException e) {
                Notification.show(
                        "Validation failed: " + e.getMessage(),
                        Notification.TYPE_WARNING_MESSAGE);
            }
        }
    });
    layout.addComponent(validate);

    setContent(layout);
}
}

答案 1 :(得分:0)

我将此作为答案添加,因为我认为它会阻止您尝试完全使用Bean Validation插件。

问题是Bean Validation插件只与Vaadin 6+版本兼容(我最近看了一下插件页面,看它是否兼容): Vaadin Bean Validation supported Vaadin versions

我猜是因为你用vaadin7标记了这个问题,你使用的是Vaadin 7.所以你不能使用这个插件,因此你得到的例外。

您需要寻找另一种方法来为您的bean提供验证。一种方法是将自定义Validator实例添加到字段中。查看Vaadin书页的Bean Validation部分。