我正在使用primefaces 3.5,我无法弄清楚如何在下一页咆哮消息。例如,我想在数据库中添加一条记录,之后我将重定向到另一个页面,在那里我要显示一条咆哮消息“记录已成功添加!” 我试过这样的事情:
public String addLabelInDB() {
try {
//logic to add a record in DB
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!", "Label has been added with success!"));
} catch (Exception e) {
logger.debug(e.getMessage());
}
return "listLabelsPage";
}
并在listLabelsPage.xhtml中我有:
<p:growl id="msgs" showDetail="true" autoUpdate="true"/>
但它不起作用。 我猜这个消息会丢失,因为是另一个请求还是什么?是否有可能根据请求存储消息并在下一页显示?谢谢!
答案 0 :(得分:1)
您可以在正在加载的 listLabelsPage.xhtml 页面上设置preRender
<f:event type="preRenderView" listener="#{yourBean.showGrowl}" />
和只有
的showGrowl方法public void showGrowl() {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!", "Label has been added with success!"));
}
答案 1 :(得分:0)
我发布了自己问题的答案,以帮助其他人像我一样面对同样的问题:
public String addLabelInDB() {
try {
//some logic to insert in db
//below I set a flag on context which helps me to display a growl message only when the insertion was done with success
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.getRequestMap().put("addedWithSuccess","true");
} catch (Exception e) {
logger.debug(e.getMessage());
}
return "listLabelsPage";
}
public void showGrowl() {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
String labelAddedWithSuccess = (String) ec.getRequestMap().get("addedWithSuccess");
//if the flag on context is true show the growl message
if (labelAddedWithSuccess!=null && labelAddedWithSuccess.equals("true")) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success!", "Label has been added with success!"));
}
}
在我的xhtml中我有:
<f:event type="preRenderView" listener="#{labelsManager.showGrowl}" />
答案 2 :(得分:0)
这个怎么样?制作一个单独的重定向按钮,在显示msg后会被点击:
HTML:
<h:form prependId="false">
<p:growl />
<p:button outcome="gotoABC" id="rdr-btn" style="display: none;" />
<p:commandButton action="#{bean.process()}" update="@form" />
</form>
豆:
public void process(){
addInfoMsg(summary, msgDetail); //Add msg func
RequestContext.getCurrentInstance().execute("setTimeout(function(){ $('#rdr-btn').click(); }, 3000);"); // 3 seconds delay. I put the script in Constants to config later.
}