【问题标题】:Rendering validation messages RichFaces渲染验证消息 RichFaces
【发布时间】:2012-04-22 01:40:42
【问题描述】:

我有一个表单,我有一个 rich:select,无论他们选择选项 A 还是选项 B,都会呈现 h:inputText。 最初,与选项 A 相对应的 h:inputText 被渲染,如果我将其留空,则会出现 requiredMessage 并且一切正常。 现在,当我选择 rich:select 的第二个选项时,会呈现选项 B 的 h:inputText,但如果我将其留空,则不会出现 requiredMessage

我错过了什么?

我的xhtml代码如下:

                <h:panelGrid columns="3">
                    <h:outputLabel for="tipoPersona">Tipo de persona</h:outputLabel>
                    <rich:select id="tipoPersona"
                                 required="true" value="#{userInformationController.tipoPersona}"
                                 requiredMessage="Seleccione el tipo de persona"
                                 defaultLabel="Seleccione una opción">
                        <f:selectItems value="#{userInformationController.tipoPersonaOpciones}"/>
                        <f:ajax event="blur" render="tipoPersonaMessage"/>
                        <f:ajax event="selectitem"
                                render="outputLabelCURP inputTextCURP messageCURP outputLabelRFC inputTextRFC messageRFC"
                                listener="#{userInformationController.doRenderCURP}"/>
                    </rich:select>
                    <rich:message id="tipoPersonaMessage" ajaxRendered="true"
                                  for="tipoPersona"/>


                    <a4j:outputPanel id="outputLabelCURP">
                        <h:outputLabel for="CURP" value="CURP"
                                       rendered="#{userInformationController.curpRendered}"/>
                    </a4j:outputPanel>                        
                    <a4j:outputPanel id="inputTextCURP">
                        <h:inputText id="CURP" required="true"
                                     requiredMessage="Introduzca el CURP"
                                     rendered="#{userInformationController.curpRendered}">
                            <f:ajax event="blur" render="curpMessage"/>
                        </h:inputText>
                    </a4j:outputPanel>
                    <a4j:outputPanel id="messageCURP">
                        <rich:message id="curpMessage" ajaxRendered="true"
                                      for="CURP"
                                      rendered="#{userInformationController.curpRendered}"/>
                    </a4j:outputPanel>


                    <a4j:outputPanel id="outputLabelRFC">
                        <h:outputLabel for="RFC" value="RFC"
                                       rendered="#{not userInformationController.curpRendered}"/>
                    </a4j:outputPanel>
                    <a4j:outputPanel id="inputTextRFC">
                        <h:inputText id="RFC" required="true"
                                     requiredMessage="Introduzca el RFC"
                                     rendered="#{not userInformationController.curpRendered}">
                            <f:ajax event="blur" render="rfcMessage"/>
                        </h:inputText>
                    </a4j:outputPanel>
                    <a4j:outputPanel id="messageRFC">
                        <rich:message id="rfcMessage" ajaxRendered="true"
                                      for="RFC"
                                      rendered="#{not userInformationController.curpRendered}"/>
                    </a4j:outputPanel>
                </h:panelGrid>

更新

我在 UI 上做了一些更改,现在我有了这个

            <fieldset>
                <legend>Datos SURI</legend>
                <h:panelGrid columns="3">
                    <h:outputLabel for="tipoPersona">Tipo de persona</h:outputLabel>
                    <rich:select id="tipoPersona"
                                 required="true" value="#{userInformationController.tipoPersona}"
                                 requiredMessage="Seleccione el tipo de persona"
                                 defaultLabel="Seleccione una opción">
                        <f:selectItems value="#{userInformationController.tipoPersonaOpciones}"/>
                        <f:ajax event="blur" render="tipoPersonaMessage"/>
                        <f:ajax event="selectitem"
                                render="outputLabelCURP inputTextCURP messageCURP 
                                        outputLabelRFC inputTextRFC messageRFC
                                        datosPersonaFisica datosPersonaMoral"
                                listener="#{userInformationController.doRenderTipoPersona}"/>
                    </rich:select>
                    <rich:message id="tipoPersonaMessage" ajaxRendered="true"
                                  for="tipoPersona"/>


                    <a4j:outputPanel id="outputLabelCURP">
                        <h:outputLabel for="CURP" value="CURP"
                                       rendered="#{userInformationController.personaFisicaRendered}"/>
                    </a4j:outputPanel>                        
                    <a4j:outputPanel id="inputTextCURP">
                        <h:inputText id="CURP" required="true"
                                     requiredMessage="Introduzca el CURP"
                                     value="#{userInformationController.curp}"
                                     rendered="#{userInformationController.personaFisicaRendered}">
                            <f:ajax event="blur" render="curpMessage identificadorSURI"
                                    listener="#{userInformationController.doSearchIdSURI}"/>
                        </h:inputText>
                    </a4j:outputPanel>
                    <a4j:outputPanel id="messageCURP">
                        <rich:message id="curpMessage" ajaxRendered="true" for="CURP"
                                      rendered="#{userInformationController.personaFisicaRendered}"/>
                    </a4j:outputPanel>


                    <a4j:outputPanel id="outputLabelRFC">
                        <h:outputLabel for="RFC" value="RFC"
                                       rendered="#{userInformationController.personaMoralRendered}"/>
                    </a4j:outputPanel>
                    <a4j:outputPanel id="inputTextRFC">
                        <h:inputText id="RFC" required="true"
                                     requiredMessage="Introduzca el RFC"
                                     value="#{userInformationController.rfc}"
                                     rendered="#{userInformationController.personaMoralRendered}">
                            <f:ajax event="blur" render="rfcMessage identificadorSURI"
                                    listener="#{userInformationController.doSearchIdSURI}"/>
                        </h:inputText>
                    </a4j:outputPanel>
                    <a4j:outputPanel id="messageRFC">
                        <rich:message id="rfcMessage" ajaxRendered="true" for="RFC"
                                      rendered="#{userInformationController.personaMoralRendered}"/>
                    </a4j:outputPanel>

                    <h:outputLabel for="identificadorSURI">Identificador SURI</h:outputLabel>
                    <h:inputText id="identificadorSURI" disabled="true"
                                 value="#{userInformationController.identificadorSuri}"/>

                </h:panelGrid>
            </fieldset>

