0

我有两个h:selectManyCheckBoxrequired 属性设置为true。我想要的是两个组件的 required 属性一起工作。

仅当且仅当两个选定项列表均为空时才显示错误消息。

现在我的问题是,如果其中任何一个为空,则会显示该消息。这是我的代码:

            <rich:panel>
                <f:facet name="header">
                    <h:outputText value="Actualización de catálogos"/>
                </f:facet>

                <h:panelGrid columns="4">

                    <h:outputLabel for="actualizarCatalogoPEC"
                                   value="Actualizar catálogos PEC"/>
                    <h:selectBooleanCheckbox id="actualizarCatalogoPEC"
                                             value="#{administrationBean.actualizaTodosPecChecked}">
                        <f:ajax event="click"
                                render="todosCatalogosPEC"/>
                    </h:selectBooleanCheckbox>

                    <h:outputLabel for="actualizarCatalogoSAGARPA"
                                   value="Actualizar catálogos SAGARPA"/>
                    <h:selectBooleanCheckbox id="actualizarCatalogoSAGARPA"
                                             value="#{administrationBean.actualizaTodosSagarpaChecked}">
                        <f:ajax event="click"
                                render="todosCatalogosSAGARPA"/>
                    </h:selectBooleanCheckbox>

                    <a4j:outputPanel id="todosCatalogosPEC">
                        <h:selectManyCheckbox id="selectCatalogosPEC"
                                              disabled="#{administrationBean.actualizaTodosPecChecked}"
                                              required="true"
                                              value="#{administrationBean.catalogosPecSeleccionados}"
                                              requiredMessage="Seleccione al menos un catálogo"
                                              layout="pageDirection">
                            <f:selectItems value="#{administrationBean.catalogosPecOptions}"/>
                        </h:selectManyCheckbox>
                    </a4j:outputPanel>
                    <h:panelGroup/>
                    <a4j:outputPanel id="todosCatalogosSAGARPA">
                        <h:selectManyCheckbox id="selectCatalogosSAGARPA"
                                              disabled="#{administrationBean.actualizaTodosSagarpaChecked}"
                                              required="true"
                                              value="#{administrationBean.catalogosSagarpaSeleccionados}"
                                              requiredMessage="Seleccione al menos un catálogo"
                                              layout="pageDirection" >
                            <f:selectItems value="#{administrationBean.catalogosSagarpaOptions}"/>
                        </h:selectManyCheckbox>
                    </a4j:outputPanel>
                    <h:panelGroup/>

                    <rich:message id="messageCatalogosPEC"
                                  for="selectCatalogosPEC"/>
                    <h:panelGroup/>
                    <rich:message id="messageCatalogosSAGARPA"
                                  for="selectCatalogosSAGARPA"/>
                    <h:panelGroup/>

                    <a4j:commandButton value="Actualizar catálogos"
                                       render="messageCatalogosPEC"
                                       action="#{administrationBean.doActualizaCatalogos}"/>
                </h:panelGrid>
            </rich:panel>

干杯

更新

我已将代码更改如下:(刚刚添加binding和修改required

                    <a4j:outputPanel id="todosCatalogosPEC">
                        <h:selectManyCheckbox id="selectCatalogosPEC"
                                              disabled="#{administrationBean.actualizaTodosPecChecked}"
                                              binding="selectCatalogosPEC"
                                              required="#{empty selectCatalogosSAGARPA.value}"
                                              value="#{administrationBean.catalogosPecSeleccionados}"
                                              requiredMessage="Seleccione al menos un catálogo"
                                              layout="pageDirection">
                            <f:selectItems value="#{administrationBean.catalogosPecOptions}"/>
                        </h:selectManyCheckbox>
                    </a4j:outputPanel>
                    <h:panelGroup/>
                    <a4j:outputPanel id="todosCatalogosSAGARPA">
                        <h:selectManyCheckbox id="selectCatalogosSAGARPA"
                                              binding="selectCatalogosSAGARPA"
                                              disabled="#{administrationBean.actualizaTodosSagarpaChecked}"
                                              required="#{empty selectCatalogosPEC.value}"
                                              value="#{administrationBean.catalogosSagarpaSeleccionados}"
                                              requiredMessage="Seleccione al menos un catálogo"
                                              layout="pageDirection" >
                            <f:selectItems value="#{administrationBean.catalogosSagarpaOptions}"/>
                        </h:selectManyCheckbox>
                    </a4j:outputPanel>

但收到此错误:

jun 01, 2012 10:31:37 AM org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() for servlet [Faces Servlet] in context with path [/SRC_RichFaces] threw exception [java.lang.String cannot be cast to javax.faces.component.UIComponent] with root cause
java.lang.ClassCastException: java.lang.String cannot be cast to javax.faces.component.UIComponent
    at com.sun.faces.application.ApplicationImpl.createComponentApplyAnnotations(ApplicationImpl.java:1941)
    at com.sun.faces.application.ApplicationImpl.createComponent(ApplicationImpl.java:1144)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.createComponent(ComponentTagHandlerDelegateImpl.java:504)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:157)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at org.richfaces.view.facelets.html.BehaviorsAddingComponentHandlerWrapper.applyNextHandler(BehaviorsAddingComponentHandlerWrapper.java:53)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:184)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at org.richfaces.view.facelets.html.BehaviorsAddingComponentHandlerWrapper.applyNextHandler(BehaviorsAddingComponentHandlerWrapper.java:53)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:184)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at org.richfaces.view.facelets.html.BehaviorsAddingComponentHandlerWrapper.applyNextHandler(BehaviorsAddingComponentHandlerWrapper.java:53)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:184)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at org.richfaces.view.facelets.html.BehaviorsAddingComponentHandlerWrapper.applyNextHandler(BehaviorsAddingComponentHandlerWrapper.java:53)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:184)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at org.richfaces.view.facelets.html.BehaviorsAddingComponentHandlerWrapper.applyNextHandler(BehaviorsAddingComponentHandlerWrapper.java:53)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:184)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:86)
    at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:152)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:774)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
4

1 回答 1

2

您需要将它们都绑定到视图和required第一个的属性中,检查UIInput#getSubmittedValue()第二个的是否为空,并在required第二个的属性中,检查UIInput#getValue()第一个的是否为空。

<h:selectManyCheckbox 
    binding="#{selectCatalogosPEC}"
    required="#{empty selectCatalogosSAGARPA.submittedValue}"
>
    ...
</h:selectManyCheckbox>

<h:selectManyCheckbox
    binding="#{selectCatalogosSAGARPA}"
    required="#{empty selectCatalogosPEC.value}"
>
    ....
</h:selectManyCheckbox>

也可以看看:

于 2012-06-01T16:47:56.860 回答