2

After a customer is successfully added in my JSF application on the page add_customer.jsf, the user is redirected to the page list_customers.jsf.

On this page I would like a success message to be shown. The message should only be shown if the user is really coming from the add_customer.jsf page.

How can I do this?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Ziko
  • 135
  • 1
  • 4
  • 10
  • Possible duplicate of [preserving-facesmessage-after-redirect-for-presentation-through-hmessage-in-j](http://stackoverflow.com/questions/5137601/preserving-facesmessage-after-redirect-for-presentation-through-hmessage-in-j) and look this question for detailed answer [adding-faces-message-to-redirected-page-using-externalcontext-redirect](http://stackoverflow.com/questions/10595760/adding-faces-message-to-redirected-page-using-externalcontext-redirect) – Ravi Kadaboina Aug 18 '12 at 23:51

2 Answers2

0

One approach is to add a message to The Flash in the action method that adds the customer.

You don't need to explicitly check that the user was coming from add_customer.jsf, if it's that page (or actually the code behind it) that sets the message. On the destination page you only need to have an <h:messages> component.

A co-worker of my posted an example CRUD app that does exactly this at: Sample CRUD application with JSF.

The relevant code is in the backing bean UserEdit and the page where the redirect happens to.

Note that if your JSF implementation is Mojarra, The Flash only works between pages in the same directory. If this bothers you, please consider voting for this issue: http://java.net/jira/browse/JAVASERVERFACES-2136

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • i tried your method ,but it shows me the error : the method getFlash() is undefined for the type ExternalContext . – Ziko Aug 19 '12 at 14:09
  • Are you really using JSF 2, or having JSF 2 on your build path? – Arjan Tijms Aug 19 '12 at 14:39
  • I have jsf-api ,jsf-impl ,jstl ,standard on my build path – Ziko Aug 19 '12 at 15:44
  • That sounds old. The current Mojarra impl. is one jar. To which AS do you deploy? – Arjan Tijms Aug 19 '12 at 16:02
  • jsf2 implement Mojarra impl ,isn't it ? do you have a link to download Mojarra impl ! – Ziko Aug 19 '12 at 16:16
  • No, it's the other way around: Mojarra implements JSF 2. Do you know which AS (server) you are using? Since extended troubleshooting is discouraged in comments, you might want to start a new question about how to install/setup JSF and/or how to discover what you have running. – Arjan Tijms Aug 19 '12 at 16:27
  • i'm using tomcat server 7 ,and i'm sorry for the troubleshooting ,because it's my first project with jsf ! – Ziko Aug 19 '12 at 18:45
  • You can download the latest JSF (Mojarra) release here: http://javaserverfaces.java.net/download.html goodluck! – Arjan Tijms Aug 19 '12 at 19:21
  • @Arjan, you can still get the current version as separate jars from their maven [repo](https://maven.java.net/content/repositories/releases/com/sun/) – Ravi Kadaboina Aug 19 '12 at 19:27
0

The best way to do it would be using a <h:message> tag with which you could bind a message which can be populated before you redirect to list_customer.jsf.

We have been using this approach for showing messages in our application from last 3-4 years and occasionally used partial triggers to refresh on the same page.

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63