0

我有一个带有 selectOneMenu 和 inputText 的 JSF 页面。

只有在 selectOneMenu 上选择了某些选项时,inputText 才需要出现。

这是JSF代码:

        <span>
            <h:selectOneMenu id="select" value="#{myBean.model.selectValue}" >
                <f:selectItem itemValue="1" itemLabel="1" />
                <f:selectItem itemValue="2" itemLabel="2" />
                <f:selectItem itemValue="3" itemLabel="3" />
                <f:ajax listener="#{myBean.showInput}" render="input" />
            </h:selectOneMenu>
        </span>
        <span>
            <h:inputText id="input" value="#{myBean.model.inputValue}" rendered="#{myBean.input}"/>
        </span>

这是 MyBean 代码:

@ManagedBean(name = "myBean")
public class MyBean {

    public class Model {

        private String selectValue = "";
        private String inputValue = "";

        public String getInputValue() {
            return inputValue;
        }

        public void setInputValue(String inputValue) {
            this.inputValue = inputValue;
        }

        public String getSelectValue() {
            return selectValue;
        }

        public void setSelectValue(String selectValue) {
            this.selectValue = selectValue;
        }
    }

    private Model model = new Model();
    private boolean input = false;

    public Model getModel() {
        return model;
    }

    public void setModel(Model model) {
        this.model = model;
    }

    public boolean isInput() {
        return input;
    }

    public void setInput(boolean input) {
        this.input = input;
    }

    public void showInput() {
        this.input = "3".equals(model.getSelectValue());
    }

}

但是输入永远不会显示。无论在 selectOneMenu 上选择了哪个。

我错过了什么?

4

1 回答 1

1

#{myBean.input}最初为 false,因此在加载页面时不会呈现组件。您必须以h:inputTextfrom的父容器为目标f:ajax,因为一旦渲染属性为false,该输入文本就不再在组件树中并且无法重新渲染。尝试这个

    <span>
        <h:selectOneMenu id="select" value="#{myBean.model.selectValue}" >
            <f:selectItem itemValue="1" itemLabel="1" />
            <f:selectItem itemValue="2" itemLabel="2" />
            <f:selectItem itemValue="3" itemLabel="3" />
            <f:ajax listener="#{myBean.showInput}" render="wrapper" />
        </h:selectOneMenu>
    </span>
    <span>
        <h:panelGroup id="wrapper">
            <h:inputText id="input" value="#{myBean.model.inputValue}" rendered="#{myBean.input}"/>
        </h:panelGroup>
    </span>
于 2015-02-26T14:16:01.470 回答