RequestContext不起作用

时间:2014-11-30 10:57:55

标签: jsf jsf-2 primefaces requestcontext

我无法使用PrimeFaces的RequestContext从后面的bean更新视图。在下面的示例中,我有一个按钮和2个面板。按下按钮时,我想更新一个面板,而不是另一个面板。 虽然它不起作用,我找不到错误! requestContext.update("panela");被解雇,但没有完成任务! 非常感谢!

XHTML文件:

<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head/>
    <h:body>
        <h:form>
            <p:panelGrid columns="1">
                <p:commandButton value="Save" actionListener="#{runtimeUpdatesBean.save}" />
                <p:panel id="panela">
                    <h:outputText value="#{runtimeUpdatesBean.texta}"/>
                </p:panel>
                <p:panel id="panelb">
                    <h:outputText value="#{runtimeUpdatesBean.textb}"/>
                </p:panel>
            </p:panelGrid>
        </h:form>
    </h:body>
</html>

豆子:

package com.glasses.primework;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;

@ManagedBean
@SessionScoped
public class RuntimeUpdatesBean {
    private String texta;
    private String textb;
    private boolean outcome;

    public String getTexta() {
        texta += "a";
        System.out.println("RuntimeUpdatesBean.getTexta() = " + texta);
        return texta;
    }

    public String getTextb() {
        textb += "b";
        System.out.println("RuntimeUpdatesBean.getTextb() = " + textb);
        return textb;
    }

    public void save() {
        RequestContext requestContext = RequestContext.getCurrentInstance();

        if(outcome) {
            System.out.println("RuntimeUpdatesBean.save() = update panela");
            requestContext.update("panela");
            outcome = false;
        } else {
            System.out.println("RuntimeUpdatesBean.save() = update panelb");
            requestContext.update("panelb");
            outcome = true;
        }
    }
}

2 个答案:

答案 0 :(得分:4)

问题是你所指的组件的ID 在JSF中,当您将组件放在h:form(或某些Primefaces组件,如TabView)中时,该组件的ID也将基于h:form ID生成。
这是例子:

<h:form id="panelaForm">
   <p:panel id="panela">
       ....
   </p:panel>
</h:form>

在上述情况下,您p:panel的ID将生成为panelaForm:panela 在您的情况下,因为您没有为h:form提供任何ID,所以会附加动态ID,例如j_xyz:panela(您可以使用浏览器的Inspect Element查看它)。

因此,如果您想在同一p:panel内访问ID为panela的{​​{1}},则无需附加表单ID。
但是,如果您想访问h:form以外的p:panel,则需要附加h:form ID才能访问它。

问题的解决方案是:对h:form使用自定义ID(顺便说一句,这是最佳做法..)并通过附加表单ID访问h:form

p:panel

在Managed bean中使用:

<h:form id="panelaForm">
   <p:panel id="panela">
       ....
   </p:panel>
</h:form>

答案 1 :(得分:0)

我是这里的新人(Java EE)但是以下解决方案对我有用:

<!DOCTYPE html>
<html   xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head/>
<h:body>
    <h:form id="form">
        <p:panelGrid columns="1">
            <p:commandButton value="Save" actionListener="#{runtimeUpdatesBean.save}" update=":form" />
            <p:panel id="panela">
                <h:outputText value="#{runtimeUpdatesBean.texta}"/>
            </p:panel>
            <p:panel id="panelb">
                <h:outputText value="#{runtimeUpdatesBean.textb}"/>
            </p:panel>
        </p:panelGrid>
    </h:form>
</h:body>

相关问题