简体   繁体   English

JSF Bean验证不起作用

[英]JSF Bean validation not working

i have bean validation working if i try to save an object with incorrect values, which means an exception is thrown when i do a Session.save or EntityManager.persist , but the automatic bean validation don't work when i submit the form and there is some fields annotated with annotations like: 如果我尝试使用不正确的值保存对象,则可以进行bean验证,这意味着当我执行Session.saveEntityManager.persist时会引发异常,但是当我提交表单时,自动bean验证不起作用是一些带有注释的字段,例如:

@NotEmpty
@NotNull
@Size
etc

I have added all the required .jar files from hibernate validator into my WEB-INF/lib, but it doesn't to work. 我已将所有必需的.jar文件从休眠验证器添加到我的WEB-INF / lib中,但无法正常工作。

Is thee any special thing i should do? 你有什么特别的事我应该做吗?

---------EDIT---------- Here is the code of my page (this is the template) ---------编辑----------这是我页面的代码(这是模板)

<body>
    <p:growl id="growl" showDetail="true" autoUpdate="true"/>
    <h:form id="formPrincipalTemplate">
        <p:ajaxStatus style="width:40px;height:40px;position:fixed;right:5px;bottom:18px">  
            <f:facet name="start">  
                <p:graphicImage library="bmp" name="gifCarregando.gif" />  
            </f:facet>  

            <f:facet name="complete">  
                <h:outputText value="" />  
            </f:facet>  
        </p:ajaxStatus>

        <ui:insert name="conteudo">
            <!-- Aqui vai o conteúdo da página! -->
        </ui:insert>

    </h:form>
</body>

And here is the page itself (the button that should execute the validation is the one which the word "Gravar" on the value: 这是页面本身(应该执行验证的按钮是值上的单词“ Gravar”的按钮:

<ui:composition template="../template/adminTemplate.xhtml">
    <ui:define name="conteudo">

        <h:panelGroup id="atualiza">
            <div class="btnComandos">
                <p:commandButton value="Novo" actionListener="#{mbAdmin.novo}" disabled="#{!mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza"/>
                <p:commandButton value="Alterar" actionListener="#{mbAdmin.alterar}" disabled="#{!mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza"/>
                <p:commandButton value="Gravar" actionListener="#{mbAdmin.gravar}" disabled="#{mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza"/>
                <p:commandButton value="Excluir" actionListener="#{mbAdmin.excluir}" disabled="#{!mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza" onclick="return confirm('Deseja realmente excluir?');"/>
                <p:commandButton value="Cancelar" actionListener="#{mbAdmin.cancelar}" disabled="#{mbAdmin.notModoEdicao}" update=":formPrincipalTemplate:atualiza"/>
            </div>
            <br/>
            <p:tabView id="tabView" orientation="left" style="width:80%;margin:auto;" activeIndex="#{mbAdmin.aba}">

                <p:tab title="Consulta">
                    <p:dataTable id="admins" var="adm" value="#{mbAdmin.admins}" rowKey="#{adm.adminCodigo}"  
                            selection="#{mbAdmin.admin}" selectionMode="single">

                        <p:ajax update=":formPrincipalTemplate:atualiza" event="rowSelect" listener="#{mbAdmin.mudarAba}"/>

                        <p:column width="80">
                            <f:facet name="header">
                                <h:outputText value="Código" />
                            </f:facet>
                            <h:outputText value="#{adm.adminCodigo}" />
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Nome" />
                            </f:facet>
                            <h:outputText value="#{adm.adminNome}" />
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="Email" />
                            </f:facet>
                            <h:outputText value="#{adm.adminEmail}" />
                        </p:column>

                    </p:dataTable>
                </p:tab>

                <p:tab title="Cadastro" id="formulario">
                    <h:panelGrid columns="2">
                        <h:outputText value="Código:"/>
                        <h:outputText value="Nome:"/>

                        <p:inputText value="#{mbAdmin.admin.adminCodigo}" disabled="true"/>
                        <p:inputText value="#{mbAdmin.admin.adminNome}" disabled="#{mbAdmin.notModoEdicao}"/>
                    </h:panelGrid>
                    <h:panelGrid columns="2">
                        <h:outputText value="Email:"/>
                        <h:outputText value="Senha:"/>

                        <p:inputText value="#{mbAdmin.admin.adminEmail}" disabled="#{mbAdmin.notModoEdicao}"/>
                        <p:inputText value="#{mbAdmin.admin.adminSenha}" disabled="#{mbAdmin.notModoEdicao}"/>
                    </h:panelGrid>
                </p:tab>

            </p:tabView>
        </h:panelGroup>
    </ui:define>
</ui:composition>

I would like to ask this as a comment but my reputation is to low, for that reason i ask here. 我想问一下这个问题,但我的声誉很低,因此我在这里问。

Can you write things to your database over your application? 您可以通过应用程序将内容写入数据库吗? Are the columns of your database same annotated like your entity file? 数据库的列是否像实体文件一样被注释?

I never saw a .jar file in WEB-INF/lib therfore I use the "Dependencies" folder. 在使用“ Dependencies”文件夹之前,我从未在WEB-INF / lib中看到.jar文件。 To add sth. 补充 to this folder you rightklick the folder Dependencies -> Add Dependency, better you directly use a maven project there you can put all your dependencies in a pom file and maven will do the rest for you. 在该文件夹中,右键单击文件夹Dependencies-> Add Dependency,最好直接使用maven项目,在那里您可以将所有依赖项放入pom文件中,而maven会为您完成其余工作。 The dependency for maven you'll find easy over Goolge, just search for "hibernate dependency". 您可以通过Goolge轻松找到maven的依赖项,只需搜索“休眠依赖项”即可。 If you use a maven project you never take care about .jar files again. 如果您使用的是Maven项目,则无需再担心.jar文件。 Just add the dependency for whatever you like to use and maven will download everything and put it in the right place that it is available in your project. 只需添加依赖项即可,maven会下载所有内容并将其放在项目中可用的正确位置。 The only thing you need to do is to fix your imports, after you wrote the code, that the packages are avaiable for your class. 您唯一需要做的就是在编写代码之后修复您的导入,以确保该类可使用这些软件包。 I hope this was more or less understandable? 我希望这或多或少是可以理解的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM