• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSF selectOneMenu + Database Values

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I will give it a try and post my results...
 
Josh Juneau
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show how you get that to work?
 
Josh Juneau
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could this be done using a properties (text) file? If so, could someone show me how to do the bean?

Thanks
reply
    Bookmark Topic Watch Topic
  • New Topic