How to show faces message in the redirected page
Asked Answered
L

1

29

I have provided an interface that allows users to create accounts for an application. At the end of the process the registration method performs the registration process adds a success message to the page and then navigates the user to a central data table that displays all the system users.

The problem I have is that the success message never gets displayed because of the page redirect. I can't have a wait() in the method because the JSF message won't get displayed until the method has completed. Ideally I want the success message to be displayed and then a specified time later the page is redirected.

How can I achieve this?

Lonergan answered 3/12, 2012 at 14:47 Comment(0)
T
61

Keep the message in the flash scope. It'll survive the redirect.

context.addMessage(clientId, message);
externalContext.getFlash().setKeepMessages(true);
return "users.xhtml?faces-redirect=true";

Note that older Mojarra versions have some peculiar Flash scope related bugs:

You'd best to upgrade to a minimum of Mojarra 2.1.27 / 2.2.5 in order to ensure that your application is not affected by this.

Tussock answered 3/12, 2012 at 14:52 Comment(9)
Thanks BalusC, do I need to call an update for messages on the new page or should they be triggered by default when the page loads?Lonergan
No, a redirect creates a synchronous request, so there's no means of a partial render. It's just been rendered as a brand new GET request. So all you need to have is just a <h:message> or <h:messages>.Tussock
Thank very much. Just noticed im on Majorra 2.1.6 so Ill need to quickly upgrade to get it to work? Is 2.1.14 stable yet?Lonergan
Only if the target page is in a different base folder. Mojarra is currently already at 2.1.15. If you can't upgrade, your best bet is to reinvent the flash scope yourself: 1) generate some unique ID. 2) store message in session scope with unique ID as key. 3) store unique ID as cookie value. 4) in preRenderView event of redirected page, check if cookie is present and if so then use it as session attribute key to get message from session (and don't forget to remove both the cookie and the session attribute).Tussock
Ive upgraded and it works great. Just out of curiosity, this technique is to bring the message to the new page. Is it possible to 'delay' the redirect as per the original question?Lonergan
You could conditionally render a <meta http-equiv="refresh" content="3;url=users.xhtml"> on a synchronous postback, or execute a setTimeout(function() { window.location = 'users.xhtml'; }, 3000) callback script on an asynchronous postback. Either way, it'll redirect after 3 seconds.Tussock
@Tussock I want to clear the message after it has been passed to the next page. i.e. after it is shown. Can you help me how to do it?Bethea
Excellent. It works like a charm, and it is a useful feature for a log out message to be shown on the welcome page. Upvoted.Calvities
@Tom: You'll have to manually remove it. And when the enduser opens multiple pages on the same session within a second, you'll also risk that the message is shown/removed via the wrong page. When passing message via flash scope instead of session scope, these concerns are solved.Tussock

© 2022 - 2024 — McMap. All rights reserved.