Vaadin 7 - Bean Validation

a 夏天 提交于 2019-12-12 02:59:43

问题


I have been trying out Vaadin 7. Firstly, I must admin that I am quiet a noob at Vaadin and not much familiar with JAVA persistance. I am thus trying to learn bean validation and how to use it. I have followed couple of tutorials, but its really hard to decide which is the latest and correct one for Vaadin 7. Nonetheless, I tried downloading and using the JPA container and the BeanValidator and I can't get it working. Here is the code I am using:

package com.example.firstvaadin;

import java.io.Serializable;

import org.hibernate.validator.constraints.Email;
import javax.validation.constraints.*;

public class RegisterBean implements Serializable{

    public  RegisterBean(String email){
        this.email = email;
    }

    @Size(min = 5, max = 50)
    private String email;

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }
}

and

RegisterBean rbean = new RegisterBean("");
BeanItem<RegisterBean> item = new BeanItem<RegisterBean>(rbean);
TextField email = new TextField("email",item.getItemProperty("email"));
email.setImmediate(true);
layout.addComponent(email);
email.setNullRepresentation("");

but I hit run-time exceptions on the following Code: email.addValidator(new BeanValidator(RegisterBean.class, "email"));

The exception is:
    aused by: com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in com.example.firstvaadin.LoginView$1 failed.
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:167)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:984)
    at com.vaadin.ui.Button.fireClick(Button.java:367)
    at com.vaadin.ui.Button$1.click(Button.java:56)
    ... 28 more
Caused by: java.lang.NoClassDefFoundError: javax/validation/MessageInterpolator$Context
    at com.example.firstvaadin.RegisterView.<init>(RegisterView.java:55)
    at com.example.firstvaadin.LoginView$1.buttonClick(LoginView.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    ... 32 more
Caused by: java.lang.ClassNotFoundException: javax.validation.MessageInterpolator$Context
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    ... 39 more

I have no idea which classes are missing and I have added hibernate jars and validation-api.jar

Can someone please give me a hand?

Also, here is a list of jar files that are in the /WebContent/WEB-INF/lib directory and added as classpath in Eclipse.

classmate-0.8.0.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-search-engine-4.3.0.Alpha1.jar
hibernate-search-orm-4.3.0.Alpha1.jar
hibernate-validator-5.0.0.Final.jar
hibernate-validator-annotation-processor-5.0.0.Final.jar
hibernate-validator-cdi-5.0.0.Final.jar
javax.el-2.2.4.jar
javax.el-api-2.2.4.jar
jboss-logging-3.1.1.GA.jar
joda-time-2.1.jar
jsoup-1.7.1.jar
log4j-1.2.17.jar
mysql-connector-java-5.1.22-bin.jar
vaadin-sqlcontainer-1.1.0.jar
validation-api-1.0.0.GA.jar
validation-api-1.1.0.Final.jar

The classnotfound exception is gone but have a new one now:

java.lang.NoSuchMethodError: javax.validation.spi.ConfigurationState.getParameterNameProvider()Ljavax/validation/ParameterNameProvider; at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.(ValidatorFactoryImpl.java:113) at org.hibernate.validator.HibernateValidator.buildValidatorFactory(HibernateValidator.java:45) at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:217) at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111) at com.vaadin.data.validator.BeanValidator.getJavaxBeanValidatorFactory(BeanValidator.java:167) at com.vaadin.data.validator.BeanValidator.getJavaxBeanValidator(BeanValidator.java:182) at com.vaadin.data.validator.BeanValidator.validate(BeanValidator.java:115) at com.vaadin.ui.AbstractField.validate(AbstractField.java:935) at com.vaadin.ui.AbstractField.validate(AbstractField.java:900) at com.vaadin.ui.AbstractField.getErrorMessage(AbstractField.java:1009) at com.vaadin.ui.AbstractComponent.beforeClientResponse(AbstractComponent.java:650) at com.vaadin.ui.AbstractField.beforeClientResponse(AbstractField.java:1643) at com.vaadin.ui.AbstractTextField.beforeClientResponse(AbstractTextField.java:100) at com.vaadin.server.AbstractCommunicationManager.writeUidlResponse(AbstractCommunicationManager.java:799) at com.vaadin.server.AbstractCommunicationManager.paintAfterVariableChanges(AbstractCommunicationManager.java:728) at com.vaadin.server.AbstractCommunicationManager.handleUidlRequest(AbstractCommunicationManager.java:599) at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:315) at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:201) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:680)


回答1:


you don't need hibernate-validator-annotation-processor-5.0.0.Final.jar as a runtime dependency. This is a development library helping to avoid wrong placements of constraints during development.

Regarding validation-api, removing validation-api-1.0.0.GA.jar works, because you are adding two different version of the same library. validation-api-1.0.0.GA.jar is the API version for Bean Validation 1.0. This is compatible with Hibernate Validator 4.3.

validation-api-1.1.0.Final.jar is the Bean Validation 1.1 API which aligns with Hibernate Validator 5.x. Since you use the latter you need the Bean Validation 1.1 API. As simple as that.




回答2:


I'm sure you've already resolved your issue, but for others coming to this late (as I am) and also using Vaadin 7 Framework, the Vaadin Bean Validator is not compatible with Vaadin 7, only Vaadin 6 as mentioned on their download page here:

https://vaadin.com/directory#addon/vaadin-bean-validation

Even in their own demo (jpacontainer-addressbook-demo) they use the hibernate-validator in the pom.xml:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>

Hope this helps out others as it has me.




回答3:


You need to put some more dependencies in your project. I solved with this ones:

    <!--- Dependencies for validating Beans in Vaadin -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.2.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.10</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.10</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.3.Final</version>
    </dependency>

With this its more than enought! You may specify the messages putting the message field in the javax.validation.constraints notation or putting internationalized ones in project properties, looking at the message default value like javax.validation.constraints.Size.message.




回答4:


I believe you need to add the validation-api-1.0.0.GA.jar to WEB-INF/lib

I don't know if there are any any other dependencies.



来源:https://stackoverflow.com/questions/16163162/vaadin-7-bean-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!