如何在页面加载期间添加 FacesMessage?使用@PostConstruct 似乎不起作用 [英] How can I add FacesMessage during page load? Using @PostConstruct does not seem to work
问题描述
在支持 bean 的@PostConstruct 方法中,我调用了一个 EJB,它可能会返回一些我想通过 p:messages 在页面上显示的消息.但是,即使我添加了 FacesMessages,例如FacesContext.getCurrentInstance().addMessage(...), p:messages 没有被 FacesMessages 更新.
In a backing bean's @PostConstruct method, I make a call to an EJB which might return some messages that I want to display on the page via p:messages. However, even if I add the FacesMessages e.g. FacesContext.getCurrentInstance().addMessage(...), p:messages is not being updated with the FacesMessages.
如果我改为在页面上的操作上调用对 EJB 的调用(假设用户单击页面上的按钮,该按钮调用调用 EJB 的方法,然后添加 FacesMessage(s)),则消息显示按预期使用 p:messages .
If I instead invoke the call to the EJB on an action from the page (say a user clicks a button on the page which invokes a method that calls the EJB and then adds the FacesMessage(s)), then the messags show up using p:messages as expected.
如何在@PostConstruct 期间添加面孔消息并在最初呈现页面时显示它们?
How do I add Faces Messages during @PostConstruct and have them show up when the page is initially rendered?
代码:
Page1Controller.java:
Page1Controller.java:
@ManagedBean
public class Page1Controller
{
@PostConstruct
public void init()
{
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Test Message from @PostConstruct"));
}
public String getValue()
{
return "Some Value";
}
public void triggerMessage(ActionEvent event)
{
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Test Message from Trigger Button"));
}
}
page1.xhtml
page1.xhtml
<h:form>
<p:messages showDetail="true" showSummary="true" autoUpdate="true"/>
<h:outputText value="#{page1Controller.value}"/>
<br/>
<p:commandButton value="Trigger Message"
actionListener="#{page1Controller.triggerMessage}"/>
</h:form>
推荐答案
当消息组件在添加消息之前 呈现时,可能会发生这种情况.
That can happen when the message component is rendered before the message is added.
在您的具体示例中,该 bean 首次被 <h:outputText>
组件引用,因此是在该时刻第一次构造的.但是
组件出现在您的特定示例中 之后
组件,所以 <p:messages>
组件已经呈现,因此显示消息为时已晚.
In your specific example, the bean is referenced for the first time by the <h:outputText>
component and thus constructed for the first time at that moment. But the <h:outputText>
component appears in your specific example after the <p:messages>
component, so the <p:messages>
component is already rendered and thus it's too late to show the message.
您需要以某种方式确保在呈现消息组件之前添加消息.一种方法是使用
.它在 INVOKE_APPLICATION
阶段运行,该阶段在 RENDER_RESPONSE
阶段之前.因此它在 任何 组件被渲染之前运行.因此,这是一个绝佳的机会.
You need to make sure somehow that the message is added before the message component is rendered. One way is using <f:viewAction>
. It runs during INVOKE_APPLICATION
phase which is before RENDER_RESPONSE
phase. Thus it runs before any component is rendered. A perfect opportunity thus.
<f:metadata>
<f:viewAction action="#{bean.onload}" />
</f:metadata>
public void onload() {
// Add message here instead of in @PostConstruct.
}
另见:
- 什么可以<f:metadata>,<f:viewParam>和 <f:viewAction>用于?
这篇关于如何在页面加载期间添加 FacesMessage?使用@PostConstruct 似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!