Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List box initialization through managed bean

 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Franz van Betteraey
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic