简体   繁体   中英

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 ? 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 ?

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

Use request scoped bean instead of session scoped. Use lazy-loading pattern for such things. Separating common resources in other bean is a good solution

class CommonsBean {
    private List<User> users;

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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