1

I used for internationalization the JSF configuration, and I have a list of countries from the class Locale in <p:selectOneMenu>, but I am facing a problem of duplicate values of the countries list in <p:selectOneMenu> when I change the language of the page

How can I correct this problem?

private List<String> countriesList = new ArrayList<String>();

    public void setCountriesList(List<String> countriesList) {
        this.countriesList = countriesList;
    }

    public List<String> getCountriesList() {

        String[] locales = Locale.getISOCountries();

        for (String countryCode : locales) {

            Locale obj = new Locale("", countryCode);
            countriesList.add(obj.getDisplayCountry(Locale.ENGLISH));

        }
        Collections.sort(countriesList);
        return countriesList;
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Spartan
  • 1,167
  • 4
  • 20
  • 40

1 Answers1

0

I can't really tell without seeing more code, but from what I see my guess is the following:

Probably you call getCountriesList every time the language is changed and since it is adding entries to the list countriesList, which is not local to the method but a class member and therefore more permanent, those entries accumulate in that list.

Try doing

ArrayList<List> countriesList = new ArrayList<String>();

at the beginning of your method getCountriesList to make sure you are working on a local variable. That's the behavior I would expect from a method whose name follows the schema "getSomething".

mastov
  • 2,942
  • 1
  • 16
  • 33