带有CDI bean和@ConversationScoped的AJAX页面 [英] AJAX pages with CDI beans and @ConversationScoped

查看:141
本文介绍了带有CDI bean和@ConversationScoped的AJAX页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很感兴趣,当我使用CDI时,将JSF页面与AJAX一起使用的正确方法是什么? 我测试了使用@SessionScoped配置CDI Bean,但是我发现AJAX存在问题.

I'm interested What is the proper way to use JSF pages with AJAX when I use CDI. I tested to configure the CDI beans with @SessionScoped but I found that there is a problem with AJAX.

将AJAX与配置了@ConversationScoped的CDI bean一起使用是否合适?

Is it proper to use AJAX with CDI beans configured with @ConversationScoped?

我发现必须将conversation.begin();放入Bean构造函数中,并将conversation.end();放入Java方法中,这必须在会话完成时进行.我可以以某种方式自动执行此操作吗?

And I found that I have to put conversation.begin(); into the Bean constructor and conversation.end(); into Java method which must be when the session is completed. Can I somehow do this automatically?

P.S当用户关闭页面时,我可以使用此代码自动释放资源吗?

P.S Can I use this code to automatically free the resource when the user closes the page?

@Remove
public void finishIt(){
     conversation.end();
}

推荐答案

我发现我必须将conversation.begin();放入Bean构造函数中,并将conversation.end();放入Java方法中,这必须在会话完成时进行.

And I found that I have to put conversation.begin(); into the Bean constructor and conversation.end(); into Java method which must be when the session is completed.

是正确的.另请参见如何用CDI替换@ManagedBean/@ViewScope JSF 2.0/2.1 中的代码示例.

That's correct. See also among others How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1 for a concrete code example.

我能以某种方式自动执行此操作吗?

不.考虑安装 MyFaces CODI .它将把JSF @ViewScoped桥接到CDI.这样,您可以只使用@Named @ViewScoped.但是,缺点是它会使用windowId请求参数来污染URL.

No. Consider installing MyFaces CODI. It will bridge the JSF @ViewScoped to CDI. This way you can just use @Named @ViewScoped. A disadvantage is however that it pollutes the URL with windowId request parameter.

MyFaces CODI还提供了@ViewAccessScoped,只要您在视图中引用所需的bean,无论您坐在哪个视图,它都将一直存在.一旦导航到一个在任何地方都没有引用bean的视图, ,它将被丢弃.

MyFaces CODI also offers the @ViewAccessScoped which will live as long as you reference the desired bean in a view, regardless of the view you're sitting in. Once you navigate to a view which doesn't reference the bean anywhere, it will be trashed.

或者,如果有耐心,请等待JSF 2.2发布.它将附带与CDI兼容的@ViewScoped ,因此您不需要MyFaces CODI

Or, if you have patience, wait for JSF 2.2 to be released. It will come with a CDI compatible @ViewScoped so that you don't need MyFaces CODI.

这篇关于带有CDI bean和@ConversationScoped的AJAX页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