How to work with JSF messages between page navigation (POST-REDIRECT-GET)

I am developing a JSF 2 web application and using implicit page-to-page navigation with result values ​​and changing URL values. I also have some Ajax requests to be able to make changes to some pages without having to submit everything.

My problem arises when, for example, I edit user data and save changes. The managed Bean saves the user data and returns a string to go to the user list. When the user is correctly saved, FacesMessage will be added before the action is FacesMessage .

Button

 <p:commandButton value="#{msg.UPDATE}" action="#{manager.actionSave}" ajax="false" /> 

Code in method

 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg)); 

However, and even I have a <h:messages /> on my main page, nothing is displayed there.

+5
source share
1 answer

Symptoms of the problem indicate that you are redirecting instead of (by default) forward using the faces-redirect=true parameter in the results or the <redirect/> entry in case of navigation if you are still using outdated navigation rules in faces-config.xml .

Message faces are request areas and therefore are available only in the resource served by the current request. When navigating redirects, you basically instruct the web browser to create a new request at the given URL. Face reports are not available at all in this request.

If redirection is really mandatory, for example. to execute the POST-Redirect-GET template ; which is a good thing, then you need to save the message in the flash area. You can do this by calling Flash#setKeepMessages() , passing true .

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

Please note that this will fail if you use a version of Mojarra older than 2.1.14 and redirect the resource to another base folder. See also number 2136 .

+9
source

Source: https://habr.com/ru/post/957658/


All Articles