[英]How to limit the number of @ViewScoped beans
I'm using GF4. 我正在使用GF4。 In my app I have a main menu which is always visible to the user. 在我的应用中,我有一个主菜单,始终对用户可见。 One of the menu items refers to a @ViewScoped
backing bean, which is being created and its @PostConstruct
method called every time the user clicks on this menu item, which is the expected behavior. 菜单项之一指的是@ViewScoped
后备bean,正在创建该bean,并在用户每次单击此菜单项时调用其@PostConstruct
方法,这是预期的行为。
The problem arises when the user clicks on some other menu item, because the @ViewSoped bean continues to exist, ie its @PreDestroy
method is not being called. 当用户单击某些其他菜单项时,就会出现问题 ,因为@ViewSoped Bean继续存在,即未调用其@PreDestroy
方法。
Looking for an answer I came across this stackoverflow post which proposes setting those two parameters: com.sun.faces.numberOfViewsInSession
and com.sun.faces.numberOfLogicalViews
. 在寻找答案时,我遇到了这个stackoverflow帖子 ,该帖子建议设置这两个参数: com.sun.faces.numberOfViewsInSession
和com.sun.faces.numberOfLogicalViews
。 Since I want only one @ViewScoped bean at a time I set them both in web.xml to 1
. 由于一次只需要一个@ViewScoped bean,因此在web.xml中将它们都设置为1
。 But this has no effect! 但这没有效果! Hence my questions: 因此,我的问题是:
Is there a way to affect the container managed beans, ie manually destroy them? 有没有办法影响容器管理的bean,即手动销毁它们?
Thanks in advance! 提前致谢!
I found the answer here JSF 2.2 Memory Consumption: Why does Mojarra keep the ViewScoped Beans of the last 25 Views in Memory? 我在这里找到了答案JSF 2.2内存消耗:为什么Mojarra为什么将最近25个视图的ViewScoped Bean保留在内存中? . 。 The user there provides the solution in his question. 那里的用户提供他的问题的解决方案。 So one have to implement a custom HttpSessionListener
as described there. 因此,必须实现一个自定义的HttpSessionListener
,如此处所述。
To make the solution complete I'm adding the configuration part. 为了使解决方案完整,我将添加配置部分。 Supposed your implementation looks like this: 假设您的实现如下所示:
public class CustomViewMapConfig implements HttpSessionListener
{
@Override
public void sessionCreated(HttpSessionEvent event)
{
event.getSession().setAttribute(ViewScopeManager.ACTIVE_VIEW_MAPS_SIZE, 1);
}
}
then the corresponding web.xml looks like this: 然后相应的web.xml如下所示:
<web-app>
<listener>
<listener-class>my.package.CustomViewMapConfig</listener-class>
</listener>
...
</web-app>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.