我使用的表单带有两个单选按钮 (h:selectOneRadio
)、两个文本字段 (h:selectOneRadio
) 和一个重置按钮 (a4j:命令按钮
)。我还使用 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;
}
}
我遇到的问题是:当用户填写两个文本字段并单击重置按钮时,只有一个文本字段(渲染的文本字段)被清除。
可以通过以下步骤重现此问题:
- 单击第一个单选按钮并填写第一个文本字段;
- 点击第二个单选按钮并填写第二个文本字段;
- 点击重置按钮;
- 单击第一个单选按钮。 会被填满
我怀疑单击重置按钮时,组件树上的非渲染文本字段不会更新。
我可以做什么来清理两个文本字段?
我使用的是richfaces 3.3.3
编辑: 是否有一个上下文参数可以确保非渲染组件的更新?
最佳答案
我怀疑你是对的。
解决您的问题的方法可能是不排除文本字段的渲染,而只是将可见性设置为隐藏。这可以通过如下代码轻松完成:
<h:panelGrid columns="2"
style="visibility:#{updateNotRendered.choice1Selected ? 'visible' : 'hidden'}">
<h:outputLabel value="Choice 1 content:" />
<h:inputText value="#{updateNotRendered.content1}" />
</h:panelGrid>
第,
- 马丁
关于java - 未渲染的组件没有更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18148204/