jsf - Why primefaces is not applying validation rules to components -
i'm trying validate form using hibernate validations. hibernate not let data included without required fields, however, no message sent in primefaces. when sent form data, not included in database , screen not show error message.
how can fix that?
xhtml
<h:form id="frm" > <p:messages /> <h:panelgrid columns="3" cellpadding="5"> <h:outputlabel class="form-label required" for="nome" value="nome" /> <p:inputtext id="nome" value="#{usuariomb.usuario.nome}"> </p:inputtext> <p:message for="nome"></p:message> <h:outputlabel class="form-label" for="email" value="e-mail" /> <p:inputtext id="email" type="email" value="#{usuariomb.usuario.email}" /> <p:message for="email"></p:message> <h:outputlabel class="form-label" for="idade" value="idade" /> <p:inputtext id="idade" value="#{usuariomb.usuario.idade}" /> <p:message for="idade"></p:message> <h:outputlabel class="form-label" for="cpf" value="cpf" /> <p:inputtext id="cpf" value="#{usuariomb.usuario.cpf}" /> <p:message for="cpf"></p:message> <h:outputlabel class="form-label" for="ativo" value="ativo" /> <p:selectbooleancheckbox id="ativo" value="#{usuariomb.usuario.ativo}" /> <p:message for="ativo"></p:message> <h:outputlabel class="form-label" for="sexo" value="sexo" /> <p:selectonemenu id="sexo" value="#{usuariomb.usuario.sexo}"> <f:selectitem itemlabel="masculino" itemvalue="masculino" /> <f:selectitem itemlabel="feminino" itemvalue="feminino" /> </p:selectonemenu> <p:message for="sexo"></p:message> <h:outputlabel class="form-label" for="situacaocivil" value="situacao civil" /> <p:selectoneradio id="situacaocivil" value="#{usuariomb.usuario.situacaocivil}"> <f:selectitem itemlabel="solteiro" itemvalue="solteiro" /> <f:selectitem itemlabel="casado" itemvalue="casado" /> <f:selectitem itemlabel="divorciado" itemvalue="divorciado" /> </p:selectoneradio> <p:message for="situacaocivil"></p:message> </h:panelgrid> <p:commandbutton value="salvar" action="#{usuariomb.salvar}" process=":frm" update=":frm, :table" type="submit" icon="ui-icon-check" validateclient="true" /> </h:form>
managedbeanbean
@requestscoped @managedbean(name="usuariomb") public class usuariomb { private usuario usuario = new usuario(); public string salvar(){ daofactory.getusuariodao().save(usuario); usuario = new usuario(); return ""; } public list<usuario> findall(){ return daofactory.getusuariodao().findall(); } public usuario getusuario() { return usuario; } public void setusuario(usuario usuario) { this.usuario = usuario; } }
entity
package edu.fae.model; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import org.hibernate.validator.notempty; import org.hibernate.validator.notnull; @entity public class usuario { @id @generatedvalue(strategy = generationtype.identity) private long id; @notnull @notempty private string nome; private string email; private string sexo; private int idade; private string cpf; private string situacaocivil; private boolean ativo; //get set }
faces-config
<?xml version="1.0" encoding="utf-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd" version="2.1"> </faces-config>
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app id="webapp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>javaflex</display-name> <!-- filtro hibernate --> <filter> <filter-name>hibernatefilter</filter-name> <filter-class>edu.fae.util.opensessioninviewfilter</filter-class> </filter> <!-- filtro que desabilita o cache --> <filter> <filter-name>nocachefilter</filter-name> <filter-class>edu.fae.util.nocachefilter</filter-class> </filter> <!-- aplica o hibernatefilter em todas requisições --> <filter-mapping> <filter-name>hibernatefilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- aplica o filtro nocache em todas requisições *.xhtml --> <filter-mapping> <filter-name>nocachefilter</filter-name> <url-pattern>*.xhtml</url-pattern> </filter-mapping> <context-param> <param-name>primefaces.transform_metadata</param-name> <param-value>true</param-value> </context-param> <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>*.jsf</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsf</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
exception
ago 17, 2014 9:49:30 pm com.sun.faces.lifecycle.invokeapplicationphase execute warning: #{usuariomb.salvar}: org.hibernate.validator.invalidstateexception: validation failed for: edu.fae.model.usuario javax.faces.facesexception: #{usuariomb.salvar}: org.hibernate.validator.invalidstateexception: validation failed for: edu.fae.model.usuario @ com.sun.faces.application.actionlistenerimpl.processaction(actionlistenerimpl.java:118) @ javax.faces.component.uicommand.broadcast(uicommand.java:315) @ javax.faces.component.uiviewroot.broadcastevents(uiviewroot.java:794) @ javax.faces.component.uiviewroot.processapplication(uiviewroot.java:1259) @ com.sun.faces.lifecycle.invokeapplicationphase.execute(invokeapplicationphase.java:81) @ com.sun.faces.lifecycle.phase.dophase(phase.java:101) @ com.sun.faces.lifecycle.lifecycleimpl.execute(lifecycleimpl.java:118) @ javax.faces.webapp.facesservlet.service(facesservlet.java:593) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ edu.fae.util.opensessioninviewfilter.dofilter(opensessioninviewfilter.java:39) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:501) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:314) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(unknown source) caused by: javax.faces.el.evaluationexception: org.hibernate.validator.invalidstateexception: validation failed for: edu.fae.model.usuario @ javax.faces.component.methodbindingmethodexpressionadapter.invoke(methodbindingmethodexpressionadapter.java:102) @ com.sun.faces.application.actionlistenerimpl.processaction(actionlistenerimpl.java:102) ... 30 more caused by: org.hibernate.validator.invalidstateexception: validation failed for: edu.fae.model.usuario @ org.hibernate.validator.event.validateeventlistener.validate(validateeventlistener.java:148) @ org.hibernate.validator.event.validateeventlistener.onpreinsert(validateeventlistener.java:172) @ org.hibernate.action.entityidentityinsertaction.preinsert(entityidentityinsertaction.java:106) @ org.hibernate.action.entityidentityinsertaction.execute(entityidentityinsertaction.java:41) @ org.hibernate.engine.actionqueue.execute(actionqueue.java:248) @ org.hibernate.event.def.abstractsaveeventlistener.performsaveorreplicate(abstractsaveeventlistener.java:290) @ org.hibernate.event.def.abstractsaveeventlistener.performsave(abstractsaveeventlistener.java:180) @ org.hibernate.event.def.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:108) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid(defaultsaveorupdateeventlistener.java:186) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.entityistransient(defaultsaveorupdateeventlistener.java:175) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.performsaveorupdate(defaultsaveorupdateeventlistener.java:98) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:70) @ org.hibernate.impl.sessionimpl.firesaveorupdate(sessionimpl.java:507) @ org.hibernate.impl.sessionimpl.saveorupdate(sessionimpl.java:499) @ org.hibernate.impl.sessionimpl.saveorupdate(sessionimpl.java:495) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.hibernate.context.threadlocalsessioncontext$transactionprotectionwrapper.invoke(threadlocalsessioncontext.java:301) @ com.sun.proxy.$proxy16.saveorupdate(unknown source) @ edu.fae.dao.hibernate.usuariodaohibernate.save(usuariodaohibernate.java:44) @ edu.fae.controllers.usuariomb.salvar(usuariomb.java:19) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.apache.el.parser.astvalue.invoke(astvalue.java:278) @ org.apache.el.methodexpressionimpl.invoke(methodexpressionimpl.java:273) @ com.sun.faces.facelets.el.tagmethodexpression.invoke(tagmethodexpression.java:105) @ javax.faces.component.methodbindingmethodexpressionadapter.invoke(methodbindingmethodexpressionadapter.java:88) ... 31 more ago 17, 2014 9:49:30 pm org.hibernate.assertionfailure <init> severe: assertion failure occured (this may indicate bug in hibernate, more due unsafe use of session) org.hibernate.assertionfailure: null id in edu.fae.model.usuario entry (don't flush session after exception occurs) @ org.hibernate.event.def.defaultflushentityeventlistener.checkid(defaultflushentityeventlistener.java:55) @ org.hibernate.event.def.defaultflushentityeventlistener.getvalues(defaultflushentityeventlistener.java:157) @ org.hibernate.event.def.defaultflushentityeventlistener.onflushentity(defaultflushentityeventlistener.java:113) @ org.hibernate.event.def.abstractflushingeventlistener.flushentities(abstractflushingeventlistener.java:196) @ org.hibernate.event.def.abstractflushingeventlistener.flusheverythingtoexecutions(abstractflushingeventlistener.java:76) @ org.hibernate.event.def.defaultflusheventlistener.onflush(defaultflusheventlistener.java:26) @ org.hibernate.impl.sessionimpl.flush(sessionimpl.java:1000) @ org.hibernate.impl.sessionimpl.managedflush(sessionimpl.java:338) @ org.hibernate.transaction.jdbctransaction.commit(jdbctransaction.java:106) @ edu.fae.util.opensessioninviewfilter.dofilter(opensessioninviewfilter.java:43) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:501) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:314) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(unknown source) org.hibernate.assertionfailure: null id in edu.fae.model.usuario entry (don't flush session after exception occurs) @ org.hibernate.event.def.defaultflushentityeventlistener.checkid(defaultflushentityeventlistener.java:55) @ org.hibernate.event.def.defaultflushentityeventlistener.getvalues(defaultflushentityeventlistener.java:157) @ org.hibernate.event.def.defaultflushentityeventlistener.onflushentity(defaultflushentityeventlistener.java:113) @ org.hibernate.event.def.abstractflushingeventlistener.flushentities(abstractflushingeventlistener.java:196) @ org.hibernate.event.def.abstractflushingeventlistener.flusheverythingtoexecutions(abstractflushingeventlistener.java:76) @ org.hibernate.event.def.defaultflusheventlistener.onflush(defaultflusheventlistener.java:26) @ org.hibernate.impl.sessionimpl.flush(sessionimpl.java:1000) @ org.hibernate.impl.sessionimpl.managedflush(sessionimpl.java:338) @ org.hibernate.transaction.jdbctransaction.commit(jdbctransaction.java:106) @ edu.fae.util.opensessioninviewfilter.dofilter(opensessioninviewfilter.java:43) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:501) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:314) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(unknown source) ago 17, 2014 9:49:30 pm org.apache.catalina.core.standardwrappervalve invoke severe: servlet.service() servlet [faces servlet] in context path [/introducao] threw exception [org.hibernate.assertionfailure: null id in edu.fae.model.usuario entry (don't flush session after exception occurs)] root cause org.hibernate.assertionfailure: null id in edu.fae.model.usuario entry (don't flush session after exception occurs) @ org.hibernate.event.def.defaultflushentityeventlistener.checkid(defaultflushentityeventlistener.java:55) @ org.hibernate.event.def.defaultflushentityeventlistener.getvalues(defaultflushentityeventlistener.java:157) @ org.hibernate.event.def.defaultflushentityeventlistener.onflushentity(defaultflushentityeventlistener.java:113) @ org.hibernate.event.def.abstractflushingeventlistener.flushentities(abstractflushingeventlistener.java:196) @ org.hibernate.event.def.abstractflushingeventlistener.flusheverythingtoexecutions(abstractflushingeventlistener.java:76) @ org.hibernate.event.def.defaultflusheventlistener.onflush(defaultflusheventlistener.java:26) @ org.hibernate.impl.sessionimpl.flush(sessionimpl.java:1000) @ org.hibernate.impl.sessionimpl.managedflush(sessionimpl.java:338) @ org.hibernate.transaction.jdbctransaction.commit(jdbctransaction.java:106) @ edu.fae.util.opensessioninviewfilter.dofilter(opensessioninviewfilter.java:43) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:501) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:103) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1070) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:611) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:314) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(unknown source)
libs
github.: https://github.com/ericrf/poo-sala/tree/master/entradadados
try changing command buttons
update=":frm, :table"
to
update=":frm :table"
my minimal working example:
page.xhtml
<?xml version="1.0" encoding="utf-8"?> <!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://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:p="http://primefaces.org/ui"> <f:view> <h:form> <h:panelgrid columns="3"> <h:outputlabel for="name" value="name"/> <p:inputtext id="name" value="#{tests.test.name}"/> <p:message for="name"/> </h:panelgrid> <p:commandbutton value="save" action="#{tests.save}" process="@form" update="@form"/> </h:form> </f:view> </html>
tests view-bean
package de.cupofsoftware.carasmo.red.tech.file; import javax.faces.view.viewscoped; import javax.inject.named; @viewscoped @named public class tests { private test test = new test(); public test gettest() { return test; } public void save() { // something... } }
test entity
package de.cupofsoftware.carasmo.red.tech.file; import javax.persistence.entity; import javax.persistence.id; import javax.validation.constraints.notnull; @entity public class test { private string id; @notnull private string name; @id public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
Comments
Post a Comment