@ManagedBean(name=“foo”) not available by its name in EL #{foo}

做~自己de王妃 提交于 2019-12-02 13:48:00

问题


I'm trying to use an application scoped bean in JSF2, but for some reason it is always null in my request scoped bean. Here's the code I'm using:

The application scoped bean:

@ManagedBean(eager=true, name="applicationTracking")
@ApplicationScoped
public class ApplicationTracking implements Serializable {
    private static final long serialVersionUID = 4536466449079922778L;

    public ApplicationTracking() {
        System.out.println("ApplicationTracking constructed");
    }
}

The request scoped bean:

@ManagedBean
@RequestScoped
public class SearchResults implements Serializable {    
    private static final long serialVersionUID = 4331629908101406406L;

    @ManagedProperty("#{applicationTracking}")
    private ApplicationTracking appTracking;

    public ApplicationTracking getAppTracking() {
        return appTracking;
    }

    public void setAppTracking(ApplicationTracking appTrack) {
        this.appTracking = appTrack;
    }

    @PostConstruct
    public void init() {
        System.out.println("SearchResults.init CALLED, appTracking = " + appTracking);
    }
}

According to everything I'm seeing in the forums this should work without any other configurations. When I start the server (Tomcat) I'm seeing the ApplicationTracking constructor and init methods getting called.

But in my SearchResults component the printout in the PostConstruct is always null:

SearchResults.init CALLED, appTracking = null 

What am I missing?


回答1:


Provided that you imported those annotations from the right package javax.faces.bean.*, then this problem will happen if you re-registered the very same managed bean class in faces-config.xml on a different managed bean name. Get rid of that faces-config.xml entry. That's the JSF 1.x style of registering managed beans. You don't need it in JSF 2.x. When you do so anyway, then it will override any annotation based registration on the managed bean class, causing them to be ineffective.

Make sure you don't read JSF 1.x targeted resources while learning and working with JSF 2.x. Many things are done differently in JSF 2.x.



来源:https://stackoverflow.com/questions/30287437/managedbeanname-foo-not-available-by-its-name-in-el-foo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!