Antonio Vildes Barbosa
Antonio Vildes Barbosa

Reputation: 127

How to use JSF's selectOneMenu?

I'm having problems in using JSF's selectOneMenu.

I have been trying somethings like the ones below, but no one seems to work...

Can someone help me?

JSP:

<h:selectOneMenu value="#{myBean.listCats.desc}" id="desc">
    <f:selectItems value="#{myBean.selectAllCats}" />
</h:selectOneMenu>

myBean:

(...)
    public Collection<SelectItem> selectAllCats() throws (some exceptions...){  
        this.listCats = this.controller().selectAllCats();
        Collection<SelectItem> toReturn = new ArrayList<SelectItem>();  

        for( int i = 0; i<this.listCats.size(); i++){  
          toReturn.add( new SelectItem( this.listCats.get(i).getCod(), this.listCats.get(i).getDesc()));  
        }
        return toReturn;
    }
(...)

Just a bit more info:

List<Cats> listCats = new List<Cats>();
//-------------------------------------
class Cats{
    private int cod; // both with getters and setters
    private String desc;
}

Thanks in advance!


Edit:

I forgot to say that I keep getting the following error:

'#{myBean.selectAllCats}' Property 'selectAllCats' not found on type path.to.myBean

Upvotes: 1

Views: 4666

Answers (4)

Antonio Vildes Barbosa
Antonio Vildes Barbosa

Reputation: 127

I didn't know about the tag info page, and that's why I didn't search my answer there.

You can find it in this link: https://stackoverflow.com/tags/selectonemenu/info

Or:

By the way, have you looked at the tag info page of the [selectonemenu] tag which you put on the question yourself? Hover it with your mouse until a black box shows up and then click therein the info link. – BalusC

The answer I was looking for is in the Dynamic List section of that info page, which is quoted below for further reference on the matter:

Dynamic list

You can use to display a list which is dynamically populated in the backing bean. You can use javax.faces.model.SelectItem to represent a pair of item value and label.

View:

<h:form>
     <h:selectOneMenu value="#{bean.selectedItem}">
         <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
         <f:selectItems value="#{bean.availableItems}" />
     </h:selectOneMenu>
     <h:commandButton value="Submit" action="#{bean.submit}" /> </h:form>

Model:

private String selectedItem; // +getter +setter private
 List<SelectItem> availableItems; // +getter (no setter necessary)

 @PostConstruct public void init() {
     availableItems = new ArrayList<SelectItem>();
     availableItems.add(new SelectItem("foo", "Foo label"));
     availableItems.add(new SelectItem("bar", "Bar label"));
     availableItems.add(new SelectItem("baz", "Baz label")); 
 }

The availableItems can also be a SelectItem[]. If you omit the item label

and thus can use the item value as both option value and option label, then you can also use a List or String[] instead.

private String selectedItem; // +getter +setter private List<String>
 availableItems; // +getter (no setter necessary)

 @PostConstruct public void init() {
     availableItems = Arrays.asList("foo", "bar", "baz"); 
 }

Thanks!

Upvotes: 0

BalusC
BalusC

Reputation: 1109735

'#{myBean.selectAllCats}' Property 'selectAllCats' not found on type path.to.myBean

For properties, you should be providing a valid getter method. You didn't have any one, as the exception is trying to tell you. A valid getter method for selectAllCats property should look like this.

public Collection<SelectItem> getSelectAllCats() {
    return selectAllCats;
}

Note that performing business job in a getter is bad practice. You should be doing that in the (post)constructor of the bean.

See also:

Upvotes: 3

faradaj
faradaj

Reputation: 3689

The method

public Collection<SelectItem> selectAllCats() throws (some exceptions...)

returns a Collection, which is not supported by <h:selectOneMenu> in JSF 1.2.

This method should return List, Map or Object[] so its sign should be rewritten like this:

public List<SelectItem> selectAllCats() throws (some exceptions...)

Apart from that, you'll face another problem with your JSP at this line:

<h:selectOneMenu value="#{myBean.listCats.desc}" id="desc">

listCats is a List composed by Cats (which should be renamed with Cat) and this list doesn't have a desc() method. This line also should be modified like this (modify according your needs:

<h:selectOneMenu value="#{myBean.listCats.get(0).desc}" id="desc">

Upvotes: 0

SJuan76
SJuan76

Reputation: 24895

 List<Cats> listCats = new List<Cats>();

And

 myBean.listCats.desc

List does not have a property called desc. Cats have, but listCats is a list.

BTW: Better if your class names are singular, unless they represent a group.

Upvotes: 1

Related Questions