CDI @Named bean中的@ javax.faces.bean.ManagedProperty返回null [英] @javax.faces.bean.ManagedProperty in CDI @Named bean returns null

查看:132
本文介绍了CDI @Named bean中的@ javax.faces.bean.ManagedProperty返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理 @ javax.faces.bean.ManagedProperty 但没有成功!


我一直在遵循指南,但似乎没有好难
但是我的代码根本行不通!


这里有一个小片段

  @ManagedBean 
@SessionScoped
公共类LoginBean {

私人用户;

// ...
}



  @Named 
@RequestScoped
公共类MessagesBean {

@ManagedProperty(value =#{loginBean})
私人LoginBean loginBean;

public String getUser(){
System.err.println(loginBean == null);
return loginBean.getUser()。getUsername();
}

// ...
}

此代码给我一个 NullPointerException ,说 loginBean 为空!


任何建议?

解决方案

您正在将JSF托管bean与CDI bean混合在一起。您的 LoginBean 是JSF托管的bean(具有 @ManagedBean 批注)。您的 MessageBean 是CDI bean(具有 @Named 批注)。如果将Message Bean更改为JSF托管Bean(将 @Named 替换为 @ManagedBean ),则问题应该是解决(它也应与两个CDI bean一起使用)。或者,如果您使用的是JSF 2.3或更高版本,请使用 javax.faces.annotation.ManagedProperty 而不是CDI bean。


这里是注入方式的简短概述可以在两种bean类型之间工作:


CDI @Named-> CDI @Named(有效)


CDI @Named-> JSF @ManagedBean(仅当注入的bean的范围更广时才起作用)


JSF @ManagedBean-> JSF @ManagedBean(仅当注入的bean的范围更广时才起作用)


JSF @ManagedBean-> CDI @Named(无效)


但是要注意范围导入类。根据豆类型, @SessionScoped @RequestScoped 有不同的类。


javax.faces.bean.SessionScoped for @ManagedBeans



< blockquote>

javax.enterprise.context.SessionScoped for CDI @Named beans


此外,对于 @Named (CDI),请使用 @Inject ,对于 @ ManagedBean 使用 @ManagedProperty 。有一件事情在CDI中不起作用。您的 @ManagedProperty(value =#{loginBean})获取完整的bean,但 @ManagedProperty(value =#{loginBean .user})来获取bean的属性。这在使用 @Inject 的CDI中是不可能直接实现的。请参见用于@ManagedProperty的CDI替换以获取解决方案


I'm trying to deal with @javax.faces.bean.ManagedProperty but without success !

I've been following this guide, and it not seems that hard. But my code simply won't work!

Here's a little snippet

@ManagedBean
@SessionScoped
public class LoginBean {

    private User user;

    // ...
}

@Named
@RequestScoped
public class MessagesBean {

    @ManagedProperty(value = "#{loginBean}")
    private LoginBean loginBean;

    public String getUser() {
        System.err.println(loginBean == null);
        return loginBean.getUser().getUsername();
    }

    // ...
}

This code gives me a NullPointerException, saying that loginBean is null!

Any suggestion?

解决方案

You are mixing JSF managed beans with CDI beans. Your LoginBean is a JSF managed bean (it has the @ManagedBean annotation). Your MessageBean is a CDI bean (it has the @Named annotation). If you changed the Message bean to a JSF managed bean (replacing @Named with @ManagedBean) then the problem should be solved (It should work with two CDI beans as well). Or if you're using JSF 2.3 or newer, then use javax.faces.annotation.ManagedProperty instead in the CDI bean.

Here is a short overview of how injection works between both bean types:

CDI @Named --> CDI @Named (works)

CDI @Named --> JSF @ManagedBean (works only if scope of injected bean is broader)

JSF @ManagedBean --> JSF @ManagedBean (works only if scope of injected bean is broader)

JSF @ManagedBean --> CDI @Named (won't work)

But take care of the scope import classes. There are different classes for @SessionScoped and @RequestScoped depending on the bean type.

javax.faces.bean.SessionScoped for @ManagedBeans

javax.enterprise.context.SessionScoped for CDI @Named beans

In addition, for @Named (CDI) use @Inject and for @ManagedBean use @ManagedProperty. There is one thing that does not work in CDI. Your @ManagedProperty(value = "#{loginBean}") gets a full bean, but @ManagedProperty(value = "#{loginBean.user}") to get a 'property' of a bean works to. This is not directly possible in CDI with @Inject. See CDI Replacement for @ManagedProperty for a 'solution'

这篇关于CDI @Named bean中的@ javax.faces.bean.ManagedProperty返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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