每当我尝试重置有条件渲染字段的值时,它都会恢复为先前输入的值而不是空值。
以下是对意外行为的解释:
表格
<h:form>
<p:selectBooleanCheckbox value="#{testController.inputTextRendered}">
<p:ajax process="@form" update="@form"/>
</p:selectBooleanCheckbox>
<h:inputText value="#{testController.inputText}" rendered="#{testController.inputTextRendered}"/>
<p:commandButton process="@this" update="@form" action="#{testController.reset}"/>
</h:form>
控制器
@Named
@ViewScoped
public class TestController implements Serializable {
private boolean inputTextRendered = true;
private String inputText;
public void reset() {
setInputTextRendered(true);
setInputText(null);
}
public boolean isInputTextRendered() {
return inputTextRendered;
}
public void setInputTextRendered(boolean inputTextRendered) {
this.inputTextRendered = inputTextRendered;
}
public String getInputText() {
return inputText;
}
public void setInputText(String inputText) {
this.inputText = inputText;
}
}
此代码有效
<h:form id="testForm">
<p:selectBooleanCheckbox value="#{testController.inputTextRendered}">
<p:ajax process="@form" update="@form"/>
</p:selectBooleanCheckbox>
<h:inputText value="#{testController.inputText}" rendered="#{testController.inputTextRendered}"/>
<p:commandButton process="@this" update="@form" actionListener="#{testController.reset}">
<p:resetInput target="@form"/>
</p:commandButton>
</h:form>
但这提出了一些问题: