在我的bean被保留之前,我想要验证几个字段。
我有inputText,selectOne,SelectManyListBox,...
我的问题是,对于其中一些验证已经完成而其他一些验证没有......
对于前。我有
<tr>
<td><h:outputLabel value="#{msg.subscriptionFormFieldPhone}:" /></td>
<td><h:inputText
id="phone"
value="#{detailModel.afterObject.phone}"
disabled="#{detailModel.mode == detailModel.viewMode or (detailModel.mode == detailModel.editMode and !loggedUser.hasPermission('CMN backoffice permission id'))}" />
<h:messages for="phone" style="color:red; font-size:12px;" />
</td>
</tr>
...
<tr>
<td><h:outputLabel value="#{msg.subscriptionFormFieldFund}:" /></td>
<td>
<table>
<tr>
<td><h:inputText
id="fund"
value="#{detailModel.afterObject.fund}" disabled="true">
<f:converter converterId="ch.ethz.id.cmn.FundConverter" />
</h:inputText>
<h:messages for="fund" style="color:red; font-size:12px;" />
</td>
<td valign="top">
<!-- add button -->
<h:commandButton styleClass="waiButton"
value="#{lakshmiMsg.commonButtonAddExisting}"
action="#{detailController.gotoFundSelection()}"
rendered="#{(detailModel.mode == detailModel.createMode and detailModel.afterObject.fund == null) or ((detailModel.mode != detailModel.viewMode and detailModel.afterObject.fund == null))}"
disabled="#{detailModel.mode == detailModel.multiEditMode}" />
<!-- delete button -->
<h:commandButton styleClass="waiButton"
value="#{lakshmiMsg.commonButtonDelete}"
action="#{detailController.deleteFund()}"
rendered="#{((detailModel.mode == detailModel.editMode) and loggedUser.hasPermission('CMN backoffice permission id')) or (detailModel.mode == detailModel.createMode and detailModel.afterObject.fund != null)}"
disabled="#{detailModel.mode == detailModel.multiEditMode}" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><h:outputLabel value="#{msg.subscriptionFormFieldOwner}:" /></td>
<td>
<table>
<tr>
<td><h:inputText id="owner"
value="#{detailModel.afterObject.owner}"
disabled="true">
<f:converter converterId="ch.ethz.id.cmn.UserConverter"></f:converter>
</h:inputText>
<h:messages for="owner" style="color:red; font-size:12px;" />
</td>
<td valign="top">
<!-- add button -->
<h:commandButton styleClass="waiButton"
value="#{lakshmiMsg.commonButtonAddExisting}"
action="#{detailController.gotoUserSelection()}"
rendered="#{(detailModel.mode == detailModel.createMode and detailModel.afterObject.owner == null) or ((detailModel.mode != detailModel.viewMode and detailModel.afterObject.owner == null))}"
immediate="true"
disabled="#{detailModel.mode == detailModel.multiEditMode}" />
<!-- delete button -->
<h:commandButton styleClass="waiButton"
value="#{lakshmiMsg.commonButtonDelete}"
action="#{detailController.deleteOwner()}"
rendered="#{((detailModel.mode == detailModel.editMode) and loggedUser.hasPermission('CMN backoffice permission id')) or (detailModel.mode == detailModel.createMode and detailModel.afterObject.owner != null)}"
immediate="true"
disabled="#{detailModel.mode == detailModel.multiEditMode}" />
</td>
</tr>
</table>
</td>
</tr>
...
<tr>
<td valign="top">
<h:outputLabel value="#{msg.subscriptionFormFieldDataOptions}:" /></td>
<td>
<table>
<tr>
<td>
<h:selectManyListbox
id="dataOptions"
value="#{detailModel.selectedDataOptions}"
size="5"
disabled="#{detailModel.mode == detailModel.viewMode or (detailModel.mode == detailModel.editMode and !loggedUser.hasPermission('CMN backoffice permission id'))}">
<f:selectItems value="#{detailModel.afterObject.dataOptions}"
var="sdo" itemLabel="#{sdo.dataOptionName}"
itemValue="#{sdo}" />
</h:selectManyListbox>
<h:messages for="dataOptions" style="color:red; font-size:12px;" />
</td>
...
</tr>
</table>
</td>
</tr>
...
在我的bean(与detailModel.afterObject绑定)中,我用@NotNull标记了字段 和@Size(min = 1)。
@NotNull( message = "{validation.emptyPhoneNumber}" )
@Pattern( regexp = "^(((41)(\\s?)(-?))|(0))(7)(\\d{1})(\\s?)(-?)(\\d{3})(\\s?)(-?)(\\d{2})(\\s?)(-?)(\\d{2})", message = "{validation.notValidPhoneNumber}" )
private String phone;
@NotNull( message = "{validation.emptyFund}" )
@Size(min=1, message = "{validation.emptyFund}" )
private String fund;
@NotNull( message = "{validation.emptyOwner}" )
@Size(min=1, message = "{validation.emptyOwner}" )
private String owner;
@ManyToMany( fetch = FetchType.LAZY, cascade = {} )
@JoinTable( name = "CMN_MAP_SUBSCRIPTION_TO_DATAOPTION" )
@NotNull( message = "{validation.emptyDataOption}" )
@Size(min=1, message = "{validation.emptyDataOption}" )
private Set< DataOption > dataOptions;
好吧,第一个inputText(phone)已经过验证,如果我把它留空或者模式不匹配,我会得到错误信息,但是对于其他人来说(显然)没有验证;如果我把它们留空,就不会出现任何消息...
唯一的区别是,对于基金和所有者inputFileds,有一个转换器,最后一个是selectManyListBox,它映射到一个Set,而所有其他的都映射到String。
由于持久性失败,我在日志中有一条错误消息,在那里我可以看到ConstraintViolationException,但我不明白为什么它在验证时没有声明在页面上而不是堆栈上的消息记录中的痕迹...
[exec] [#|2013-09-17T11:26:49.901+0200|WARNING|glassfish3.1.2|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=420;_ThreadName=Thread-2;|DTX5014: Caught exception in beforeCompletion() callback:
[exec] javax.persistence.PersistenceException: error during managed flush
[exec] at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1515)
[exec] at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:109)
[exec] at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53)
[exec] at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:435)
[exec] at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:855)
[exec] at com.sun.enterprise.transaction.UserTransactionImpl.commit(UserTransactionImpl.java:208)
[exec] at ch.ethz.id.wai.lakshmi.engine.common.TransactionHelper.commitTransaction(TransactionHelper.java:74)
[exec] at ch.ethz.id.wai.lakshmi.stdcmp.boo.processing.BusinessObjectOrderProcessing.processOrder(BusinessObjectOrderProcessing.java:121)
[exec] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[exec] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[exec] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[exec] at java.lang.reflect.Method.invoke(Method.java:601)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.invokeWorkflowStep(LakshmiServerBean.java:850)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.executeNextWorkflowStep(LakshmiServerBean.java:796)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.executeNextWorkflowStep(LakshmiServerBean.java:707)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.continueWorkflow(LakshmiServerBean.java:627)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.processLakshmiComponentResponse(LakshmiServerBean.java:576)
[exec] at ch.ethz.id.wai.lakshmi.engine.ejb.LakshmiServerBean.onMessage(LakshmiServerBean.java:397)
[exec] at sun.reflect.GeneratedMethodAccessor967.invoke(Unknown Source)
[exec] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[exec] at java.lang.reflect.Method.invoke(Method.java:601)
[exec] at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
[exec] at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
[exec] at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:4180)
[exec] at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5368)
[exec] at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348)
[exec] at com.sun.ejb.containers.MessageBeanContainer.deliverMessage(MessageBeanContainer.java:1099)
[exec] at com.sun.ejb.containers.MessageBeanListenerImpl.deliverMessage(MessageBeanListenerImpl.java:81)
[exec] at com.sun.enterprise.connectors.inbound.MessageEndpointInvocationHandler.invoke(MessageEndpointInvocationHandler.java:171)
[exec] at com.sun.proxy.$Proxy409.onMessage(Unknown Source)
[exec] at com.sun.messaging.jms.ra.OnMessageRunner.run(OnMessageRunner.java:260)
[exec] at com.sun.enterprise.connectors.work.OneWork.doWork(OneWork.java:114)
[exec] at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
[exec] at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
[exec] Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [ch.ethz.id.wai.cmn.bo.Subscription] during persist time for groups [javax.validation.groups.Default, ]
[exec] List of constraint violations:[
[exec] ConstraintViolationImpl{interpolatedMessage='Please specify a fund', propertyPath=fund, rootBeanClass=class ch.ethz.id.wai.cmn.bo.Subscription, messageTemplate='{validation.emptyFund}'}
[exec] ConstraintViolationImpl{interpolatedMessage='Please specify a fund', propertyPath=fund, rootBeanClass=class ch.ethz.id.wai.cmn.bo.Subscription, messageTemplate='{validation.emptyFund}'}
[exec] ConstraintViolationImpl{interpolatedMessage='Please specify a owner', propertyPath=owner, rootBeanClass=class ch.ethz.id.wai.cmn.bo.Subscription, messageTemplate='{validation.emptyOwner}'}
[exec] ConstraintViolationImpl{interpolatedMessage='{validation.emptyDataOption}', propertyPath=dataOptions, rootBeanClass=class ch.ethz.id.wai.cmn.bo.Subscription, messageTemplate='{validation.emptyDataOption}'}
[exec] ]
[exec] at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159)
[exec] at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
[exec] at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:185)
[exec] at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81)
[exec] at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
[exec] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
[exec] at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
[exec] at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
[exec] at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
[exec] at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)
[exec] at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)
[exec] at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:104)
[exec] ... 32 more
[exec] |#]
欢迎提供帮助,提示,提示......
更新:
我写了一个Validator并将其添加到手机inputText并为inputText提供资金:
<f:validator validatorId="ch.ethz.id.cmn.validator.SubscriptionValidator" />
我在validate方法中设置了一个断点。
嗯,对于手机,该方法被调用,对于基金没有!
我很困惑。我的HTML代码中的某些内容阻止了验证......
更新2:
如果我删除了disabled="true"
,那么它就可以了。它是如此通缉或是一个错误?如果该字段被禁用,是否还有办法让它工作?
更新3:
现在我真的很困惑...... 用readonly替换了disabled后,所有(但是selectManyListBox)被联合起来运行良好,直到我在持久化之后尝试编辑页面中的某个值。 虽然我没有更改/删除基金和所有者字段,但我收到验证错误消息... 我在bean的getters方法中放置了一个断点,并设置了值,即不为null。那么为什么@NotNull还在抱怨? 如果我删除bean中的注释并在html中添加required和requiredMessage,也会发生这种情况。
答案 0 :(得分:0)
在发现问题是禁用=“真实”之后,我找到了答案。 见jsf (richfaces) readonly input text validation,Balus的回答;)
更新:
嗯,它适用于inputText但不适用于selectManyListBox,如果我删除了禁用...
有什么想法吗?