17

I have a browse.xhtml where I browse a list of cars and I want to view the details of the car in details.xhtml when a "View more" button is pressed. Their backing beans are @ViewScoped and are called BrowseBean and DetailsBean, respectively.

Now, I wouldn't like the user/client to see the car ID in the URL, so I would like to avoid using GET params, as presented here and here.

Is there any way to achieve this? I'm using Mojarra 2.2.8 with PrimeFaces 5 and OmniFaces 1.8.1.

1 Answer 1

45

Depends on whether you're sending a redirect or merely navigating.

If you're sending a redirect, then put it in the flash scope:

Faces.setFlashAttribute("car", car);

This is available in the @PostConstruct of the next bean as:

Car car = Faces.getFlashAttribute("car");

Or, if you're merely navigating, then put it in the request scope:

Faces.setRequestAttribute("car", car);

This is available in the @PostConstruct of the next bean as:

Car car = Faces.getRequestAttribute("car");

See also:

Note that I assume that you're very well aware about the design choice of having two entirely separate views which cannot exist (be idempotent) without the other view, instead of having e.g. a single view with conditionally rendered content. And that you already know how exactly the view should behave when it's actually being requested idempotently (i.e. via a bookmark, shared link, by a searchbot, etc). If not, then I strongly recommend to carefully read the answer on this question: How to navigate in JSF? How to make URL reflect current page (and not previous one).


Update: in case you're not using OmniFaces, use respectively the following:

FacesContext.getCurrentInstance().getExternalContext().getFlash().put("car", car);
Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("car");
FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("car", car);
Car car = (Car) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car");
10
  • Thanks for your reply, but I am getting a ClassNotFoundException on Faces.setFlashAttribute("car", car);, here is the trace. The omnifaces library scope, is set to compile, is that correct? (Tried setting it to runtime, and I got the same exception again)
    – Chris
    Commented Sep 6, 2014 at 9:57
  • OmniFaces is missing in runtime classpath. Apparently the Maven build failed somehow. OmniFaces must (exactly like PrimeFaces) end up in /WEB-INF/lib folder of the build. The default scope (compile) should work fine.
    – BalusC
    Commented Sep 6, 2014 at 10:07
  • I've tried mvn compile and it has completed successfuly. This is the scope I have now. I even declared it explicitly at the pom.xml, but I am still getting the exception. Should I download the jar and add it to the WEB-INF/lib folder like with Primefaces, or post a new question to resolve this?
    – Chris
    Commented Sep 6, 2014 at 10:37
  • This is indeed basically an unrelated problem. But how exactly did you install PrimeFaces? Are you saying that you manually placed PrimeFaces JAR in /WEB-INF/lib? And thus effectively you're not using Maven to build?
    – BalusC
    Commented Sep 6, 2014 at 10:40
  • The thing is that I haven't succeeded in installing Primefaces, as reported here, so I downloaded the primefaces-5.0.jar into the WEB-INF/lib folder. I probably have messed up my configuration, this is really the first time working with all those tools and using Java EE.
    – Chris
    Commented Sep 6, 2014 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.