在jsf </h:panelgroup>中通过ajax渲染<h:panelgroup>

时间:2013-05-05 17:20:32

标签: java jsf-2

我有这段代码,这与我的想法完全不同。我尝试通过Ajax设置属性,然后重新渲染一个组件。我的.xhtml看起来像这样

    <h:form>
        <h:commandButton value="Render!" >
            <f:ajax render=":result" listener="#{eFindUser.findUser}" />
        </h:commandButton>

    </h:form>

    <h:panelGroup id="result" layout="block" rendered="#{eFindUser.responseRendered}" >
        This is some text that is supposed to be rendered later
    </h:panelGroup>

支持bean看起来像这样

@Named(value = "eFindUser")
@ViewScoped
public class EFindUserManagedBean implements Serializable{
    private boolean responseRendered = false;
    /**
     * Creates a new instance of EFindUserManagedBean
     */

    public EFindUserManagedBean() {
    }

    public void findUser(AjaxBehaviorEvent abe) {
        responseRendered = !responseRendered;
        System.out.println("finding..... ("+ responseRendered+")" + this);
    }
    public boolean isResponseRendered() {
        return responseRendered;
    }

    public void setResponseRendered(boolean responseRendered) {
        this.responseRendered = responseRendered;
    }       
}

当我重新点击按钮时,属性不会更改。 serverlog中有一条消息,上面写着

INFO: finding..... (true)backingbeans.EFindUserManagedBean@5736b751
INFO: finding..... (true)backingbeans.EFindUserManagedBean@23959d6f

很明显,托管bean存在一些问题,因为每次有请求时都会创建它,即使它应该是视图范围的。

我应该更改哪些内容组(ID:“结果”)可以更改其可见性?不允许使用丰富的表面或任何其他技术。

非常感谢您的回答

2 个答案:

答案 0 :(得分:6)

您无法动态渲染未全时渲染的JSF组件。该对象必须存在于DOM客户端。使用rendered =“false”时,输出不会在服务器端生成。

更改您的代码:

<h:form>
    <h:commandButton value="Render!" >
        <f:ajax render=":result" listener="#{eFindUser.findUser}" />
    </h:commandButton>

</h:form>

<h:panelGroup id="result">
    <h:panelGroup layout="block" rendered="#{eFindUser.responseRendered}" >
        This is some text that is supposed to be rendered later
    </h:panelGroup>
</h:panelGroup>

这样,DOM将始终包含id =“result”的内容。

答案 1 :(得分:2)

这对我有用:

<ui:composition>
<h:form id="mainForm">
        <h:commandButton value="Render!" action="#{eFindUser.findUser}">
            <f:ajax event="action"  render=":result" />
        </h:commandButton>
</h:form>

    <h:panelGroup id="result" layout="block" rendered="#{eFindUser.responseRendered}" >
        This is some text that is supposed to be rendered later
    </h:panelGroup>

</ui:composition>

豆:

@ManagedBean
@ViewScoped
public class EFindUser implements Serializable{
    private static final long serialVersionUID = -7106162864352727534L;
    private boolean responseRendered = false;

    public EFindUser() {
    }

    public void findUser() {
        responseRendered = !responseRendered;
        System.out.println("finding..... ("+ responseRendered+")" + this);
    }
    public boolean isResponseRendered() {
        return responseRendered;
    }

    public void setResponseRendered(boolean responseRendered) {
        this.responseRendered = responseRendered;
    }       
}

查看JSF 2.0 + Ajax Hello World Example