0

In JSF 2 you can pass around ids, Strings other things that can be sent as request parameters with either f:viewParam or @ManagedProperty (i.e. discussion here).

However, I would like to pass around objects between views, from view1 to view2. My backing beans are @ViewScoped. I have tried the following:

<f:setPropertyActionListener value="#{view1Bean.myObject}" target="#{view2Bean.myObject}" />

This creates a view2Bean on view1, which no longer exists when viewing view2. So, the view2Bean.myObject is null, because the property was never set on the specific instance of view2bean.

I considered using the Flash scope, which seems to be exactly the tool for this. However, I read BalusC somewhere saying that it is broken in Mojarra, which I'm using.

What is the best way to pass around objects between views?

Community
  • 1
  • 1
Markos Fragkakis
  • 7,499
  • 18
  • 65
  • 103

1 Answers1

1

However, I would like to pass around objects between views, from view1 to view2.

You could just pass the unique ID of the object as a parameter to the next view and attach a Converter to the <f:viewParam>.

<f:viewParam name="id" value="#{bean.entity}" converter="entityConverter" />

I considered using the Flash scope, which seems to be exactly the tool for this. However, I read BalusC somewhere saying that it is broken in Mojarra, which I'm using.

This will as of the current Mojarra version only fail whenever the views are in a different path (folder) due to the way how the Flash cookie works. If they are in the same path (folder) then it should work just fine.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555