我很难使用PrimeFaces的RequestContext从后面的bean中更新视图。在下面的例子中,我有一个按钮和两个面板。当按下按钮时,我想要更新一个面板,而不是另一个面板。但是它不起作用,我也找不到错误!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;
}
}
}发布于 2014-12-01 04:37:21
问题是您所引用的组件的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(您可以使用浏览器的检查元素看到它)。
因此,如果您想访问Id在同一个p:panel中的Id panela的panela,那么不需要附加表单Id。
但是,如果您想要访问p:panel以外的h:form,那么您需要附加h:form id来访问它。
解决问题的方法是:对您的h:form使用自定义ID (顺便说一句,这是一个最佳实践)并通过附加表单ID来访问p:panel。
<h:form id="panelaForm">
<p:panel id="panela">
....
</p:panel>
</h:form>在托管bean使用中:
RequestContext.getCurrentInstance().update("panelaForm:panela");发布于 2014-11-30 14:23:50
我是这里的新人(Java ),但是下面的解决方案适用于我:
<!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>
https://stackoverflow.com/questions/27212481
复制相似问题