在带有 RequestScope 的 ManagedBean 中使用有状态 EJB 时出现问题

发布于 2024-10-09 14:16:11 字数 2393 浏览 0 评论 0原文

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

代码如下:

@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 的范围更改为 SessionScope,一切都会正常工作,但我不喜欢这样,因为存在内存使用问题。

我的代码有什么问题...请帮助我。

格瑞兹,格里

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-10-16 14:16:11

您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文