Managed-Beans 和作用域 - 如何处理它?

发布于 2024-10-22 06:00:56 字数 532 浏览 1 评论 0原文

我想做这样的事情:index.xhtml -> createPerson.xhtml -> addAddress.xhtml->索引.xhtml。

在 Managed-Bean CreatePerson 中,将创建、填充并保存新的 Person-Object,在 Managed-Bean AddAddress 中,我需要 Person-Object 才能向此人添加地址,在此之后,它应该导航回起点。我认为,托管Bean CreatePerson 应该是@SessionScoped,因为我必须将Person-Object 注入到AddAdress(这里是@ViewScoped)。

我想要一个循环,或者更确切地说是创建多个人的可能性,但是如果我有一个 SessionScoped-Bean,它的寿命比我需要的时间长,我该如何做到这一点呢?有没有什么命令或注释可以销毁它?您如何处理与范围相关的此类工作流程?

我知道 MyFaces Orchestra 具有对话范围,但如果可能的话,我只会使用 Eclipse/Tomcat (7.0.11)/Mojarra (2.0.3) 来实现。

I want to do something like this: index.xhtml -> createPerson.xhtml -> addAddress.xhtml -> index.xhtml.

In Managed-Bean CreatePerson a new Person-Object will be created, filled and saved, in Managed-Bean AddAddress I need the Person-Object in order to add an address to this person and after this, it should navigate back to the starting point. I think, the Managed-Bean CreatePerson should be @SessionScoped, because I have to inject the Person-Object into AddAdress (here @ViewScoped).

I want to have a loop or rather the possibility to create more than one person, but how can I do this if I have a SessionScoped-Bean, that lives longer than I need it? Is there any command or annotation to destroy it? How do you handle such workflows related to the scopes?

I know about MyFaces Orchestra with conversation-scope, but I will, if possible, do it only with Eclipse/Tomcat (7.0.11)/Mojarra (2.0.3).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梓梦 2024-10-29 06:00:56

使用单个视图并利用 rendered 属性来显示/隐藏包含的视图,其中“子表单”相互依赖。这样您就可以使用 @ViewScoped bean。

或者,只需在创建后将 Person 保存在数据库中,并将其 id 作为请求参数传递给下一个视图,然后再次从数据库加载。

Use a single view and make use of the rendered attribute to show/hide included views with "subforms" depending on each other. This way you can go with a @ViewScoped bean.

Alternatively, just save the Person in the DB after create and pass its id as request parameter to the next view and let it load from the DB again.

自在安然 2024-10-29 06:00:56

如果您只想(或可以)使用 Tomcat 和 Mojarra,那么 BalusC 提到的两种解决方案都可以完美工作,尽管有限制,您必须停留在单个页面上或在页面导航之间重做查询。您的用例正是 Java EE 6(通过 CDI)中的对话范围的用途。您也可以通过 Weld 参考实现将其添加到 Tomcat。

当使用 BalusC 概述的停留在单个页面上的方法时,为了给用户一点点处理单独页面的印象,可以选择使用“对话框”(浮动、CSS、 ...)。如果您可以选择使用第三方组件库,则可以添加 RichFaces 或 PrimeFaces,它们都包含随时可用的对话框组件。

If you only want (or can) use Tomcat and Mojarra then both solutions mentioned by BalusC work perfectly, although with the limitations that you have to stay on a single page or redo queries between page navigation. Your use case is exactly what the conversation scope in Java EE 6 (through CDI) is made for. You can add this too to Tomcat via the Weld reference implementation.

When using the method BalusC outlined for staying on a single page, to give the user a tiny bit the impression of dealing with separate pages, it might be an option to display the rendered parts of the page using 'dialogs' (floating, css, ...). If using a third party component library is an option for you, you could add RichFaces or PrimeFaces that both contain ready to use dialog components.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文