简体   繁体   English

在JavaServer Faces中注入bean

[英]Injection of beans in JavaServer faces

I have two Session Scoped beans. 我有两个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();
     }
}

So, when I open page which is using PollsBean, all works fine, all bean fields is initialized. 因此,当我打开使用PollsBean的页面时,所有工作正常,所有bean字段都被初始化。 After that I go to second page, which is using ChartBean and call someMethod(), and get NullPointerException. 之后,我转到第二个页面,该页面使用ChartBean并调用someMethod(),并获取NullPointerException。 Why I can't use initialized SessionScoped Bean? 为什么我不能使用初始化的SessionScoped Bean?

Thanks. 谢谢。

Make sure you have a beans.xml file inside your META-INF or WEB-INF folder. 确保您的META-INF或WEB-INF文件夹中有一个beans.xml文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM