Bean验证不起作用

时间:2014-09-29 18:18:39

标签: jsf jsf-2 bean-validation

我正在尝试使用Bean验证而不是JSF验证,因为它是一个更干的方法,我在我的模型上使用注释但是当我在jsf字段上插入空数据时它什么也没做......

我使用的所有注释都没有任何效果,就像它们不存在一样......

我正在使用Tomcat 8

我的xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
<h1>Cadastro de funcionario</h1>
<h:form>
    <h:messages/>
    <h:panelGrid columns="3">
        <h:outputLabel value="Salário: R$ " for="campo-salario"/>
        <h:inputText id="campo-salario" value="#{funcionarioBean.funcionario.salario}">
            <f:convertNumber locale="pt_BR"/>
        </h:inputText>
        <h:message for="campo-salario" />

        <h:outputLabel value="Código: " for="campo-codigo"/>
        <h:inputText id="campo-codigo" value="#{funcionarioBean.funcionario.codigo}"/>
        <h:message for="campo-codigo"/>

        <h:outputLabel value="Data: " for="campo-aniversario"/>
        <h:inputText id="campo-aniversario" value="#{funcionarioBean.funcionario.aniversario}">
            <f:convertDateTime pattern="dd/MM/yyyy" />
        </h:inputText>
        <h:message for="campo-aniversario"/>

        <h:commandButton value="Cadastrar" action="#{funcionarioBean.mensagem}"/>
    </h:panelGrid>

</h:form>
</h:body>
</html>

我的豆

@ManagedBean
public class FuncionarioBean {
    private Funcionario funcionario = new Funcionario();

    public void mensagem(){
        System.out.println("Pressionado");
    }

    public Funcionario getFuncionario() {
        return funcionario;
    }

    public void setFuncionario(Funcionario funcionario) {
        this.funcionario = funcionario;
    }
}

我的Funcionario课程

import java.util.Date;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

public class Funcionario {
    @NotNull(message="{br.com.k19.Funcionario.nome")
    @Min(value = 0)
    private Double salario;

    @NotNull
    @Min(value = 5)
    @Max(value = 19)
    private Integer codigo;

    @NotNull
    private Date aniversario;

    public Double getSalario() {
        return salario;
    }
    public void setSalario(Double salario) {
        this.salario = salario;
    }
    public Integer getCodigo() {
        return codigo;
    }
    public void setCodigo(Integer codigo) {
        this.codigo = codigo;
    }
    public Date getAniversario() {
        return aniversario;
    }
    public void setAniversario(Date aniversario) {
        this.aniversario = aniversario;
    }

}

我的lib 无法发布图片。 库 &GT;

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.6.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-validator-5.0.0.final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
jsf-api-2.2.8.jar
jsf-impl-2.2.8.jar
mysql-connector-java-5.1.23.bin.jar
omnifaces-1.8.1.jar
primefaces-5.0.jar
validation-api-1.0.0.GA.jar

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>K19-Conversao-e-Validacao</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>

    <context-param>
        <param-name>com.sun.faces.writeStateAtFormEnd</param-name>
        <param-value>false</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
</web-app>

1 个答案:

答案 0 :(得分:0)

再次下载hibernate jar以匹配版本

下载验证码:http://hibernate.org/validator/

下载ORM:http://hibernate.org/orm/

我的lib Lib

现在按计划工作

参考:Bean Validation @NotNull, @NotBlank and @NotEmpty does not work in JSF+Tomcat