JSF 2 应用程序中的默认托管 Bean 范围是什么? [英] What is the default Managed Bean Scope in a JSF 2 application?

查看:24
本文介绍了JSF 2 应用程序中的默认托管 Bean 范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常默认范围是请求范围.我试图删除范围注释并期望 bean 具有请求 bean 范围的行为(通过在前一页返回提交的值,我记得我过去尝试过这个并且它工作正常)但我什么都没有表达语言连接到它.那么默认范围是什么,为什么它的行为不同?!

Normally the default scope is the Request scope. I ve tried to delete the scope annotation and expected the bean to have a request bean scopped behaviour (by returning a submitted value on a previous page, I remember i've tried this the past and it worked fine) but i got nothing on the expression language wired to it. So what is the default scope and why it's not the same behaviour?!

推荐答案

取决于谁在管理 bean.

Depends on who's managing the bean.

如果是 JSF,通过 @ManagedBean,则默认为 @RequestScoped,如javadoc:

如果省略了作用域注解,则必须像存在RequestScoped注解一样处理bean

If the scope annotations are omitted, the bean must be handled as if the RequestScoped annotation is present

如果是CDI,通过@Named,则默认为 @Dependent,如 焊接文档:

最后,CDI 具有所谓的依赖伪作用域.这是未显式声​​明作用域类型的 bean 的默认作用域.

Finally, CDI features the so-called dependent pseudo-scope. This is the default scope for a bean which does not explicitly declare a scope type.

效果是在每个 EL 表达式上新创建了 bean 实例.因此,想象一个登录表单,其中有两个输入字段引用一个 bean 属性,一个命令按钮引用一个 bean 操作,因此总共有三个 EL 表达式,那么实际上将创建三个实例.一个设置了用户名,一个设置了密码,另一个设置了操作.实际上,这与 JSF @无作用域.这证实了您看到的症状.

The effect is that the bean instance is newly created on every single EL expression. So, imagine a login form with two input fields referring a bean property and a command button referring a bean action, thus with in total three EL expressions, then effectively three instances will be created. One with the username set, one with the password set and one on which the action is invoked. In effects, this behaves the same as JSF @NoneScoped. This confirms the symptoms you're seeing.

如果是 Spring,通过 @Component,则默认为 @Scope("singleton"),如javadoc:

默认值:单例"

实际上,这与 JSF <代码>@ApplicationScoped 和 CDI @ApplicationScoped.

In effects, this behaves the same as JSF @ApplicationScoped and CDI @ApplicationScoped.

Netbeans 与它无关.它只是一个类似于记事本的编辑工具,但具有 1000 多个额外功能.

Netbeans has got nothing to do with it. It's just an editing tool like notepad but then with 1000+ extra features.

这篇关于JSF 2 应用程序中的默认托管 Bean 范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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