wood burning stoves 2.0*
The moose likes JSF and the fly likes List box initialization through managed bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "List box initialization through managed bean" Watch "List box initialization through managed bean" New topic
Author

List box initialization through managed bean

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Its like this, a bean with String array say "numbers".
I have a jsp with selectOneMenu tag. Further i have written code for initializing the numbers array as list-entry with values(1,2,3). I am not able to associate this array with my listbox (or selectOneMenu) tag, imean the tag and value. I have tried replacing selectOneMenu with dataTable with its value set as "bean.numbers" var="row" and outputting it in a column using outputText value="#{row}" (it works, that is list-entries value are being set).
Instead of dataTable i want to use selectOneMenu, how do I achieve the same?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Franz van Betteraey
Greenhorn

Joined: Jun 20, 2007
Posts: 18
Hi Akhilesh,

I would suggest better to generate a Map whose entries represent the labels and values of a SelectItem, eg.

private Map<String, String> numbers = new TreeMap<String, String>();

You can fill the list in your faces-config.xml (or by code):

<managed-bean>
<description>some bean</description>
<managed-bean-name>someBean</managed-bean-name>
<managed-bean-class>SomeBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<description>predifined list of numbers</description>
<property-name>numbers</property-name>
<map-entries>
<key-class>java.lang.String</key-class>
<value-class>java.lang.String</value-class>
<map-entry>
<key>1</key>
<value>1</value>
</map-entry>
<map-entry>
<key>2</key>
<value>2</value>
</map-entry>
<map-entry>
<key>3</key>
<value>3</value>
</map-entry>
</map-entries>
</managed-property>
</managed-bean>


than include a method getNumbers in your bean code


public Map<String, String> getNumbers() {
return numbers;
}

and use the selectItems Tag to fill the selectOneMenu Component

<h:selectOneMenu value="#{bean.number}">
<f:selectItems value="#{bean.numbers}" />
</h:selectOneMenu></td>

(Also declare a bean property "number" to store the number to be post)

Hope this works.

Regards
Klaus
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Thanks Klaus, i tried it. I dont know where I could initialize the Map. I just declared it and provided corresponding getters and setters.
when I run my JSP I get exception,

"javax.servlet.jsp.JspException: Conversion Error setting value ''{0}'' for ''{1}''. "
Franz van Betteraey
Greenhorn

Joined: Jun 20, 2007
Posts: 18
Maybe the Declaration of your Map (Generics?) does not fit to the configuration of your managed bean (<map-entries>/<key-class>/<value-class>).
It would be helpfull if you post some parts of your code (faces-config.xml, Bean and JSP).

Good Luck
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Okay!

Here is portion of bean declation, (it is by name UserBean)



Following is the code in faces-config,




Code in JSP
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
For the face-config.xml I dont know if the code before property name would be interest to you, anyways, i snap it right from managed-bean tag itself...

Franz van Betteraey
Greenhorn

Joined: Jun 20, 2007
Posts: 18
it must be selectItems (with s at the end)



I'm always smiling. To look sad I have to perfom a headstand normally,.....
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
...and... and... and....... it worked! !! Thanks a lotttt Klaus. Not so important but still just one query, the list-box always gets filled up in a 3-2-1 fashion, irrespective of order map-entry tags in faces-config, any ideas why?
[ June 22, 2007: Message edited by: Akhilesh Trivedi ]
Franz van Betteraey
Greenhorn

Joined: Jun 20, 2007
Posts: 18
Use a sorted Map to influence the order.

private Map<String, String> numbers = new TreeMap<String, String>();

I do not know (better I do not believe) if the order of the map-entries in faces-config is secure.

Use other Types than String to sort numbers :-)

Have fun!
 
 
subject: List box initialization through managed bean