@ManagedBean(name =“" foo“")在EL#{foo}中无法使用其名称 [英] @ManagedBean(name="foo") not available by its name in EL #{foo}

查看:105
本文介绍了@ManagedBean(name =“" foo“")在EL#{foo}中无法使用其名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JSF2中使用应用程序范围的bean,但是由于某种原因,它在我的请求范围的bean中始终为null.这是我正在使用的代码:

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:

应用程序作用域bean:

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");
    }
}

请求范围内的bean:

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);
    }
}

根据我在论坛上看到的所有内容,此方法无需任何其他配置即可工作.当我启动服务器(Tomcat)时,我看到ApplicationTracking构造函数和init方法被调用.

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.

但是在我的SearchResults组件中,PostConstruct中的打印输出始终为空:

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

SearchResults.init CALLED, appTracking = null 

我想念什么?

推荐答案

如果您从正确的软件包javax.faces.bean.*中导入了这些注释,那么如果您在在另一个托管bean名称上.摆脱该faces-config.xml条目.这就是注册托管bean的JSF 1.x样式.在JSF 2.x中,您不需要它.无论如何,这样做都会覆盖托管Bean类上所有基于注释的注册,从而使它们无效.

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.

在学习和使用JSF 2.x时,请确保您没有阅读针对JSF 1.x的资源.在JSF 2.x中,许多事情以不同的方式完成.

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.

这篇关于@ManagedBean(name =“" foo“")在EL#{foo}中无法使用其名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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