如果启用了ajax,则PrimeFaces commandButton不会调用操作

时间:2011-10-01 07:48:44

标签: java jsf-2 primefaces

这是我的页面:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cpanel="http://java.sun.com/jsf/composite/components/cpanel"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title></title>
    </h:head>
    <h:body>
        <ui:composition template="/WEB-INF/templates/cpanelLayout.xhtml">
            <ui:define name="metadata">
                <f:metadata>
                    <f:viewParam name="id" value="#{linkDoctorBean.incomingDoctorId}" required="true"/>
                    <f:event type="preRenderView" listener="#{linkDoctorBean.preRenderView}"/>
                </f:metadata>
            </ui:define>
            <ui:define name="cpanelContent">
                <cpanel:adddedClinicDoctor doctor="#{linkDoctorBean.incomingDoctor}" clinic="#{linkDoctorBean.clinic}"/>
                <h:form id="form">
                    <div>
                        <h:panelGrid columns="2">
                            <p:selectOneMenu id="doctor"
                                             value="#{linkDoctorBean.doctor}"
                                             converter="#{doctorConverter}"
                                             effect="fade"
                                             var="d"
                                             height="180"
                                             validator="#{linkDoctorBean.validateDoctor}">
                                <f:selectItem itemLabel="Select a doctor" itemValue="" noSelectionOption="true"/>
                                <f:selectItems value="#{linkDoctorBean.doctors}" var="doctor" itemLabel="#{stringBean.doctorToString(doctor)}" itemValue="#{doctor}"/>
                                <p:column>
                                    <p:graphicImage value="#{applicationBean.baseResourceUrl}/doc-photo/small/#{d.urlDisplayName}"/>
                                </p:column>
                                <p:column>
                                    #{stringBean.doctorToString(d)}
                                </p:column>
                            </p:selectOneMenu>
                            <p:message id="doctorMessage" for="doctor"/>
                        </h:panelGrid>
                    </div>
                    <div style="margin-top:20px;">
                        <p:commandButton value="Link" action="#{linkDoctorBean.submit()}" update="doctorMessage" process="@all"/>
                        <h:link value="Cancel" outcome="/pages/cpanel/manage-doctors" style="margin-left:10px;"/>
                    </div>
                </h:form>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

当我将ajax="false"添加到<p:commandButton/>时,它可以正常工作 - 调用linkDoctorBean.submit()方法。