但是,我发现将我的 Bean 放入 RequestScope 不会触发两个 h:inputText(id='CURP' 和 id='RFC')上的侦听器,而如果我将 bean 放入 ViewScope 监听器会被正确触发和处理。

我需要在 RequestScope 中实际拥有 Bean,因为无论用户是否更改 rich:select 的值,我都需要创建一个全新的表单。如果我将范围保留在 ViewScope 中,则所有数据都保留在 Bean 中,然后当用户更改选择的值时,我将不得不删除这些字段。

有人可以向我解释为什么会这样吗?我不知道有什么诀窍!

干杯

【问题讨论】:

    标签: jsf richfaces validation mojarra


    【解决方案1】:

    在每一个请求上重新创建一个请求范围的 bean,在每一个 ajax 请求上也是如此。因此,当您通过 ajax 将属性更改为默认值以外的新值时(例如,负责 rendered 属性的值),则此更改将不会保留在后续请求中。因此rendered 属性有效地评估为false,并且组件及其所有子组件在提交期间将不再被处理。

    视图范围旨在解决这个问题。只要您通过 ajax 请求与同一个视图交互,bean 就会一直存在,直到您导航到不同的视图或重新创建视图。至于&lt;rich:select&gt;更改时重新创建表单的功能需求,只需在doRenderTipoPersona()方法内部进行即可。

    如果您确实需要坚持请求范围,那么您需要根据 (post) 构造函数中的请求参数映射手动保留 bean 的属性。

    【讨论】:

    • 谢谢。这就是我的想象(ajax 创建新请求)。我所做的是将我的 bean 保留在 View Scope 中并清除 &lt;rich:select&gt; listener 上的所有 bean 字段。
    【解决方案2】:

    你可以使用单个 ajax 函数来代替事件函数

    <rich:select id="tipoPersona"
                                     required="true" value="#{userInformationController.tipoPersona}"
                                     requiredMessage="Seleccione el tipo de persona"
                                     defaultLabel="Seleccione una opción">
                            <f:selectItems value="#{userInformationController.tipoPersonaOpciones}"/>
                           <f:ajax event="valueChange"
                                    render="tipoPersonaMessage outputLabelCURP inputTextCURP messageCURP outputLabelRFC inputTextRFC messageRFC"
                                    listener="#{userInformationController.doRenderCURP}"/>
                        </rich:select>
    

    【讨论】:

    • 感谢您的回答。也许我解释自己错了,但问题不在于 rich:select 问题在于 h:inputText(id='CURP' 和 id='RFC' )。当blur 事件被触发时,我希望监听器被调用,但它没有。
    【解决方案3】:

    嗯,这是一个解决方案。

    <a4j:jsFunction name="onBlurRFC" execute="RFC" render = "rfcMessage identificadorSURI" 
        action="#{userInformationController.doSearchIdSURI}" />     
    
        <h:inputText id="RFC" required="true"
                                                 requiredMessage="Introduzca el RFC"
                                                 value="#{userInformationController.rfc}"
                                                 rendered="#{userInformationController.personaMoralRendered}" onblur="onBlurRFC()">
                                    </h:inputText>
    

    【讨论】:

    • 抱歉,缺少 a4j:jsFunction。 a4j:jsFunction 将在 bean 上触发侦听器并渲染面板。 CURP 输入文本也会这样做。
    猜你喜欢
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 2011-08-24
    • 2011-03-12
    • 2011-11-26
    • 2012-02-25
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多