bean中的ManagedProperty返回null

bean中的ManagedProperty返回null

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

问题描述



我一直在追踪指南,似乎并不困难。
但是我的代码根本无法工作!



这是一个小代码片

  @ManagedBean 
@SessionScoped
public class LoginBean {

@EJB
private LoginUserLocal loginUser;
private boolean loggedIn = false;
私人用户用户;
私人StreamedContent图像;



// -
@Named(value =messagesBean)
@RequestScoped
public class MessagesBean {

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

public LoginBean getLoginBean(){
return loginBean;
}

public void setLoginBean(LoginBean loginBean){
this.loginBean = loginBean;
}

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

这段代码给我一个NullPointerException,说loginBean是null!



任何建议?

解决方案

您正在将JSF托管的bean与CDI bean进行混合。您的LoginBean 是一个JSF托管的bean(它具有 @ManagedBean 注释)。您的 MessageBean 是一个CDI bean(它具有 @Named 注释)。如果您将Message bean更改为JSF托管的bean(用 @ManagedBean 替换 @Named ),那么问题应该是解决了(它应该与两个CDI bean一起使用)。



以下是两种bean类型之间注入方式的简要概述:



CDI @Named - > CDI @Named(works)



CDI @Named - > JSF @ManagedBean(仅适用于注入范围bean更广泛)



JSF @ManagedBean - > JSF @ManagedBean(仅在注入的bean的范围更广的情况下工作)



JSF @ManagedBean - > CDI @Named(将不起作用)



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


I'm trying to deal with @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 {

@EJB
private LoginUserLocal loginUser;
private boolean loggedIn = false;
private User user;
private StreamedContent image;
.
.
.
//--
@Named(value = "messagesBean")
@RequestScoped
public class MessagesBean {

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

public LoginBean getLoginBean() {
    return loginBean;
}

public void setLoginBean(LoginBean loginBean) {
    this.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).

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.

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

07-23 12:52