如何在下一页显示 primefaces 咆哮消息?
How to show a primefaces growl message on the next page?
我使用的是 primefaces 3.5,但我不知道如何在下一页上发出消息。例如,我想在数据库中添加一条记录,然后我重定向到另一个页面,我想在该页面上显示带有 "The record has been added with success!" 的咆哮消息
我试过这样的事情:
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"/>
但它不起作用。
我想消息会丢失,因为是另一个请求还是什么?是否有可能根据要求存储消息并在下一页显示?谢谢!
您可以在您正在加载的 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!"));
}
我 post 回答我自己的问题,以帮助像我一样面临同样问题的其他人:
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}" />
这个怎么样?制作一个单独的重定向按钮,显示消息后将被点击:
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.
}
我使用的是 primefaces 3.5,但我不知道如何在下一页上发出消息。例如,我想在数据库中添加一条记录,然后我重定向到另一个页面,我想在该页面上显示带有 "The record has been added with success!" 的咆哮消息 我试过这样的事情:
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"/>
但它不起作用。 我想消息会丢失,因为是另一个请求还是什么?是否有可能根据要求存储消息并在下一页显示?谢谢!
您可以在您正在加载的 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!"));
}
我 post 回答我自己的问题,以帮助像我一样面临同样问题的其他人:
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}" />
这个怎么样?制作一个单独的重定向按钮,显示消息后将被点击:
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.
}