【问题标题】:JSF passthrough radio button selection resets on form validation errorsJSF 直通单选按钮选择在表单验证错误时重置
【发布时间】:2021-04-06 04:02:07
【问题描述】:

我有一个 JSF 2.2 表单,其中包含一个输入文本字段和一对内嵌显示的单选按钮。鉴于 JSF 2.2 中单选按钮组的已知限制,我正在使用 BalusC 在this blog post 中概述的技术。我们无法升级到 JSF 2.3,因为这是一个 Weblogic 应用程序,我们目前锁定在 Weblogic 12.2 (JavaEE 7)。

虽然这种技术在提交有效表单时可以正常工作,但问题是如果提交了无效表单,则用户的单选按钮选择会丢失,并且会重置为最后一个有效值(或初始值)。

下面是我如何定义单选按钮对的示例,它使用 h:inputHidden 元素并将其 binding 属性与各个单选按钮的 name 属性链接(对于它们的组 ID)。

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:jsf="http://xmlns.jcp.org/jsf"
                xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
                
    <div class="form-group">

        <h:outputLabel for="heightInput"
                       value="Height"
                       styleClass="col-xs-6" />
        <div class="col-xs-6">
            <h:inputText id="heightInput"
                         value="#{modelBean.height}"
                         required="true" />
        </div>
        
        <div class="col-xs-12">
        
            <div class="radio-inline">
                <h:outputLabel for="heightCentimeters">
                    <input type="radio"
                           jsf:id="heightCentimeters"
                           pt:name="#{hiddenHeightUnitSelection.clientId}"
                           value="CENTIMETERS"
                           pt:checked="#{modelBean.heightUnit eq 'CENTIMETERS' ? 'checked' : null}" />
                    Centimeters
                </h:outputLabel>
            </div>
            
            <div class="radio-inline">
                <h:outputLabel for="heightInches">
                    <input type="radio"
                           jsf:id="heightInches"
                           pt:name="#{hiddenHeightUnitSelection.clientId}"
                           value="INCHES"
                           pt:checked="#{modelBean.heightUnit eq 'INCHES' ? 'checked' : null}" />
                    Inches
                </h:outputLabel>
            </div>
            
            <h:inputHidden id="heightUnitSelection"
                           binding="#{hiddenHeightUnitSelection}"
                           value="#{modelBean.heightUnit}"
                           rendered="#{facesContext.currentPhaseId.ordinal ne 6}" />
        </div>
    </div>

</ui:composition>

如果提交的表单无效,如何保留用户的单选按钮选择?该模型永远不会随着他们的选择而更新。即使存在验证错误,其他表单元素也会在表单提交中保留其值。如何让我的单选按钮组表现类似?

【问题讨论】:

    标签: jsf radio-button passthrough-attributes


    【解决方案1】:

    确实,checked 属性是直接比较模型值。

    pt:checked="#{modelBean.heightUnit eq 'CENTIMETERS' ? 'checked' : null}"
    

    模型值在UPDATE_MODEL_VALUES阶段更新,但在PROCESS_VALIDATIONS阶段遇到验证错误时不会执行。

    基本上,您要检查提交的值而不是模型值。 UIInput#getValue() 背后的逻辑已经涵盖了这一点。在您的具体情况下,您希望与 &lt;h:inputHidden&gt; 的值进行比较。

    pt:checked="#{hiddenHeightUnitSelection.value eq 'CENTIMETERS' ? 'checked' : null}"
    

    您问题中链接的博客文章已同时更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-25
      • 2016-02-15
      • 1970-01-01
      • 2015-01-04
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多