在ActionEvent之后呈现

时间:2013-11-19 21:15:48

标签: jsf rendering

我希望在点击按钮后在页面上呈现inputText,而不会重新加载页面。考虑具有以下组件的facelet 1.xhtml:

<h:commandButton id="show" value="Show" actionListener="#{f.rend}"/>
<h:inputText id="usrname" binding="#{f.usrname}" rendered="false"/>

和托管bean

@Named("f")
@RequestScoped
private HtmlInputText usrname;
//getter,setter
public void rend(ActionListener e){
//How this method must be implemented?
}

1 个答案:

答案 0 :(得分:0)

您需要发送ajax(部分)请求而不是完整请求。您可以在命令组件中使用<f:ajax>。它具有render属性,其中包含需要更新的空间分隔的客户端ID集合。然后,输入组件的rendered属性必须是引用bean属性而不是硬编码值false的动态值表达式。例如。一个boolean财产。您只需将该属性设置为true即可。您不需要也不应该将整个组件绑定到bean。那是不好的做法。

总而言之,这应该做到:

<h:commandButton id="show" value="Show" action="#{bean.setShow(true)}">
    <f:ajax render="group" />
</h:commandButton>
<h:panelGroup id="group">
    <h:inputText id="username" rendered="#{bean.show}" />
</h:panelGroup>

with(假设JSF 2.2,@ViewScoped,原因提到here

import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named
@ViewScoped
public class Bean {

    private boolean show;

    public boolean isShow() {
        return show;
    }

    public void setShow(boolean show) {
        this.show = show;
    }

}