但是,启用ajax后,如果表单在当前视图中从未通过验证,则将调用action方法,但如果表单验证失败一次并随后提交,则会清除错误消息,但{{1}永远不会调用方法。

修改 我也尝试用这个替换submit()

p:commandButton

行为完全相同。在这两种情况下,当提交有效选项时,验证消息似乎被清除。所以更新/渲染正在发生,而不是进程/执行。所有后续尝试都无法执行操作,因此表单实际上已经死亡,直到下次加载页面为止。

修改 这似乎不是PrimeFaces的问题。这是没有PrimeFaces的表单:

<h:commandButton value="Link" action="#{linkDoctorBean.submit()}">
    <f:ajax execute="@all" render="doctorMessage"/>
</h:commandButton>

现在存在一些细微差别,例如 <h:form id="form"> <div> <h:panelGrid columns="2"> <h:selectOneMenu id="doctor" value="#{linkDoctorBean.doctor}" converter="#{doctorConverter}" style="width:200px;" required="true" requiredMessage="Please select a doctor" validator="#{linkDoctorBean.validateDoctor}"> <f:selectItem itemLabel="Select a doctor" itemValue="#{null}" noSelectionOption="true"/> <f:selectItems value="#{linkDoctorBean.doctors}" var="doctor" itemLabel="#{stringBean.doctorToString(doctor)}" itemValue="#{doctor}"/> </h:selectOneMenu> <h:message id="doctorMessage" for="doctor"/> </h:panelGrid> </div> <div style="margin-top:20px;"> <h:commandButton value="Link" action="#{linkDoctorBean.submit()}"> <f:ajax execute="@form" render="doctorMessage"/> </h:commandButton> <h:link value="Cancel" outcome="/pages/cpanel/manage-doctors" style="margin-left:10px;"/> </div> </h:form> 和占位符选择项的值为required。也许最好关注这个最新片段可能出现的问题,暂时忘掉PrimeFaces。

如果它有帮助,这是null类:

DoctorConverter

修改 ......以下是@Named("doctorConverter") @RequestScoped public class DoctorConverter implements Converter, Serializable { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { UIInput in = (UIInput) component; String s = in.getSubmittedValue() != null ? in.getSubmittedValue().toString() : ""; try { long doctorId = Long.parseLong(s); Doctor doctor = doctorDao.findByDoctorId(doctorId); if (doctor == null) { throw new Exception("Doctor not found."); } return doctor; } catch (Exception e) { throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please enter a valid doctor", null), e); } } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { if (value == null || value.toString().length() == 0) { return ""; } Doctor doctor = (Doctor) value; return doctor.getDoctorId().toString(); } @EJB private DoctorDao doctorDao; } 实体类中的几种方法:

Doctor

修改 正如评论中所讨论的,@Override public int hashCode() { int hash = 0; hash += (doctorId != null ? doctorId.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { if (!(object instanceof Doctor)) { return false; } Doctor other = (Doctor) object; if ((this.doctorId == null && other.doctorId != null) || (this.doctorId != null && !this.doctorId.equals(other.doctorId))) { return false; } return true; } 被称为三次。以下是按顺序进行三次调用的堆栈跟踪:

1

DoctorConverter.getAsObject

2

at java.lang.Thread.dumpStack(Thread.java:1342)
at com.localgp.jsf.converter.DoctorConverter.getAsObject(DoctorConverter.java:34)
at com.localgp.jsf.converter.org$jboss$weld$bean-localgp-server-web-1$0-SNAPSHOT_war-ManagedBean-class_com$localgp$jsf$converter$DoctorConverter_$$_WeldClientProxy.getAsObject(org$jboss$weld$bean-localgp-server-web-1$0-SNAPSHOT_war-ManagedBean-class_com$localgp$jsf$converter$DoctorConverter_$$_WeldClientProxy.java)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:508)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1589)
at javax.faces.component.UIForm.visitTree(UIForm.java:344)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1170)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.localgp.NoCacheFilter.doFilter(NoCacheFilter.java:36)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:785)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:649)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:483)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:454)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:350)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:300)
at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:444)
at com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:308)
at com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:87)
at com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:171)
at com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:143)
at com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:94)
at com.sun.grizzly.http.TaskBase.run(TaskBase.java:193)
at com.sun.grizzly.http.TaskBase.execute(TaskBase.java:175)
at com.sun.grizzly.arp.DefaultAsyncHandler.handle(DefaultAsyncHandler.java:145)
at com.sun.grizzly.arp.AsyncProtocolFilter.execute(AsyncProtocolFilter.java:204)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)

3

at java.lang.Thread.dumpStack(Thread.java:1342)
at com.localgp.jsf.converter.DoctorConverter.getAsObject(DoctorConverter.java:34)
at com.localgp.jsf.converter.org$jboss$weld$bean-localgp-server-web-1$0-SNAPSHOT_war-ManagedBean-class_com$localgp$jsf$converter$DoctorConverter_$$_WeldClientProxy.getAsObject(org$jboss$weld$bean-localgp-server-web-1$0-SNAPSHOT_war-ManagedBean-class_com$localgp$jsf$converter$DoctorConverter_$$_WeldClientProxy.java)
at javax.faces.component.SelectUtils.doConversion(SelectUtils.java:191)
at javax.faces.component.SelectUtils.matchValue(SelectUtils.java:99)
at javax.faces.component.UISelectOne.validateValue(UISelectOne.java:153)
at javax.faces.component.UIInput.validate(UIInput.java:967)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:508)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1589)
at javax.faces.component.UIForm.visitTree(UIForm.java:344)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1170)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.localgp.NoCacheFilter.doFilter(NoCacheFilter.java:36)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:785)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:649)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:483)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:454)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:350)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:300)
at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:444)
at com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:308)
at com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:87)
at com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:171)
at com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:143)
at com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:94)
at com.sun.grizzly.http.TaskBase.run(TaskBase.java:193)
at com.sun.grizzly.http.TaskBase.execute(TaskBase.java:175)
at com.sun.grizzly.arp.DefaultAsyncHandler.handle(DefaultAsyncHandler.java:145)
at com.sun.grizzly.arp.AsyncProtocolFilter.execute(AsyncProtocolFilter.java:204)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)

这是JSF页面中的当前表单:

