Pass an object between @ViewScoped beans without using GET params

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");

Leave a Comment