ManagedBean 访问另一个 ManagedBean

发布于 2025-01-05 02:16:27 字数 1281 浏览 0 评论 0原文

我可能非常接近解决方案,但我是 JSF 新手,我没有看到我的错误。 我有第一个 SessionScoped Managed Bean 代表业务信息(地址、网站等)

@Named(value = "businessController")
@SessionScoped
public class BusinessController implements Serializable {
    private Business current;
    @EJB private BusinessFacade ejbFacade;
    ....

我有第二个 SessionScoped Managed Bean 代表登录用户

@Named(value = "loginController")
@SessionScoped
public class LoginController implements Serializable {

    private Login current;
    @EJB
    private LoginFacade ejbFacade;
    @ManagedProperty(value="#{businessController}")
    private BusinessController businessController;

    public BusinessController getBusinessController() {
        return businessController;
    }

    public void setBusinessController(BusinessController businessController) {
        this.businessController = businessController;
    }

当用户登录时,我从 loginController 设置当前属性 根据当前用户,我想从businessController 设置业务属性:

businessController.setCurrent(current.getBusiness());

我的问题是businessController 属性为null!

我使用 NetBeans 7.0.1 和 GlassFish 3.1 在调试模式下,我可以看到一个带有值的 viewId 变量

>No current context (stack frame)<

不幸的是它没有给我带来任何提示。

任何帮助将不胜感激 谢谢

I'm probably really close to the solution but I'm new with JSF and I don't see my mistake.
I have a first SessionScoped Managed Bean that represents Business information (address, website, ...)

@Named(value = "businessController")
@SessionScoped
public class BusinessController implements Serializable {
    private Business current;
    @EJB private BusinessFacade ejbFacade;
    ....

I have a second SessionScoped Managed Bean that represents the logged in user

@Named(value = "loginController")
@SessionScoped
public class LoginController implements Serializable {

    private Login current;
    @EJB
    private LoginFacade ejbFacade;
    @ManagedProperty(value="#{businessController}")
    private BusinessController businessController;

    public BusinessController getBusinessController() {
        return businessController;
    }

    public void setBusinessController(BusinessController businessController) {
        this.businessController = businessController;
    }

When a user logs in, I set the current attribute from the loginController
Depending on this current user, I want to set the business attribute from the businessController :

businessController.setCurrent(current.getBusiness());

My problem is that the businessController attribute is null !

I use NetBeans 7.0.1 and GlassFish 3.1
In debug mode, I can see a viewId variable with the value

>No current context (stack frame)<

Unfortunately it doesn't ring any bell to me.

Any help would be appreciated
Thanks

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

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

发布评论

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

评论(1

尝蛊 2025-01-12 02:16:27

您将 JSF 托管 Bean 与 CDI 托管 Bean 混合在一起。

您的 BusinessController 使用 CDI 注释 @Named 进行注释,但注入了 @ManagedProperty 注释(来自 JSF)。 CDI 托管 Bean 需要使用 @Inject 进行注入。在这种情况下不需要 getter 或 setter。如果您倾向于使用 CDI,请确保导入正确的 @SessionScoped

CDI:javax.enterprise.context.SessionScoped

JSF:javax.faces.bean.SessionScoped

尝试以下操作(在确保导入了正确的作用域类之后):

 @Inject private BusinessController businessController;

You are mixing JSF managed beans with CDI managed beans.

Your BusinessController is annotated with the CDI annotaion @Named but gets injected with the @ManagedProperty annotation (from JSF). CDI managed beans need to be injected with @Inject. No getter or setter needed in this case. If you tend to use CDI, make sure that you import the correct @SessionScoped:

CDI: javax.enterprise.context.SessionScoped

JSF: javax.faces.bean.SessionScoped

Try the following (After making sure to have the correct scope class imported):

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