java 在 jsf 中通过 ajax 渲染 <h:panelGroup>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16387067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:50:19  来源:igfitidea点击:

rendering <h:panelGroup> by ajax in jsf

javajsf-2

提问by Martin P

I've got this piece of code, which doesn't do exactly what I thought. I try to set property by Ajax and then re-render one component. My .xhtml looks like this

我有这段代码,它并不完全符合我的想法。我尝试通过 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>

the backing bean looks like this

支持 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;
    }       
}

When I re-click the button, the property is not changed. There is a message in serverlog, which says

当我重新单击按钮时,属性不会改变。服务器日志中有一条消息,上面写着

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

Clearly there is some issue with the managed bean as it is created every time there is a request even if it should be view-scoped.

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

What should I change so the panelgroup(id:"result") could change it's visibility? No richfaces or any other technology allowed.

我应该更改什么才能使 panelgroup(id:"result") 可以改变它的可见性?不允许使用富人脸或任何其他技术。

Thank you very much for your answers

非常感谢您的回答

采纳答案by uzvar

This works for me:

这对我有用:

<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>

Bean:

豆角,扁豆:

@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;
    }       
}

Take a look on JSF 2.0 + Ajax Hello World Example

看一看JSF 2.0 + Ajax Hello World 示例

回答by Alexandre Lavoie

You can't render dynamically a JSF component that is not rendered all time. The object must exist in the DOM client side. When using rendered="false", the output is not generated server side.

您无法动态呈现并非始终呈现的 JSF 组件。该对象必须存在于 DOM 客户端。使用 render="false" 时,不会在服务器端生成输出。

Change your code like this :

像这样改变你的代码:

<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>

With that, the DOM will always contain something with the id="result".

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