Populate dropdown

I have a dropdown menu with 5 values. As of now the name value pairs are hard coded and added into my collection object in the backing bean.

Now I want to fetch this list of 5 name value pairs from the properties file and populate my collection object.

Problem here is that I don't have a clue, how to define a name value pair list in my properties file and how do I retrieve the same.

All I can think about is place the data as a CSV string and later parse it after fetching it. Any suggestions or pointers to implement the above, please let me know.

SirG

[589 byte] By [SirGenerala] at [2007-11-27 2:59:37]
# 1

Propertiesfiles aren't that hard. First create a text file named "filename.properties" and put the key=value pairs in it. Then retrieve the InputStream from it using FileInputStream or ClassLoader and load it in properties using [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html]Properties#load()[/url]. Finally you can use getProperty() to retrieve the value for the given key or use propertyNames() to retrieve an iterable enumeration of keys.

BalusCa at 2007-7-12 3:40:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

>I have a dropdown menu with 5 values. As of now the name value pairs are hard coded and added into my collection object in the backing bean.

well ru you refering to the one with JSF(BackingBean) ?

if it is here is a typical example where we can acheive it.

If you want to access ResourcesBundle using Backing and then create a List of Select items and then

populate in the Page View.

ProjectResourcesBundle.properties:

==================================

-

-

-

# default values always insert in form of key=value pairs

proj.dropdown.value=US,UK,UAE,GERM,IND,SL,AUS

proj.dropdown.text=United States,United Kingdom,United Arab Emrites,Germany,India,Srilanka,Australia

-

-

-

Editing Message Resource Bundle with diffrent drop down values & labels separated by ","

with keys proj.dropdown.value & proj.dropdown.text

faces-config.xml:

=================

<!-- points to WEB-INF/classes/ProjectResourcesBundle.properties file -->

<application>

<message-bundle>ProjectResourcesBundle</message-bundle>

</application>

<managed-bean>

<managed-bean-name>UiBean</managed-bean-name>

<managed-bean-class>com.uiBean.UiBackingBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>

Configuring faces-config.xml file accordingly.

Sample jsp view :

=================

<f:view>

<h:form>

<!-- the dropdown box -->

<h:selectOneMenu id="DropDown" value="#{UiBean.ddValue}">

<f:selectItems value="#{UiBean.ddList}"/>

</h:selectOneMenu>

</h:form>

</f:view>

UiBackingBean:

==============

public UiBackingBean{

private List ddList;

private String ddValue;

private ResourceBundle bundle;

public UiBackingBean(){

ArrayList value = new ArrayList();

ArrayList txt = new ArrayList();

this.ddList = new ArrayList();

String bundleName = FacesContext.getCurrentInstance().getApplication().getMessageBundle();

Locale locale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();

//Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

// reading the configured resource bundle & exctracting values with hardcoded keys

this.bundle = ResourceBundle.getBundle (bundleName,locale);

String values = this.bundle.getString("proj.dropdown.value");

String text = this.bundle.getString("proj.dropdown.text");

StringTokenizer st = new StringTokenizer(values,",");

while(st.hasMoreElements()){

value.add(st.nextElement());

}

st = new StringTokenizer(text,",");

while(st.hasMoreElements()){

txt.add(st.nextElement());

}

for(int i = 0; i < values.size() ; i++){

SelectItem si = new SelectItem(value.get(i),txt.get(i));

this.ddList.add(si);

}

}

public void setDdList(List ddList){

this.ddList = ddList;

}

public List getDdList(){

return this.ddList;

}

public void setDdValue(String ddValue){

this.ddValue = ddValue;

}

public String getDdValue(){

return this.ddValue;

}

}

else if you wanna use normally you may go about and use it by creating a variable in the viewing page & use its keys using EL.

by <f:loadbundle basename="ProjectResourcesBundle" var="variable" />

and value using EL "#{variable['KeyName']}"

hope that might help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 3:40:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Thanks Rahul,I was referring to the backing bean in Struts. But anyway I've got some clue on how to proceed. Thanks once again.And I am successful!!!SirGMessage was edited by: SirGeneral
SirGenerala at 2007-7-12 3:40:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...