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 存在一些问题,因为它是在每次有请求时创建的,即使它应该是视图范围的。

我应该改变什么,以便 panelgroup(id:"result") 可以改变它的可见性?不允许使用 Richfaces 或任何其他技术。

非常感谢您的回答

4

2 回答 2

6

您不能动态地呈现并非一直呈现的 JSF 组件。该对象必须存在于 DOM 客户端。使用 render="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" 的内容。

于 2013-05-05T19:28:24.053 回答
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 示例

于 2013-05-05T19:09:26.767 回答