简体   繁体   English

JSF @ManagedProperty没有被注入

[英]JSF @ManagedProperty not being injected

I am writing a web app and I am using JSF 2.0 and hibernate 3.5. 我正在编写一个Web应用程序,并且正在使用JSF 2.0和Hibernate 3.5。 My problem is when I would like to inject a bean it's not being injected. 我的问题是,当我想注入未注入的bean时。

This is the class I would like to inject(I copied only the important parts): 这是我要注入的类(我只复制了重要部分):

@ManagedBean(name="permissionBean", eager = true)
@SessionScoped
public class PermissionBean {
    private List<Role> rolesList;
    public PermissionBean(){
        refresh();
    }

    private void refresh(){
        rolesList = Role.queryRolesList();
    }
    public void test(){
        System.out.println("__________TEST ");
    }
//getter setter for the rolesList
}

Into this: 变成这个:

@ManagedBean(name="triggerBean")
@SessionScoped
public class Trigger extends EmptyInterceptor {

    @ManagedProperty(value="#{permissionBean}")
    public PermissionBean pb;
    public void onDelete(..) {
        pb.test();
    }
    public void setPb(PermissionBean pb) {
        System.out.println("______setting bean");
        this.pb = pb;
    }
}

The second class is an interceptor class for hibernate. 第二类是休眠的拦截器类。 The program starts and works, I am sure the PermissionBean is being constructed because I am using the methods in it, I am seeing the list. 该程序启动并运行,我确定PermissionBean正在构造中,因为我正在使用其中的方法,并且看到了列表。 But when I try to delete from it, and the onDelete() function triggers I got a null pointer exception(on that line where I call pb.test() ). 但是,当我尝试从中删除它时, onDelete()函数触发了一个空指针异常(在那一行我调用pb.test() )。 After some trying I put a test write out into the setter, but that method never gets called. 经过一番尝试后,我将测试写出到设置器中,但是该方法从未被调用。

I guess the eager=true is the problem. 我想问题在于eager=true 'Eager true' only makes sense in an application scoped bean, i don't know what effect it has in a session scoped bean as it was not designed for that. “渴望真实”仅在应用程序范围的Bean中有意义,我不知道它在会话范围的Bean中有什么作用,因为它不是为此而设计的。 But from your describtion, it seems that it has the effect that one object is being created when you start the application but this object is not in the same session context as that object of the Trigger bean from which you are trying to access it. 但是从您的描述来看,似乎具有以下效果:启动应用程序时正在创建一个对象,但是该对象与您尝试从中访问它的Trigger bean的对象不在同一会话上下文中。 As long as the PermissionBean is not referenced from any xhtml page the managedProperty inside the Trigger Class will remain null . 只要未从任何xhtml页中引用PermissionBeanTrigger类内的managedProperty将保持为null

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

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