Following is an example of how to get the array of string or list of properties from a Resource Bundle. We can specify the list of values in one property (single key) and obtain it as an string array or an array list in Java.
Here is an example of property file which has a list of key/value combination for employee's designations and its corresponding value.
EmployeeConfig.properties
====================
employeeDesignations=\
0=Director,\
1=Manager,\
2=Clerk,\
3=Business Analyst,\
4=Sales Executive,\
#Note : a backslash ("\") is required if you are trying to span the property values to multiple lines. Its more readable.
In my application I want to show this list in a dropdown for which I need this as an ArrayList.
Here is my Code Snippet for the same.
============================================
ArrayList<SelectItem> employeeDesignationList = new ArrayList<SelectItem>();
// Get the Resource Bundle instance.
ResourceBundle resourceBundleConfig= getResourceBundle("EmployeeConfig.properties");
String propertyStr = resourceBundleConfig.getValue("employeeDesignations");
// Get an String Array from the comma separated string.
String propertyArr[] = propertyStr.split(",");
/*
* Now our values are key value pair i.e. 0=Director.
* Hence we would need to split the values again with an equal ("=") as separator.
*/
for(int i=0; i< propertyArr.length ; i++){
String employeeDesignation[] = propertyArr[i].split("=");
String key = employeeDesignation[0];
String value = employeeDesignation[1];
employeeDesignationList.add(new SelectItem(key, value));
}
System.out.println("Values Obtained :: \n ");
for(SelectItem item : employeeDesignationList) {
System.out.println(item.getValue() + " :: " + item.getLabel());
}
============================================
In the above code we create an ArrayList "employeeDesignationList" which is a list of SelectItem objects. The SelectItem is a class in JSF framework which is used by the Dropdown to populate the key/value pair. You can use a different object depending on the implementation.
I had to resort to the above solution as when I tried to use the ResourceBundle.getStringArray() method, I got a ClassCastException at runtime. Its return type is String[] but when you try to run the program it actually return a String object. I read that its advisable to Implement your own ResourceBudle object and override the getStringArray method to return an array. You can customize this method as required.
Here is an example of property file which has a list of key/value combination for employee's designations and its corresponding value.
EmployeeConfig.properties
====================
employeeDesignations=\
0=Director,\
1=Manager,\
2=Clerk,\
3=Business Analyst,\
4=Sales Executive,\
#Note : a backslash ("\") is required if you are trying to span the property values to multiple lines. Its more readable.
In my application I want to show this list in a dropdown for which I need this as an ArrayList.
Here is my Code Snippet for the same.
============================================
ArrayList<SelectItem> employeeDesignationList = new ArrayList<SelectItem>();
// Get the Resource Bundle instance.
ResourceBundle resourceBundleConfig= getResourceBundle("EmployeeConfig.properties");
String propertyStr = resourceBundleConfig.getValue("employeeDesignations");
// Get an String Array from the comma separated string.
String propertyArr[] = propertyStr.split(",");
/*
* Now our values are key value pair i.e. 0=Director.
* Hence we would need to split the values again with an equal ("=") as separator.
*/
for(int i=0; i< propertyArr.length ; i++){
String employeeDesignation[] = propertyArr[i].split("=");
String key = employeeDesignation[0];
String value = employeeDesignation[1];
employeeDesignationList.add(new SelectItem(key, value));
}
System.out.println("Values Obtained :: \n ");
for(SelectItem item : employeeDesignationList) {
System.out.println(item.getValue() + " :: " + item.getLabel());
}
============================================
In the above code we create an ArrayList "employeeDesignationList" which is a list of SelectItem objects. The SelectItem is a class in JSF framework which is used by the Dropdown to populate the key/value pair. You can use a different object depending on the implementation.
I had to resort to the above solution as when I tried to use the ResourceBundle.getStringArray() method, I got a ClassCastException at runtime. Its return type is String[] but when you try to run the program it actually return a String object. I read that its advisable to Implement your own ResourceBudle object and override the getStringArray method to return an array. You can customize this method as required.
No comments:
Post a Comment