Vaadin 7 - Bean验证

时间:2013-04-23 07:13:22

标签: exception jpa vaadin classnotfoundexception bean-validation

我一直在尝试Vaadin 7.首先,我必须管理我在Vaadin安静的一个菜鸟,并且不太熟悉JAVA持久性。因此,我正在尝试学习bean验证以及如何使用它。我已经按照几个教程,但很难确定哪个是Vaadin 7的最新和正确的。尽管如此,我尝试下载并使用JPA容器和BeanValidator,我无法让它工作。这是我正在使用的代码:

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

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("");

但我在以下代码中遇到了运行时异常:     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

我不知道缺少哪些类,我添加了hibernate jar和validation-api.jar

有人可以帮我一把吗?

此外,这是一个jar文件列表,它位于/ WebContent / WEB-INF / lib目录中,并在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

classnotfound异常消失了,但现在有一个新的:

  

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

4 个答案:

答案 0 :(得分:5)

我确定你已经解决了你的问题,但是对于其他人来说这么晚(我是)以及使用Vaadin 7 Framework,Vaadin Bean Validator与Vaadin 7不兼容,只有Vaadin 6在他们的下载页面上提到:

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

即使在他们自己的演示(jpacontainer-addressbook-demo)中,他们也使用pom.xml中的hibernate-validator:

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

希望这能帮助别人,因为它有我。

答案 1 :(得分:1)

您不需要将hibernate-validator-annotation-processor-5.0.0.Final.jar作为运行时依赖项。这是一个开发库,有助于避免在开发过程中错误放置约束。

关于validation-api,删除 validation-api-1.0.0.GA.jar 是有效的,因为您要添加同一个库的两个不同版本。 validation-api-1.0.0.GA.jar 是Bean Validation 1.0的API版本。这与Hibernate Validator 4.3兼容。

validation-api-1.1.0.Final.jar 是Bean Validation 1.1 API,它与Hibernate Validator 5.x一致。由于您使用后者,因此需要Bean Validation 1.1 API。就这么简单。

答案 2 :(得分:1)

您需要在项目中添加更多依赖项。我解决了这个问题:

    <!--- 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>

有了这个超过了它!您可以指定将message字段放在javax.validation.constraints表示法中或将国际化字段放在项目属性中的消息,查看message默认值,如javax.validation.constraints.Size.message

答案 3 :(得分:0)

我相信你需要将validation-api-1.0.0.GA.jar添加到WEB-INF / lib

我不知道是否有任何其他依赖。