I'm developing JSF 2 web application and using implicit navigation from page to page specifying outcome values and changing url values. I also have some Ajax requests to be able to make changes in some pages without having to submit everything.
My problem comes when, for example, I edit a user's data and save the changes. Managed Bean saves user data and return an String to navigate to the user list. When user is properly saved, a FacesMessage
is added before the action method finishes.
Button
<p:commandButton value="#{msg.UPDATE}" action="#{manager.actionSave}"
ajax="false" />
Code into the method
FacesContext.getCurrentInstance().addMessage(clientId,
new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
However, and even I have an <h:messages />
tag in my main page, nothing is displayed there.
The problem symptoms suggests that you're navigating by a redirect instead of a (default) forward by either the faces-redirect=true
parameter in the outcome, or the <redirect/>
entry in the navigation case, if you're still using legacy navigation rules in faces-config.xml
.
Faces messages are request scoped and are thus only available in the resource served by the current request. When navigating by a redirect, you're basically instructing the webbrowser to create a brand new request on the given URL. The faces messages are not available in that request at all.
If redirecting is really mandatory, e.g. to accomplish the POST-Redirect-GET pattern — which is a Good Thing —, then you need to store the message in the flash scope. You can do that by calling Flash#setKeepMessages()
, passing true
.
context.addMessage(clientId, message);
context.getExternalContext().getFlash().setKeepMessages(true);
Note that this will fail if you're using a Mojarra version older than 2.1.14 and are redirecting to a resource in a different base folder. See also issue 2136.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With