My boss wanted to add a select form in feedback form where the user is required to select his/her country. I can achieve this by:
Select country = form.addItem().addSelect("country ");
userType.setLabel("Country");
userType.addOption("","Please select your country");
userType.addOption("ABW","Aruba");
userType.addOption("AFG","Afghanistan");
<----- Rest of countries from A to Z --->
userType.setOptionSelected(parameters.getParameter("country",""));
TextArea countryOther = form.addItem().addTextArea("countryOther");
countryOther.setValue(parameters.getParameter("countryOther", ""));
I want to avoid cluttering the code with a really long list of countries. Is there an alternative to this like placing the list of countries in a separate place and just pull this information? Thanks in advance.
One simple way is to create a config file: config/modules/countries.cfg
and fill in your list like configuration properties:
ABW=Aruba
AFG=Afghanistan
...
Then add them to your Select using the ConfigurationManager
Select country = form.addItem().addSelect("country ");
country.setLabel("Country");
country.addOption("", "Please select your country");
Enumeration<?> countries = ConfigurationManager.propertyNames("countries");
while (countries.hasMoreElements()) {
String key = (String) countries.nextElement();
String value = ConfigurationManager.getProperty("countries", key);
country.addOption(key, value);
}