3

我已经设法让代码按照我的意图去做,但我不明白为什么这个工作的特定方面。我正在使用 Richfaces 3.3.3 运行 Seam 2.2.2。

这是来自xhtml页面的代码:

...
<h:form id="radiobuttontestform">

    <fieldset>           
        <h:panelGrid columns="3">

            <h:selectOneRadio layout="pageDirection" id="myRadio" value="#{actionBean.myRadioButton}">
                <f:selectItem itemLabel="First" itemValue="0" />
                <f:selectItem itemLabel="Second" itemValue="1" />
                <a4j:support event="onclick" ajaxSingle="true" process="myDropdown" reRender="myDropdown,myCount,test" />
            </h:selectOneRadio>
          <h:panelGrid columns="1" border="0">
            <h:selectOneListbox size="1" id="myDropdown" value="#{actionBean.rowCountPredefined}" disabled="#{actionBean.myRadioButton != '0'}">
                <f:selectItem itemLabel="10" itemValue="10" />
                <f:selectItem itemLabel="20" itemValue="20" />
                <f:selectItem itemLabel="30" itemValue="30" />
            </h:selectOneListbox>

            <h:inputText id="myCount" maxlength="5" value="#{actionBean.rowCountSpecified}" disabled="#{actionBean.myRadioButton != '1'}" required="true">
                <f:validateLongRange minimum="1" maximum="1000" />
                <rich:ajaxValidator event="onkeyup" for="myCount" />
            </h:inputText>
            <rich:message id="errorMessage" for="myCount" ajaxRendered="true" showDetail="false" showSummary="true">
                <f:facet name="errorMarker">ERROR:</f:facet>
            </rich:message>
       </h:panelGrid>

    </h:panelGrid>
    </fieldset>
    <h:outputLabel id="test" value="RadioValue: #{actionBean.myRadioButton}" />

    <a4j:commandButton id="show" value="Show Values in Log" action="#{actionBean.showValues}" />
    <a4j:commandButton id="done" value="Save and end conversation" action="#{actionBean.apply}" />
</h:form>
...

支持 bean 只是一个简单的 POJO,其中包含三个属性的 getter 和 setter。(myRadioButton, rowCountPredefined, rowCountSpecified)

这就是我得到的:(正确的结果)

单选按钮示例 http://katzekat.de/Bilder/radio2.png

单选按钮示例 http://katzekat.de/Bilder/radio.png

这是我的想法:

将 ajaxSingle 设置为 true 意味着服务器上只会处理单选按钮。它旁边的下拉列表不需要验证 - 它始终包含正确的值。我添加了 process="myDropdown" 以便将值持久保存到支持 bean 中,否则当我将单选按钮切换到位置 2 时,下拉菜单将恢复为原始值。(我意识到这只是装饰性的!)我已经用调试器检查了它,它确实在支持 bean 中设置了属性,并且一切都按预期工作。

一旦单选按钮切换到位置 2,就可以在文本框中输入一个值并进行验证。这非常有效,当我将单选按钮切换回位置 1 时,如果该字段处于错误状态,验证错误将被清除。大概是因为错误消息只存在于请求范围内。

当单选按钮位于位置 2 并在文本字段中输入有效值时再次启动调试器显示在切换回位置 1 时支持 bean 没有更新。我也预料到了这一点,因为我只告诉收音机和在服务器上处理的下拉列表。这是我不明白的一点 - 文本字段中的值在回发保持不变。(见上面的第二个链接)如果不在支持 bean 中,这个文本字段的这个值保存在哪里?

4

1 回答 1

2

提交的表单值存储在Apply Request Values阶段的组件中。

例如,在您javax.faces.component.html.HtmlInputText#decode的文本字段调用的案例方法中javax.faces.component.UIInput#setSubmittedValue。提交的值未设置为 bean(如您所料),因为组件未包含在execute部件中。然后 inputText 的渲染器重新显示(写入响应)提交的值。

当验证失败时,它的工作原理几乎相同。提交的值存储在Apply Request Values阶段,然后由于验证失败,未将提交的值设置为 bean(Update Model Values跳过阶段),然后重新显示提交的值。

于 2013-04-23T15:06:34.547 回答