@Named的行为与@ManagedBean不同 [英] @Named behaving different from @ManagedBean
问题描述
我目前正在将一个项目从JBoss 4.2.2迁移到JBoss 6.0.0,我还在使用CDI添加依赖注入,并从JSF 1.2迁移到JSF 2.0。我将一个beans.xml文件添加到ejb-package以及war-package中。
I'm currently migrating a project from JBoss 4.2.2 to JBoss 6.0.0 and I'm also adding Dependency Injection with CDI and migrate from JSF 1.2 to JSF 2.0. I added a beans.xml file to both the ejb-package, as well as the war-package.
现在我有一个xhtml页面,它使用托管bean LoginBean.java
。这些bean已在faces-config.xml中配置如下:
Now I have a xhtml page that uses the managed bean LoginBean.java
. The beans had been configured in the faces-config.xml like this:
<managed-bean>
<description>Sample description</description>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>com.sample.managedbeans.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
所以,首先我删除了上面的配置并添加了 @ManagedBean @SessionScoped
到班级本身。因为我想将CDI添加到项目中,所以我将 @ManagedBean
更改为 @Named
(与问题2930889 )。
So, at first I removed above configuration and added @ManagedBean @SessionScoped
to the class itself. Because I wanted to add CDI to the project, I changed @ManagedBean
to @Named
(in relation to question 2930889).
现在,当我提交相应xhtml的表单时,字段 username 和 password (在JSP中用作#{loginBean.username}
)为空。当我改回 @ManagedBean
时,它运行正常。
Now when I submit the form of the corresponding xhtml, the fields username and password (used in the JSP as #{loginBean.username}
) are null. When I change back to @ManagedBean
, it works fine.
我在这里遗漏了什么吗?
Am I missing something here?
亲切的问候,
Sebastian
Kind regards, Sebastian
推荐答案
现在ee6中实际上有两个 @SessionScoped
注释, @ javax.faces.bean.SessionScoped
来自jsf 2规范,只能与 @ManagedBean
一起使用,然后有来自cdi的@ javax.enterprise.context.SessionScoped
。我的猜测是你使用的是jsf注释,cdi会忽略它,所以cdi会创建你的bean的新实例。
There are actually two @SessionScoped
annotations now in ee6, @javax.faces.bean.SessionScoped
which comes from the jsf 2 spec and only works together with @ManagedBean
and then there is @javax.enterprise.context.SessionScoped
from cdi. My guess is that you are using the jsf annotation, which is ignored by cdi, and so cdi creates a new instance of your bean.
这篇关于@Named的行为与@ManagedBean不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!