开发者

Problem using Stateful EJB in ManagedBean with RequestScope

I'm using JSF 2.0 and EJB 3.1 in the Glassfish v3 app server. And I'm facing actually the follwing problem:

In a MenagedBean with RequestScope I want to access a session object (an EJB with @Stateful) which should store some session relevant information as the seleced category, the seleced page (with a datatable paginator for each category), etc. - nothing special I think.

The first time a category is selected, the datatable is created and displayed. Allright so far. Now, if an item (row) is clicked (to show the item's details) or if the next page should be displayed, the session (the stateful EJB) is recreated and again the default values are used to display and render the page.

The code looks like:

@ManagedBean
@RequestScoped
public class TableViewBean {

    @EJB
    SessionBean session;

    public DataModel getTable() {
            return session.getDataModel();
         }

        public SessionBean getSession(){
            return session;
        }
         public void next() {
             session.getPaginator().nextPage();
             session.resetList();
         }

         public void previous() {
                session.getPaginator().previousPage();
                session.resetList();
         }
         // some other code
    }

and the session EJB:

@Stateful
public class SessionBean {

private String selectedType = "Entity";

private DataModel dataModel;
private int rowsPerPage = 5;
private Paginator paginator;


public void setSelectedType(String type){
    if(!type.equalsIgnoreCase(selectedType)){
        selectedType = type;

        updateService();
    }
    resetList();
}


public void resetList() {
    dataModel = null;
}

public void resetPagination() {
    paginator = null;
}

public int getRowsPerPage() {
    return rowsPerPage;
}

public void setRowsPerPage(int rowsPerPage) {
    this.rowsPerPage = rowsPerPage;
    resetList();
    resetPagination();
}

public Paginator getPaginator() {
    if (paginator == null) {
        paginator = new Paginator(rowsPerPage) {

            @Override
            public int getItemsCount() {
                return selectedService.getCount();
            }

       开发者_开发知识库     @Override
            public DataModel createPageDataModel() {
                DataModel model = null;
                if(selectedService != null){
                    model = new ListDataModel(....);
                }
                return model;
            }
        };
    }

    return paginator;

}

public DataModel getDataModel() {
    if(dataModel == null){
        dataModel = getPaginator().createPageDataModel();
    }

    return dataModel;
}

}

If I change the Scope of the ManagedBean to SessionScope everything works fine, but I don't like this, because of use of memory concerns.

What's wrong with my code...please help me.

Greetz, Gerry


Your RequestScoped ManagedBean is re-instantiated for each request (that's what RequestScoped means after all). Therefore, with each instantiation it gets injected with a new SFSB instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