在带有RequestScope的ManagedBean中使用有状态EJB的问题 [英] Problem using Stateful EJB in ManagedBean with RequestScope

查看:139
本文介绍了在带有RequestScope的ManagedBean中使用有状态EJB的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Glassfish v3应用服务器中使用JSF 2.0和EJB 3.1.我实际上正面临着以下问题:
在带有RequestScope的MenagedBean中,我想访问一个会话对象(一个具有@Stateful的EJB),该对象应将一些与会话相关的信息存储为选定的类别,选定的页面(每个类别都有一个数据表分页器),等等-没什么特别的我想.
第一次选择类别时,将创建并显示数据表.好的,到目前为止. 现在,如果单击某个项目(行)(以显示该项目的详细信息),或者如果应该显示下一页,则将重新创建会话(有状态EJB),并再次使用默认值来显示和呈现该页面.

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.

代码如下:

@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
    }

和会话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;
}

}

如果我将ManagedBean的Scope更改为SessionScope,则一切正常,但是由于使用内存问题,我不喜欢这样.

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.

格里茨,格里

推荐答案

将为每个请求重新实例化您的RequestScoped ManagedBean(这毕竟是RequestScoped的意思).因此,每次实例化都会注入一个 SFSB实例.

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.

这篇关于在带有RequestScope的ManagedBean中使用有状态EJB的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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