jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes JSF selectOneMenu + Database Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JSF selectOneMenu + Database Values" Watch "JSF selectOneMenu + Database Values" New topic
Author

JSF selectOneMenu + Database Values

Josh Juneau
Ranch Hand

Joined: Jun 16, 2004
Posts: 86
I am trying to create a <selectOneMenu> tag using database values for the <selectItem> tag "itemValue" and "itemLabel" attributes.

I am using a backing bean to populate the database values, however I cannot get this to work properly. My attempts fail when I try to use an iterating scriptlet and backing bean values such as:

<h:selectOneMenu id="thisid" value="#{backingbean.thisproperty}" >
<% String[] valuearray = bean.getvaluearray;
for (int i=0; i<=valuearray.length-1; i++){ %>
<f:selectItem itemValue="#{backingbean.databaseSequenceValue}"
itemLabel="#{backingbean.dynamiclabel" />
<% } %>
</h:selectOneMenu>

Is there a way to achieve database backing values for JSF itemValue and/or itemLabel attributes or do I need to create a custom JSF component for this process?

Any help would be greatly appreciated as this issue is causing me to turn away from JSF...I use dynamic database values to populate most of my web applications, and drop-down lists are used extensively.

Thanks in advance


Database Administrator/Application Developer
will lopez
Greenhorn

Joined: Dec 03, 2003
Posts: 13
Hello:

Not sure this will help you out but I found this code that uses 1.0 tags:
<f:use_faces>
<h:form formName="myForm">
<h:selectone_menu size="3"
valueRef="selectedCountryBean.selectedCountry">
<h:selectitems valueRef="countryListBean.countryList"/>
</h:selectone_menu>
<h:command_button commandName="submit" label="submit"/>
<hr>
You selected:
<h utput_text valueRef="selectedCountryBean.selectedCountry"/>
</h:form>
</f:use_faces>

...converting it to 1.1 looks like this:
<f:view>
<h:form id="myForm">
<h:selectOneMenu value="#{selectedCountryBean.selectedCountry}">
<f:selectItems value="#{countryListBean.countryList}"/>
</h:selectOneMenu>
<h:commandButton value="submit" />
<hr>
You selected: <h utputText value="#selectedCountryBean.selectedCountry}"/>
</h:form>
</f:view>

...I'm no guru but from what I gathered I thought the implementation knew that it had a collection so one didn't have to use an iterating tag or loop but I keep getting a NPE with this code...I haven't had time to figure it out yet.
Josh Juneau
Ranch Hand

Joined: Jun 16, 2004
Posts: 86
Thanks, I will give it a try and post my results...
Josh Juneau
Ranch Hand

Joined: Jun 16, 2004
Posts: 86
That works!

I had to define a method in the backing bean which returned a Collection (ArrayList of SelectItems) of the items I wanted to appear in the SelectMenu...

Thanks for the help!
Clement S.
Greenhorn

Joined: Aug 18, 2004
Posts: 1
Can you show how you get that to work?
Josh Juneau
Ranch Hand

Joined: Jun 16, 2004
Posts: 86
First, you set up your page (JSP in this case) as follows:

<f:view>
<h:form id="mytestform" >
<h:selectOneMenu value="#{BackingBean.mySubject" >
<f:selectItems value="#{BackingBean.mySubjectList}" />
</h:selectOneMenu>
<h:commandButton value="submit" />
</h:form>
</f:view>

Next, you must use a backing bean method to populate an ArrayList then then pass it back to the page as a "Collection":

...

public void setMySubject(String value)
{
mySubject = value;
}
public String getMySubject()
{
return mySubject;
}

...
public String mySubject = null;
private static ArrayList mySubjectList = null;

...

public Collection myBackingBeanArray(){

try{
myQuery = "select subject from MYTABLE " +
"order by MY_id";

rset = db.executeQuery(myQuery);
while(rset.next()){
mySubjectList.add(new SelectItem(rset.getString("subject")));

}
...

}

The first time I tried to implement this solution, I failed to use the new SelectItem() method. Remember to import javax.faces.model.SelectItem to make this work!

Good luck!
jrthor2
Greenhorn

Joined: Dec 13, 2004
Posts: 4
Could this be done using a properties (text) file? If so, could someone show me how to do the bean?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF selectOneMenu + Database Values