at java.lang.Thread.dumpStack(Thread.java:1342)
at com.localgp.jsf.converter.DoctorConverter.getAsObject(DoctorConverter.java:34)
at com.localgp.jsf.converter.org$jboss$weld$bean-localgp-server-web-1$0-SNAPSHOT_war-ManagedBean-class_com$localgp$jsf$converter$DoctorConverter_$$_WeldClientProxy.getAsObject(org$jboss$weld$bean-localgp-server-web-1$0-SNAPSHOT_war-ManagedBean-class_com$localgp$jsf$converter$DoctorConverter_$$_WeldClientProxy.java)
at javax.faces.component.SelectUtils.doConversion(SelectUtils.java:191)
at javax.faces.component.SelectUtils.valueIsNoSelectionOption(SelectUtils.java:150)
at javax.faces.component.UISelectOne.validateValue(UISelectOne.java:159)
at javax.faces.component.UIInput.validate(UIInput.java:967)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:508)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1589)
at javax.faces.component.UIForm.visitTree(UIForm.java:344)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1170)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.localgp.NoCacheFilter.doFilter(NoCacheFilter.java:36)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:785)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:649)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:483)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:454)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:350)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:300)
at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.comet.CometEngine.executeServlet(CometEngine.java:444)
at com.sun.grizzly.comet.CometEngine.handle(CometEngine.java:308)
at com.sun.grizzly.comet.CometAsyncFilter.doFilter(CometAsyncFilter.java:87)
at com.sun.grizzly.arp.DefaultAsyncExecutor.invokeFilters(DefaultAsyncExecutor.java:171)
at com.sun.grizzly.arp.DefaultAsyncExecutor.interrupt(DefaultAsyncExecutor.java:143)
at com.sun.grizzly.arp.AsyncProcessorTask.doTask(AsyncProcessorTask.java:94)
at com.sun.grizzly.http.TaskBase.run(TaskBase.java:193)
at com.sun.grizzly.http.TaskBase.execute(TaskBase.java:175)
at com.sun.grizzly.arp.DefaultAsyncHandler.handle(DefaultAsyncHandler.java:145)
at com.sun.grizzly.arp.AsyncProtocolFilter.execute(AsyncProtocolFilter.java:204)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)

修改 已删除的<h:form id="form"> <!-- Validating here instead of at the selectOneMenu level due to its invalidity not being cleared. --> <div style="clear:both;"> <h:panelGrid columns="2"> <h:selectOneMenu id="doctor" value="#{linkDoctorBean.doctor}" converter="#{doctorConverter}" converterMessage="Please select a doctor" style="width:200px;" required="true"> <f:selectItem itemLabel="Select a doctor" itemValue="" noSelectionOption="true"/> <f:selectItems value="#{linkDoctorBean.doctors}" var="doctor" itemLabel="#{stringBean.doctorToString(doctor)}" itemValue="#{doctor}"/> </h:selectOneMenu> <p:message id="doctorMessage" for="doctor"/> </h:panelGrid> </div> <div style="margin-top:20px;"> <h:commandButton value="Link" action="#{linkDoctorBean.submit()}"> <f:ajax execute="@form" render="doctorMessage"/> </h:commandButton> <h:link value="Cancel" outcome="/pages/cpanel/manage-doctors" style="margin-left:10px;"/> </div> </h:form> <f:ajax/>仍被调用三次。

2 个答案:

答案 0 :(得分:6)

您的转换器坏了。它必须根据value参数转换值,而不是UIInput#getSubmittedValue()noSelectionOption值也即通过转换器(作为第3个跟踪提示)。如果您的实际提交值等于noSelectionOption值,则该值被视为无效,因此验证错误。

我还建议其他小改写;空字符串应为空。

@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
    if (modelValue == null) {
        return "";
    }

    if (modelValue instanceof Doctor) {
        Long id = ((Doctor) modelValue).getId();
        return (id != null) ? String.valueOf(id) : null;
    } else {
        throw new ConverterException(new FacesMessage(modelValue + " is not a valid Doctor"));
    }
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
    if (submittedValue == null || submittedValue.isEmpty()) {
        return null;
    }

    try {
        Long id = Long.valueOf(submittedValue);
        return doctorService.find(id);
    } catch (NumberFormatException e) {
        throw new ConverterException(new FacesMessage(submittedValue + " is not a valid Doctor ID"));
    }
}

请注意,我的web.xml中也有此内容:

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

更新:根据评论,感谢<h:messages/>,结果证明是其他需要的东西。最后结果是a <f:viewParam required="true">,它被重新调用了ajax回发。为防止这种情况,请改为使用

<f:viewParam ... required="#{not facesContext.postback}" />

答案 1 :(得分:0)

以下内容:

converter="#{doctorConverter}"

必须写成:

converter="doctorConverter"