【问题标题】:Rendered Attibute not working [duplicate]渲染属性不起作用[重复]
【发布时间】:2015-04-28 23:00:41
【问题描述】:

我有一个带有 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 上选择哪个。

我错过了什么?

【问题讨论】:

    标签: jsf rendered-attribute


    【解决方案1】:

    #{myBean.input} 最初为 false,因此在加载页面时不会呈现组件。您必须从f:ajax 定位h:inputText 的父容器,因为一旦呈现的属性为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-11-05
      • 1970-01-01
      • 2018-07-05
      • 2017-05-31
      • 2011-01-26
      • 2015-12-20
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多