javax.validation.ConstraintViolationException:在组的持续时间内类的验证失败[javax.validation.groups.Default,]

时间:2011-11-24 17:42:59

标签: jsf-2 bean-validation hibernate-validator

我的实体在@NotBlank字段上有bean验证注释@Sizename。但是,当我提交JSF表单并将name字段留空时,它不会验证该字段。

以下是相关字段:

@NotBlank(message = "{name.required}")
@Size(max = 25, message = "{long.value}")
@Column(name = "name", length = 25, nullable = false)
private String name;

在没有输入任何数据的情况下提交表单时,我收到以下异常:

Caused by: javax.faces.el.EvaluationException: javax.validation.ConstraintViolationException: validation failed for classes [com.myapp.domain.Department] during persist time for groups [javax.validation.groups.Default, ]
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 89 more
Caused by: javax.validation.ConstraintViolationException: validation failed for classes [com.myapp.domain.Department] during persist time for groups [javax.validation.groups.Default, ]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:132)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:71)
    at org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:177)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:72)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:179)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy212.addDepartment(Unknown Source)
    at com.myapp.beans.DepartmentBean.addOrUpdateDepartment(DepartmentBean.java:105)
    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 org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)

这是如何引起的,现在可以解决它?


更新:这是视图的相关部分:

<h:form>         
    <ice:panelGrid columns="3">
        <h:outputLabel>Department Name:</h:outputLabel>
        <h:inputText id="name" value="#{departmentBean.departmentObj.name}" />
        <h:message for="name" />
    </ice:panelGrid>        
</h:form>
<h:form>
    <ice:panelGroup>
        <h:commandLink value="Add New" action="#{departmentBean.addOrUpdateDepartment}" />
    </ice:panelGroup>
</h:form>

1 个答案:

答案 0 :(得分:3)

来自stacktrace:

at com.myapp.beans.DepartmentBean.addOrUpdateDepartment(DepartmentBean.java:105)
...
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)

这意味着无论如何都会调用您的JSF bean操作方法,而这应该被空name字段上的验证约束阻止,您应该在<h:message>或{{}}中看到验证错误表格的{1}}。

这只能意味着您已将<h:messages>放在命令按钮/链接上,这会导致所有不具有此属性的输入组件将在表单处理中跳过:

immeditate="true"

或您<h:commandButton ... immediate="true" /> 中的此上下文参数会跳过空字段的验证:

web.xml

或者您已通过以下标记在视图中禁用了bean验证:

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
</context-param>

确保你没有。


更新:根据您的更新,您犯了一个经典错误:您将提交按钮的格式与输入字段不同。提交按钮需要与您希望与提交一起发送的输入字段采用相同的形式。