Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

jsf 2 - How to deal with JSF messages between page navigations (POST-REDIRECT-GET)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...