aspose file tools*
The moose likes JSF and the fly likes Can I supply a Bean in selectItem.setValue()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Can I supply a Bean in selectItem.setValue()?" Watch "Can I supply a Bean in selectItem.setValue()?" New topic
Author

Can I supply a Bean in selectItem.setValue()?

Sergey Kargopolov
Ranch Hand

Joined: Jul 04, 2003
Posts: 63
Good day.

Can I store a Bean(with setters and getters) in selectItem.setValue(a Bean here)?

My problem is that until selectItem.setValue("1") is set with String everything works well. But if I set it with a Bean(with simple setters and getters) or with a "new Integer()" for example it stops working properly.

selectManyListBox is displayed okay but when the form is submited "action method" action="#{EventJSFBean.addFriend} is not called.

<h:commandButton id="addFriend" value="Add" action="#EventJSFBean.addFriend}"/>

Once I change selectItem.setValue("1") to accept String then action method addFriend() is called and values are submited.

Thank you.
Sergey Kargopolov
Ranch Hand

Joined: Jul 04, 2003
Posts: 63
The problem is still there... I just cannot figure it out.

If value is set to Integer the actionListener and action method does not get called. Everything starts working when I change the value to String.

In this example SelectManyListBox displays correctly but actionListener method is not called.

public SelectItem[] getTestValues()
{
SelectItem[] values = new SelectItem[1];
SelectItem item = new SelectItem();
item.setValue(new Integer("1"));
item.setLabel("Test");
values[0] = item;
return values;
}

This eample is 100% working


public SelectItem[] getTestValues()
{
SelectItem[] values = new SelectItem[1];
SelectItem item = new SelectItem();
item.setValue("1")
item.setLabel("Test");
values[0] = item;
return values;
}

Where could be the problem?
Sergey Kargopolov
Ranch Hand

Joined: Jul 04, 2003
Posts: 63
I put <h:message for="selectManyListBox"/> to see if there is an error and there is one.

I get Validation Error: Value is not valid when form is submited.
Sergey Kargopolov
Ranch Hand

Joined: Jul 04, 2003
Posts: 63
I got it working only by creating and using my own Convertor:

public class AnyStringToInteger implements Converter{
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try{
return Integer.valueOf(value);
}catch(Throwable t){
return new Integer(0);
}
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
return String.valueOf(value);
}
}


if not this Convertor, it does not work. Integers(Objects) are not allowed as values in SelectItem if standard Convertors are used.
[ February 12, 2007: Message edited by: Sergey Kargopolov ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I supply a Bean in selectItem.setValue()?