JSF 2.1查看每次刷新时重新创建的作用域托管bean [英] JSF 2.1 View scoped managed bean re-created on every refresh

查看:120
本文介绍了JSF 2.1查看每次刷新时重新创建的作用域托管bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个问题,我需要任何有关如何解决此问题的信息。
我们在JBoss 7.1上使用JSF 2.1,我们正在使用具有与该视图相关的表的视图范围bean。在该表中表示为行的对象非常大。

So i have a problem and i kindly need any info on how to resolve this. We're using JSF 2.1 on JBoss 7.1 and we're using view scoped beans which have tables related to that view. The object represented as a row in that table is pretty big.

每次刷新这些视图时,都会创建该bean的新实例。

On every refresh of those views, a new instance of that bean is created.

为了验证这种情况,我创建了一个演示示例:

To verify that this is happening, i have created a demo example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
        <h:outputText value="#{viewScopedBean.i}" />
</h:body>
</html> 

然后将此模板链接到这样定义的bean:

this template is then linked to a bean defined like this:

@ManagedBean
@ViewScoped
public class ViewScopedBean {


    private int i = 0;

    @PostConstruct
    public void init(){
        System.out.println("Init - " + i);
    }


    @PreDestroy
    public void dest(){
        System.out.println("Destroy - " + i);
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }
}

每次使用浏览器刷新按钮刷新视图或者只需在浏览器地址字段中输入Enter,我就会清楚地看到 @PostConstruct 方法调用。

Every time i refresh the view using the browser refresh button or by simply pressing enter in the browser address field, i clearly see the @PostConstruct method invocation.

如果我让应用程序保持活动很长一段时间我看不到 @PreDestroy 方法被调用,并且进行堆转储向我显示 ViewScopedBean 与我重新加载视图的数量具有相同数量的实例,即使我销毁会话,它们似乎也会保留在堆上。

If I leave the app alive for a very long time i see no @PreDestroy methods being called, and taking a heap dump shows me that ViewScopedBean has the same number of instances as the number i have reloaded the view, and the appear to remain on the heap even if i destroy the session.

这是对我来说这是一个很大的问题,因为如果有500个用户使用那个大表重新加载视图,那么JBoss会因为它的堆空间已满而死掉。

This is a huge problem for me because if 500 users reloads the view with that large table, JBoss dies because it's heap space is full.

这是<$ c的设计行为$ c> @ViewScoped bean或我做错了什么?

Is this the designed behavior of @ViewScoped beans or am i doing something wrong?

推荐答案

仅查看scoped beans live只要用户通过将回发返回到同一视图来与当前视图进行交互(通过从 UICommand 操作方法返回 null / void ) 。从操作方法返回当前视图ID,将get请求发送到同一视图,刷新页面,在浏览器的地址栏中手动输入URL以及这些事件都会导致重新创建视图。因此,您会看到在每个此类操作上重新实例化视图范围bean。

View scoped beans live only as long as a user interacts with a current view by returning postbacks to the same view (by returning null/void from UICommand action methods). Returning a current view id from an action method, firing a get request to the same view, refreshing the page, manually entering URL in browser's address bar and the events like these all cause the view to be recreated. Thus, you see view scoped beans reinstantiated on every such action.

这篇关于JSF 2.1查看每次刷新时重新创建的作用域托管bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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