简体   繁体   中英

Unable to inject a CDI bean in Primefaces LazyDataModel implementation class

Any CDI bean I inject in the implementation class of org.primefaces.model.LazyDataModel , turns out null. The lazy model implementation class itself is a CDI @Named bean with scope @javax.faces.bean.ViewScoped . It looks like this:

    @Named("myLazyModel")
    @ViewScoped
    Class MyLazyModel extends LazyDataModel implements Serializable {

    public MyLazyModel() {

    }

    @Inject
    AnotherCdiBeanClass injectedObj;

    public AnotherCdiBeanClass getInjectedObj() {
        return injectedObj;
    }

    public AnotherCdiBeanClass setInjectedObj(AnotherCdiBeanClass anotherCdiBeanClassObj) {
        this.injectedObj = anotherCdiBeanClassObj;  
    }


    public void someMethod() {
       getInjectedObj(); // Its NULL, why?
    }


}

I did get a warning in eclipse from Java while injecting in lazymodel implementation bean class. Warning was something like that the lazymodel class bean can not proxied by the container as it does not contain non-private no-arg constructor and the bean is not a normal scoped bean (something like that).

There is another problem, in the LazyDataModel implementation class, I have set the rowCount to the size of the actual data list's dynamic size (size after filtering in grid). But then after instantiating the lazymodel implementation class outside in some CDI bean gives me a rowCount of zero.

new MyLazyModel(actualDataList).getRowCount() -> 0

Upon debugging I found it does contain the complete actual data list but rowCount is still zero.

I needed on the go rowCount of the filtered rows as I type and filter them on a primefaces grid so that I can dynamically update an outputLabel with the currently filtered grid's rowCount value.

int currentRowCount = actualDataListInLazyModelClass.size();

I will be updating the outputLabel like this:

org.primefaces.context.RequestContext.getCurrentInstance.update("myForm:recordCountLabel");

But before that I need to set property of an injected bean (View's backing bean) with the rowCount value but the injected bean itself turns out null as said before.

getInjectedObj().setNoOfRecords(Integer.toString(currentRowCount));

// NULL POINTER because getInjectedObj() is null

Sorry if I have described things more or less due to limitations at my end.

You have the wrong ViewScoped. For CDI on JSF 2.2 it should be javax.faces.view.ViewScoped and if you are on JSF 2.0/2.1 but are using CDI (you 'should'), you can use the OmniFaces Viewscoped

See also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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