在JavaServer面中注入bean

时间:2014-03-12 13:04:34

标签: jsf javabeans cdi code-injection

我有两个Session Scoped bean。

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;

@Named
@SessionScoped
public class PollsBean implements Serializable {
    private long id;
    private String title;
    ......
    get...
    set...
}



import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

@Named
@SessionScoped
public class ChartBean implements Serializable {

    @Inject private PollsBean currentPoll;
     ... 
     public void someMethod(){
        Long curid = currentPoll.getId();
     }
}

因此,当我打开使用PollsBean的页面时,一切正常,所有bean字段都被初始化。之后我转到第二页,它使用ChartBean并调用someMethod(),并获得NullPointerException。为什么我不能使用初始化的SessionScoped Bean?

感谢。

1 个答案:

答案 0 :(得分:1)

确保META-INF或WEB-INF文件夹中有一个beans.xml文件。

http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html

相关问题