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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
tarka
  • 5,289
  • 10
  • 51
  • 75

1 Answers1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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? – tarka Dec 03 '12 at 15:05
  • 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 `` or ``. – BalusC Dec 03 '12 at 15:05
  • 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? – tarka Dec 03 '12 at 15:10
  • 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). – BalusC Dec 03 '12 at 15:18
  • 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? – tarka Dec 03 '12 at 15:32
  • You could conditionally render a `` 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. – BalusC Dec 03 '12 at 15:36
  • @BalusC 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? – Keerthivasan Jul 18 '14 at 07:38
  • 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. –  Dec 15 '15 at 16:18
  • @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. – BalusC Nov 11 '19 at 21:36