Saving JSF Messages After Forwarding

I have a JSF page (using MyFaces 2.0) that collects data on the first display. If he cannot find any information, he must provide a message about this and redirect back to another page. I tried using the solution found Saving FacesMessage after redirection for presentation via <h: message> in JSF (setKeepMessages (true)), but messages do not appear after redirection. The only difference that I can highlight is that I do not use the navigation rule, I call the redirect () call in the external context, because this does not happen in the normal action.

Relevant Code:

public void redirectToPageWithMessage(String inPage, String message, FacesMessage.Severity severity){ getFlash().setKeepMessages(true); addMessage(message, severity); try { getFacesContext().getExternalContext().redirect(inPage); } catch (IOException e) { e.printStackTrace(); } } 

Unfortunately this does not work. The forwarding happens just fine, but <messages / "> doesn't display a message. Is there anything else in the way the () redirection happens that prevents it from working?

+6
source share
3 answers

Code that stores messages is executed after the end of the phase (see Flash.doPostPhaseActions (FacesContext)). Thus, this is not expected to work, but perhaps you can call Flash.doPostPhaseActions before the redirect. Note is not a β€œclean” solution, but it is possible.

+4
source

JSFMessages are saved only to handle the actual request. The second request is executed using redirection, so JSF messages will be lost. EL-Flash is a way around this. This example should work: http://ocpsoft.com/java/persist-and-pass-facesmessages-over-page-redirects/

0
source

I had the same problem and solved it not using ExternalContext.redirect() , but to play with the results for your actions.

That is, my action, called by my buttons, returns a String (result), which indicates the navigation rules for moving to the next page. Messages are saved with this system.

0
source

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


All Articles