问题描述
我是CDI的新手,想将它用于JSF2应用程序。类 MyUser
是一个简单的 @Entity
-Bean,并且在中创建了一个对象@ bean中的PostConstruct
方法:
I am new to CDI and want to use this for a JSF2 application. The class MyUser
is a simple @Entity
-Bean and a object is created in a @PostConstruct
method in bean:
@Stateful
@Named @javax.faces.bean.SessionScoped
public class UserBean implements Serializable
{
@Named
private MyUser user;
//setter and getter
//@PostConstruct
}
在JSF页面中访问用户就像一个魅力:#{user.lastName}
。但是现在我想从其他bean访问这个对象,例如在这个 @ViewScopedBean
:
Accessing the user in a JSF pages works like a charm: #{user.lastName}
. But now I want to access this object from other beans, e.g. in this @ViewScopedBean
:
@Named @javax.faces.bean.ViewScoped
public class TestBean implements Serializable
{
@Inject private MyUser user;
}
我想要当前(已登录) MyUser用户
可以在其他几个bean中使用,但我不知道如何做到这一点。只需 @Inject
它不起作用(而且我很确定这对简单来说是一点点)。
I want the current (logged in) MyUser user
to be available in a couple of other beans, but I'm not sure how to do this. Simply @Inject
ing it did not work (and I'm pretty sure this would be a litte bit to simple).
13:56:22,371 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController]
Error installing to Start: name=vfs:///Applications/Development/
jboss-6.0.0.Final/server/default/deploy/test.ear_WeldBootstrapBean state=Create:
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied
dependencies for type [MyUser] with qualifiers [@Default] at injection
point [[field] @Inject private test.controller.mbean.TestBean.user]
从其他bean访问用户
的最佳方法是什么? ? JSF1.2样式代码,如 UserBean bean =(UserBean)FacesContext.getCurrentInstance()。getExternalContext()。getSessionMap()。get(UserBean);
似乎似乎是老式的!
What is the best approach to access the user
from other beans? JSF1.2 style code like UserBean bean = (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("UserBean");
seems seems to be old fashioned!
推荐答案
首先:你不想直接注入实体。实体由ORM框架独立控制,并具有自己的生命周期。不要将它们用作托管bean。
First of all: You don't want to directly inject entities. Entities are pretty independently controlled by the ORM-framework, and have their own life cycle. Don't use them as managed beans.
参见了解详情。
回答你的问题:即使这是一个(经过身份验证的)用户,你也不能弃用在Seam 2中,CDI的整个代理机制不再允许这样做。您需要做的是:
To answer your question: You cannot "outject" something like an (authenticated) user, even though this was possible in Seam 2, the whole proxy mechanism of CDI doesn't allow this anymore. What you need to do is the following:
- 编写一个托管bean来处理身份验证并将其放在正确的范围内(可能是会话范围) )。
- 如果登录成功,请使用此bean的属性来存储经过身份验证的用户。
- 使用生产者方法(可能带有
@LoggedIn
等限定符)使用户可以在您的应用程序中使用
- Write a managed bean which handles the authentication and put it in the correct scope (probably session scope).
- If login succeeds, use an attribute of this bean to store the authenticated user.
- Use a producer method (probably with a qualifier like
@LoggedIn
) to make the user availabe in your application
像这样注入用户:
@Inject
@LoggedIn
private User user
这是CDI方式;-)
That's the CDI-way ;-)
这篇关于将实体注入ViewScoped Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!