简体   繁体   English

如何限制@ViewScoped bean的数量

[英]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.numberOfViewsInSessioncom.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: 因此,我的问题是:

  1. Is web.xml the right place? web.xml是否正确?
  2. 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.

相关问题 如何从Servlets访问@ViewScoped托管bean? - How to access @ViewScoped managed beans from Servlets? 如何在两个@ViewScoped bean之间共享数据? - How to share data between two @ViewScoped beans? 如何在ViewScoped bean之间传输秘密的用户名和密码? - How to transfer secret usernames and passwords between ViewScoped beans? 无状态视图不支持@ViewScoped bean - @ViewScoped beans are not supported on stateless views 会话销毁后如何使用Viewscoped bean / viewmap生存(使用客户端保存)? - How to survive viewscoped beans/viewmap after session destroy (using client side saving)? 如何在导航窗体中使用@viewScoped Beans在JSF2中使用Seach页面编辑页面来填充编辑页面中的数据? - How to populate data in Edit page when navigated form Seach page to Edit page in JSF2 with @viewScoped Beans? 如何在会话期间检测和删除无法回收的@ViewScoped bean - How detect and remove (during a session) unused @ViewScoped beans that can't be garbage collected jsf2视图参数和viewscoped bean - Jsf2 view parameters and viewscoped beans 每次请求JSF ViewScoped bean调用3次 - JSF ViewScoped beans called 3 times at every request 在SPA JSF 2.2应用程序中删除@ViewScoped bean - Removing @ViewScoped beans in a SPA JSF 2.2 application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM