0

I am using JSF-2.2, Primefaces-3.5 and JDK-1.7.

I have a Registration Page where i have a Parent-Child Menu Relations on Countries and Cities, that needs to be loaded dynamically,based on the selected parent value.

Currently i am displaying the list of Countries and States! Which works like a charm! :) How to display list of countries and cities in JSF

My Problem is if we have n-users accessing the page we are creating n-Objects which is load on the server. I want to reduce the load and memory wastage

Is there a way to Load Data and store in Context of Application and access them in JSF.

Useful Reference:

How to Populate Child Menus in JSF

Community
  • 1
  • 1
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68
  • Is it correct that you want to load a List and List once and for all, for all users? – Jaqen H'ghar Apr 19 '15 at 09:20
  • @JaqenH'ghar Yes! I want to `Load the List of Countries and Cities`, one and only once, like loading `DB Configurations` at application level. So, that i can access it from any bean in my App. – 09Q71AO534 Apr 19 '15 at 09:27
  • Those names of countries, states and cities are static and are changed quite less frequently. You could store them in application scoped beans. – Tiny Apr 19 '15 at 20:11

1 Answers1

1

You can just make the bean from Balusc's answer @ApplicationScoped, and only use this bean for reading the codelists. You can then inject the bean in your other beans, or just call it directly from the xhtml.

Cities you can also load in init() and keep in a Map<String, List<String>>, where key is country and value is cities. Or you can lazy initialize the cities:

Map<String, List<String>> map = new HashMap<>();

public synchronized List<String> getCities(String country) {
    if (!map.containsKey(country)) {
        List<String> cities = someService.getCities(country);
        map.put(country, cities);
        return cities;
    }
    return map.get(country);    
}

However, if it at all is possible that the entries can change over time (other system updating/deleting in DB for example) @SessionScoped might be better, since you won't have to remember to reload the application/restart the server as with @ApplicationScoped.

Community
  • 1
  • 1
Jaqen H'ghar
  • 4,305
  • 2
  • 14
  • 26