简体   繁体   English

在视图之间传递对象

[英]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 ). 在JSF 2中,您可以传递id,String以及可以通过f:viewParam@ManagedProperty作为请求参数发送的其他内容(即此处的讨论)。

However, I would like to pass around objects between views, from view1 to view2. 但是,我想在视图之间(从view1到view2)传递对象。 My backing beans are @ViewScoped . 我的支持bean是@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. 这将在view1上创建一个view2Bean ,在查看view2时不再存在。 So, the view2Bean.myObject is null , because the property was never set on the specific instance of view2bean . 因此, view2Bean.myObjectnull ,因为从未在view2bean的特定实例上设置该属性。

I considered using the Flash scope, which seems to be exactly the tool for this. 我考虑过使用Flash范围,这似乎正是它的工具。 However, I read BalusC somewhere saying that it is broken in Mojarra, which I'm using. 但是,我在某处阅读了BalusC,说它在Mojarra中已损坏,我正在使用它。

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. 但是,我想在视图之间(从view1到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> . 您可以将对象的唯一ID作为参数传递给下一个视图,并将Converter附加到<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. 我考虑过使用Flash范围,这似乎正是它的工具。 However, I read BalusC somewhere saying that it is broken in Mojarra, which I'm using. 但是,我在某处阅读了BalusC,说它在Mojarra中已损坏,我正在使用它。

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. 从当前的Mojarra版本开始,只有当视图位于不同路径(文件夹)时,才会因Flash cookie的工作方式而失败。 If they are in the same path (folder) then it should work just fine. 如果它们在同一路径(文件夹)中,则应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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