0

我正在使用一个带有两个单选按钮 ( h:selectOneRadio)、两个文本字段 ( h:selectOneRadio) 和一个重置按钮 ( a4j:commandButton) 的表单。单击单选按钮时,我还a4j:support用于重新呈现文本字段。

这种形式的行为必须是这样的:

  • 当用户单击第一个单选按钮时,必须呈现第一个文本字段,而不能呈现第二个文本字段;

  • 当用户点击第二个单选按钮时,情况反转,只需要渲染第二个文本字段;

  • 当用户单击重置按钮时,必须清除两个文本字段的内容。

这是xhtml页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<head/>
<body>
    <h:form>
        <rich:panel style="width:50%">
            <rich:messages showDetail="false" showSummary="true"/> <br />
            <h:panelGrid columns="1">
                <h:selectOneRadio value="#{updateNotRendered.choice}" label="Choose one">
                    <f:selectItem itemValue="CHOICE1" itemLabel="Choice number 1"/>
                    <f:selectItem itemValue="CHOICE2" itemLabel="Choice number 2"/>

                    <a4j:support event="onclick" reRender="pnlFields"/>
                </h:selectOneRadio>

                <h:panelGroup id="pnlFields">
                    <h:panelGrid columns="2" rendered="#{updateNotRendered.choice1Selected}">
                        <h:outputLabel value="Choice 1 content:" />
                        <h:inputText value="#{updateNotRendered.content1}" />
                    </h:panelGrid>

                    <h:panelGrid columns="2" rendered="#{updateNotRendered.choice2Selected}">
                        <h:outputLabel value="Choice 2 content:" />
                        <h:inputText value="#{updateNotRendered.content2}" />
                    </h:panelGrid>
                </h:panelGroup>
                <a4j:commandButton action="#{updateNotRendered.reset}" value="Reset" reRender="pnlFields"/>
            </h:panelGrid>
        </rich:panel>       
        <a4j:log />
        <f:phaseListener type="poc.jsf.richfaces.PhaseL"/>
    </h:form>
</body>
</html>

这是托管 Bean 代码:

public class UpdateNotRendered {
    private String choice;
    private String content1;
    private String content2;

    public String getChoice() {
        return choice;
    }
    public void setChoice(String choice) {
        this.choice = choice;
    }
    public String getContent1() {
        return content1;
    }
    public void setContent1(String content1) {
        this.content1 = content1;
    }
    public String getContent2() {
        return content2;
    }
    public void setContent2(String content2) {
        this.content2 = content2;
    }

    public boolean isChoice1Selected() {
        return choice != null && choice.equals("CHOICE1");
    }

    public boolean isChoice2Selected() {
        return choice != null && choice.equals("CHOICE2");
    }

    public void reset() {
        content1 = null;
        content2 = null;
    }
}

我遇到的问题是:当用户填写两个文本字段并单击重置按钮时,只有一个文本字段,即渲染的文本字段被清除。

可以通过以下步骤重现此问题:

  1. 单击第一个单选按钮并填写第一个文本字段;
  2. 单击第二个单选按钮并填写第二个文本字段;
  3. 点击重置按钮;
  4. 单击第一个单选按钮。会被填满

我怀疑单击重置按钮时未呈现的文本字段在组件树上没有得到更新。

我该怎么做才能清理两个文本字段?

我正在使用 Richfaces 3.3.3

已编辑:是否有一个上下文参数可以确保更新非渲染组件?

4

1 回答 1

2

我怀疑你是对的。

您的问题的解决方法可能是不排除文本字段的呈现,而只是将可见性设置为隐藏。这可以通过如下代码轻松完成:

<h:panelGrid columns="2"
             style="visibility:#{updateNotRendered.choice1Selected ? 'visible' : 'hidden'}">
    <h:outputLabel value="Choice 1 content:" />
    <h:inputText value="#{updateNotRendered.content1}" />
</h:panelGrid>

hth,
-马丁

于 2013-08-09T14:25:45.723 回答