简体   繁体   中英

Passing objects between Views

In JSF 2 you can pass around ids, Strings other things that can be sent as request parameters with either f:viewParam or @ManagedProperty (ie 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM