• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get SelectItem's Label in backing Bean

 
Sunny Shah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have one SelectOneMenu having selectItems , now i want to get selected item from menu bean , how can i get it. I wan't to get label,description and value out of it


Thanks & Regards
Sunny
 
Sunny Shah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to bind selectItem to backing bean ?
 
Akaine Harga
Ranch Hand
Posts: 99
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a too general question. What exactly do you want? To read the selected value from a backing bean after it was submited, or display backing data in your template?

Anyways the general recommendations would be:
- write a converter to display labels and to save objects (integers or whatever your ids are);
- in your backing bean use List<SelectItem> or Collection<SelectItem> as backing property;
- fill this property using SelectItem constructor setting value, label and description of each option adding each one to the list;
- set this property as value of f:selectItems so JSF would list the options automatically;

This way if you fill the list property before the page is loaded or use some ajax to update the data the selectOneMenu would get filled with the data. If you have any difficulties setting all this up let me know ;)
 
Enrique Villamizar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
“..To read the selected value from a backing bean after it was submited...”
That's what I need.

1) The JSP has:


2) In the backing bean the items are taken from a database query:



3) The get method:

3) The question is: How can I get the selected item after the form was submitted?

Thanks in advanced for any help you could offer me.
 
Mallika gowd
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF:

you have put value null in JSF selected Item..

have look at sample code:

<h:selectOneMenu value="#{esFormBB.selectedItem}">
<f:selectItem itemValue="" itemLabel="Select a serial number"/>
<f:selectItems value="#{esFormBB.serialNumbersList}" />
</h:selectOneMenu>


And in backing bean write getter and setter methods..


public String getSelectedItem() {
return selectedItem;
}

public void setSelectedItem(String selectedItem) {
this.selectedItem = selectedItem;
}
 
Enrique Villamizar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mallika Gowd:

Thank you very much for your quick and accurate response. The idea is simple but I spent two days working on it. That was what I was looking for. It works!

Now that I get the selected item I wonder if there is a way to associate a number to it, because I need to save in a new table not the description showed to the user (selectedItem) but the id (primary key) corresponding to that selectedItem.

If there's no way I could have to execute a new query to get it.

Anyway my big problem (for now) has disappeared. Once again, Thank you.
 
Mallika gowd
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think i understand you correctly...
for example if you see below example we have two parameters first one is value you can pass to the new database second one item lable which is visable to the user like your description.

select item in BB

SelectItem(Item Value,slist.Item Lable)



 
Enrique Villamizar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Mallika you hit the target!

The final solution:

At the beginning I used the constructor SelectItem(java.lang.Object value) where value was the description which I got back in the selectedItem.



Now I use the constructor SelectItem(java.lang.Object value, java.lang.String label) , where value is the table's primary key and label is the description showed to the user in the GUI.



Like magic now I get the id instead of the label of the selectedItem!

Being a dummy learner is really hard but sometimes I feel happy...especially when I solve (with help of Javaranch's members) a simple problem!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!