JSF重置有条件地呈现输入字段

时间:2017-02-20 14:54:24

标签: ajax jsf checkbox conditional view-scope

修改

如果找到了解决方案,但我需要帮助理解它。问题列在下面

每当我尝试重置有条件渲染字段的值时,它都会恢复为先前输入的值而不是空值。

以下是对意外行为的解释:

  1. 复选框为true - >文本字段呈现
  2. 在textfield中输入'test'
  3. 取消选中复选框 - > texfield未呈现
  4. 点击重置按钮 - > checkbox value = true&& textfield value = null
  5. 预期 - >具有空值的inputtextfield
  6. 实际 - >带有'test'值的inputtextfield
  7. 简化示例

    表格

    <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;
        }
    }
    

    旁注

    • 这是omnifaces @ViewScoped annotation
    • 复选框上的@form进程是必要的,因为取消选中复选框不应该重置文本字段的值(只有按钮应该重置它)

    解决方案

    此代码有效

    <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>
    

    但这提出了一些问题:

    1. 为什么我甚至不需要在没有验证错误的情况下添加p:resetInput?
    2. 为什么我需要使用actionListener属性而不是action属性?

0 个答案:

没有答案