Passing a FacesMessage to the next page [duplicate]

大憨熊 提交于 2019-12-18 05:09:09

问题


I am looking for a graceful JSF way to do this and was wondering if there was any commonly accepted practice for doing this.

When I navigate from one page to another, I want the new page to show a FacesMessage in a h:message component (actually p:message but it should be the same thing) that was transmitted from the last page.

So for example, if a user clicks a "Create Document" button it navigates to a new page on success, and shows the success message on the new page. I want all my data-base changing operations to behave that way.

It seems that the FacesContext clears all messages on the navigation, so my closest solution looks like this:

  1. Store the FacesMessage object in the session bean.
  2. Create a method checkMessage in the message bean that calls FacesContext.getCurrentInstance().addMessage(..) and removes the message from the message bean.
  3. Put a EL reference to checkMessage in the destination page.
  4. Put a p:message in the destination page.

This seems a bit forced -- is there a better way of doing this?


回答1:


Faces messages are indeed request scoped.

Just make use of the new JSF 2.0 Flash scope: Flash#setKeepMessages():

context.addMessage(clientId, message);
context.getExternalContext().getFlash().setKeepMessages(true);
// ...

This has only one caveat in the current Mojarra 2.1.13 release: the redirect has to take place in the same "folder" in the URL. This is fixed in the shortly upcoming 2.1.14. See also issue 2136.




回答2:


http://ocpsoft.org/java/persist-and-pass-facesmessages-over-page-redirects/

http://balusc.blogspot.se/2007/03/post-redirect-get-pattern.html

Maybe one of those will help?



来源:https://stackoverflow.com/questions/12481506/passing-a-facesmessage-to-the-next-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!