简体   繁体   English

如何制作共享/实用程序支持bean?

[英]How to make a shared/utility backing bean?

i have several pages that shares specific part of the page let's say for example a groups of checkboxes that display all users with ability to search in those users, so i am thinking of grouping the UI part in a facelet so i can include it in other pages, and for the server side part i am thinking of make a sessionScoped bean that contains the methods and properties for that page, what do you think about this approach ? 我有几个页面,它们共享页面的特定部分,例如说一组复选框,显示所有具有搜索这些用户能力的用户,因此,我考虑将UI部分分组在一个方面中,以便可以将其包括在其他方面页面,对于服务器端部分,我正在考虑制作一个sessionScoped bean,其中包含该页面的方法和属性,您对此方法有何看法? please advise, thanks. 请指教,谢谢。

UPDATE: there's an important concern, is that should i make the methods in that bean synchronized so that it will return different values for different requests , i mean not to return same results for different requests ? 更新:有一个重要的问题,我是否应该使该bean中的方法同步,以便它将为不同的请求返回不同的值,我的意思是不为不同的请求返回相同的结果?

是的,听起来不错,只要不太沉重,就可以将这些内容放入会话范围

Use request scoped bean instead of session scoped. 使用请求范围的Bean而不是会话范围的。 Use lazy-loading pattern for such things. 对此类事情使用延迟加载模式。 Separating common resources in other bean is a good solution 在其他bean中分离公共资源是一个很好的解决方案

class CommonsBean {
    private List<User> users;

    public List<User> getUsers() {
      if (users == null) {
          users = // here some code to load it from DB
      }
      return users;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM