我已经设法让代码按照我的意图去做,但我不明白为什么这个工作的特定方面。我正在使用 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 中,这个文本字段的这个值保存在哪里?