RequestContext将不起作用 [英] RequestContext won't work
问题描述
我无法使用PrimeFaces的RequestContext
从背面的Bean更新视图.在下面的示例中,我有一个按钮和2个面板.按下按钮时,我要更新一个面板,而不要更新另一个面板.
虽然它不起作用,但我找不到错误! requestContext.update("panela");
被解雇,但没有执行!
非常感谢您的帮助!
I am having trouble to update the view from the bean in the back using PrimeFaces's RequestContext
. In the example below I have a button and 2 panels. When pressing the button, I want to update one panel, but not the other one.
It does not work though and I can't find the error! requestContext.update("panela");
is fired, but doesn't do its job!
Help greatly appreciated!
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;
}
}
}
推荐答案
好吧,这是您所引用的组件的ID.
在JSF中,当您将组件放置在h:form
(或某些Primefaces组件,例如TabView)中时,该组件的ID也将基于h:form
id生成.
这是示例:
Well the problem is the ID of the component that you are referring.
In JSF when you place a component inside h:form
(or Some Primefaces components like TabView), that component's Id will be generated based on the h:form
id too.
Here is the Example:
<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元素查看它).
In the above case your p:panel
's id will be generated as panelaForm:panela
.
In your case since you haven't provided any ID for h:form
a dynamic id will be attached like for example j_xyz:panela
(you can see it using you browser's Inspect Element).
因此,如果您要使用同一h:form
中的ID为panela
的p:panel
进行访问,则无需附加ID.
但是,如果要访问h:form
之外的p:panel
,则需要附加h:form
id进行访问.
So If you wan to access p:panel
with Id panela
inside the same h:form
then no need to attach the form Id.
But If you want to access the p:panel
outside h:form
then you need to attach the h:form
id to access it.
为您解决的问题是:在h:form
中使用自定义ID(顺便说一句,这是最佳做法.)并通过附加该表单ID来访问p:panel
.
Solution to you problem is: use an custom ID to your h:form
(which is a best practice by the way..) and access the p:panel
by attaching that form ID.
<h:form id="panelaForm">
<p:panel id="panela">
....
</p:panel>
</h:form>
在Managed bean中使用:
And in Managed bean use:
RequestContext.getCurrentInstance().update("panelaForm:panela");
这篇关于RequestContext将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!