• 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 SelectItem

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi friends I have one doubts in SelectItem constructor having two parameters the constructor is defined like this in java api public SelectItem(java.lang.Object value, java.lang.String label) in which the 1st parameter takes an Object type and the 2nd one as string. Now the problem is when i pass the value as an object i.e the 1st parameter i m not able to see the labels when it renders in webpage but when i convert that object to string by using toString() method it renders the value in webpage.

It is something like this

jsp page

<f:view>
<html>
<body>
<h:form>
<hutputText value="Select choices given below :"/><br><br>
<h:selectManyListbox id="subscriptions" value="#{SItemsBean.options}" size="3">
<f:selectItems value="#{SItemsBean.options}" />
</h:selectManyListbox>
</h:form>
</body>
</html>
</f:view>


public class SItemsBean
{
private List options;
public SItemsBean()
{

SomeClass smc = new SomeClass();
options = new ArrayList();
SelectItem option = new SelectItem(smc, "choice2);// this is just an overview what i m trying to say
// now if i keep the 1st parameter of SeletItem as it is it wont show me the label as choice2 in my webpage but if
// i make the 1st parameter as smc.toString() then it shows me the lablel as choice2 in webpage but why it is so?
// the 1st parameter is an object itself why it needs to be a string only the 1st parameter is what we want to send

// on selecting the label in webpage it is not giving any syntax error because it is perfectly fine but not displaying
// the data i have also tried with the getter of selected label as of Type SomeClass but not working please help

public void setOptions(List opt) {
options = opt;
}

public List getOptions()
{
return options;
}

}

 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To really understand what the rules are, I'd suggest you look at the docs for the Jakarta BeanUtils, but here's some info that might help.

When you construct a SelectItem using the 2-element constructor, the HTML renderer will produce an "<option value="arg1">arg2</option> from it.

BeanUtils is responsible for converting arg1 to a string, and it will do it using various rules, including invoking the "toString" method on it.

However, that's only half the story. When you submit the form and select "arg2" off the list, your browser will send the "arg1" string to the server. In JSF, the BeanUtils will then take care of data conversion of arg1 from a string to the form that the selection value's "set" method needs.

You can't actually serialize a bean, send it to the browser and have the browser return a serialized bean to the server. That's in large part because Java's serialized form is not a text format, but binary. On the other hand, if your class has a constructor that accepts a String as its sole parameter, I think that will make the BeanUtils happy, which makes everything else happy.

I do this fairly often, but not with complex classes. Mostly my selection items are record keys, so they're Integers or things like that.
 
Vibhas Kumar
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Timm is it that the Object smc cant be seriliazed? but it will be converted into string by BeanUtils. Is it what you want to say? If you eloborate your points then it will be even more helpful to me or else provide me some link to understand this concept better. Thnaks in advance.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic