我如何使用@ConversationScoped

发布于 2024-11-29 17:07:50 字数 1099 浏览 1 评论 0原文

我正在编写一个 JSF 2.0 应用程序,并且我想使用 CDI 注释而不是“等效的”JSF 注释。换句话说,@Model 或@Named 代替@ManagedBean,@Inject 代替@ManagedProperty。

我唯一无法开始工作的是 @ViewScoped,它对于 AJAX 组件来说是必需的。唯一可靠的解决方法是使用 @SessionScoped,这不是一个好的做法。

正确的做法是什么?尽管我搜索得越多,我就越感到困惑。

这是在 GlassFish 3.1.1 上,据我所知其中有 Weld 1.1.0。

更新:这个问题的原始形式说我无法让@ConversationScoped 工作。从那时起,我发现了我的错误,并且确实让它像这样工作:

@Model
@ConversationScoped
public class Abean implements Serializable {

@Inject Conversation conversation;

// stuff omitted for brevity

public String getSomething() {
    if (conversation.isTransient()) conversation.begin();
    return "something";
}

这似乎可以解决问题。然而现在我的问题改变了。你到底应该在哪里调用conversation.end()?我是否必须编写一个过滤器来检测用户何时离开页面?或者如果不管它,Abean 实例什么时候会被取消引用?

第二次更新:对 CDI 的 @ConversationScoped 进行了很好的讨论,我发现 这里。

我仍然面临着如何调用conversation.end()的问题。我的 bean 为通过 AJAX 更新的数据表浏览器提供有状态支持,并且调用 end() 的最佳位置是当用户离开页面时。然而,如果没有编写一个过滤器来监视页面,我真的看不到任何方法可以做到这一点。欢迎任何“最佳实践”建议。

I am writing a JSF 2.0 application, and I want to use CDI annotations instead of the "equivalent" JSF annotations. In other words, @Model or @Named instead of @ManagedBean, and @Inject instead of @ManagedProperty.

The only thing I cannot get to work is @ViewScoped which is necessary for AJAX components. The only reliable work-around is to use @SessionScoped, which is not a good practice.

What is the correct practice? As much as I search I just get more confused.

This is on GlassFish 3.1.1, which I understand has Weld 1.1.0 in it.

UPDATE: The original form of this question said that I could not get @ConversationScoped to work. Since then I found my error and I did get it to work like so:

@Model
@ConversationScoped
public class Abean implements Serializable {

@Inject Conversation conversation;

// stuff omitted for brevity

public String getSomething() {
    if (conversation.isTransient()) conversation.begin();
    return "something";
}

This seems to do the trick. However now my question is changed. Where exactly are you supposed to call conversation.end()? Do I have to write a filter to detect when the user leaves the page? Or if it is left alone, just when would the Abean instance be de-referenced?

SECOND UPDATE: A very good discussion of CDI's @ConversationScoped I found here.

I am still left with the problem of how to call conversation.end(). My bean provides stateful backing to a data table browser updated via AJAX, and the optimal place to call end() is when the user navigates away from the page. However short of writing a filter to monitor the pages I don't really see any way of doing that. Any suggestion of "best practice" is welcome.

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

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

发布评论

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

评论(2

丶情人眼里出诗心の 2024-12-06 17:07:50

使用 MyFaces CODI 的 (CDI) 范围就更简单了。他们有更好的@ConversationScoped,您会喜欢@ViewAccessScoped 来实现您正在尝试的功能。

That's way simpler with the (CDI) scopes of MyFaces CODI. They have a better @ConversationScoped and you will love the @ViewAccessScoped for what you are trying.

南风几经秋 2024-12-06 17:07:50

更新:JSF 2.2(jsr 344,处于早期草案审查中)为此添加了 @FlowScoped CDI 范围。 更多信息...

update: JSF 2.2 (jsr 344, in early draft review) is adding an @FlowScoped CDI scope for this. More info...

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