JSF - 用于在页面之间保存数据的适当 bean 范围,但仅限于“浏览器选项卡相关”
我正在使用 JSF 2.2.20 创建一个 Web 应用程序,其中我正在实现一个“有点向导”流程,让用户填写输入字段并通过导航来回查看页面< /强>。我对所有这些视图使用单个 bean。
假设我有视图 A.xhtml、B.xhtml、C.xhtml 和 D.xhtml,全部由同一个 bean MyBean.java 管理
我希望我的应用程序是“浏览器选项卡范围”,这意味着
- 我不希望我的 bean 数据被重新定义在每个 HTTP 之后实例化请求,因为它发生在
@RequestScoped
beans 上,或者在视图更改之后,因为它发生在@ViewScoped
上,我希望在视图更改之间保留我的 bean 的数据和重定向,以便用户可以在页面之间来回移动而不会丢失已经提供的数据。 - 我不想使用
@SessionScoped
范围因为每次用户打开新选项卡时我都希望重新实例化bean来自页面“A.xhtml”。
是否有任何内置方法可以使用当前的JSF版本实现上述场景?如果没有,请问可以 您提出任何解决方法?
提前谢谢
I am creating a web application using JSF 2.2.20 in which I am implementing a "kinda wizard" flow which lets the user filling input fields and go back and forth the view pages through navigation. I am using a single bean for all these views.
Let's say I have views A.xhtml, B.xhtml, C.xhtml and D.xhtml, all managed by the same bean MyBean.java
I want my application to be "browser tab scoped", which means that
- I do not want my bean's data be re-instantiated after every HTTP Request as it happens with
@RequestScoped
beans or after view changing as it happens with@ViewScoped
, I want the data of my bean to be kept between view changes and redirections so the user can go back and forth between pages without losing the data he has already given. - I do not want to use the
@SessionScoped
scope since each time the user opens a new tab I want the bean to be re-instantiated starting from page "A.xhtml.
Is there any built-in way to achieve the scenario described above using the current JSF version? In case there is not any, could you please propose any workarounds?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
@ViewScoped
是您要寻找的,但这取决于您的确切用法。两张注:
Javax.faces.view.view.viewscoped
。不要使用弃用的bean注释,因为它的工作方式有所不同。@ViewScoped
通过将bean存储在视图中来工作。因此,每次加载页面时,都会获得与该视图相对应的视图和视图ID。因此,有效地将页面的每个负载(可以读为“每个浏览器选项卡”)都会获得自己的bean。@ViewScoped
是一个钝化范围。这意味着您的豆子及其注入的依赖项确实需要序列化
。如果发生了页面导航,则可能要使用FlowsCoped。这是一个多页豆,一直活着,直到您结束“流”为止。
如果这两项工作都没有,那么您始终可以实现自己的范围,而CDI则非常容易。
I think
@ViewScoped
is what you are looking for, but it depends on your exact usage.Couple of notes:
javax.faces.view.ViewScoped
. Don't use the deprecated managed bean annotation as it works differently.@ViewScoped
works by storing the beans in the view. So each time you load the page you get a view and a viewId that corresponds to that view. So effectively each load of the page (could be read as 'each browser tab') gets its own bean.@ViewScoped
is a passivating scope. That means your beans, and their injected Dependencies, do need to beSerializable
.If there is a Page Navigation occurring, you probably want to use FlowScoped. This is a multi-page bean that stays alive until you end the 'flow'.
If neither of these two work, you can always implement your own scope which is surprisingly easy with CDI.