aspose file tools
The moose likes JSF and the fly likes getAsObject converter Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSF
Reply Bookmark "getAsObject converter" Watch "getAsObject converter" New topic
Author

getAsObject converter

A Saari
Greenhorn

Joined: Mar 31, 2006
Posts: 26
Hi All

I hava a selectManyListbox - the code below is its getter:

public java.util.List getMySelectManyList() {

// some code ...

Iterator iter = projectsList.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
ProjectsList pl = new ProjectsList();
Integer theKey = (Integer)entry.getKey();
pl.setId(theKey.intValue());
pl.setLabel((String)entry.getValue());
mySelectManyList.add( new SelectItem( pl, pl.getLabel( )));
}
Gregg Bolinger
Ranch Hand

Joined: Jul 11, 2001
Posts: 15230

Is there a question somewhere in there?
A Saari
Greenhorn

Joined: Mar 31, 2006
Posts: 26
oops...continued...

the list displays fine, but when i select an item and go to process it my converter fails - because i don't understand how to handle the value parameter:

public Object getAsObject(FacesContext ctx, UIComponent component, String value)
{

value looks like the result of a toString() call

does anyone know how to implement a converter when the SeletItem holds an object and a label ?

Thanks,
Amy
Gregg Bolinger
Ranch Hand

Joined: Jul 11, 2001
Posts: 15230

your converter? Or one of the built in converters that you are using? You said it fails, what is the error message? What are you trying to convert to?
A Saari
Greenhorn

Joined: Mar 31, 2006
Posts: 26
My converter - is there a builtin converter for <h:selectManyListbox ?

public class ProjectsListConverter implements Converter
{

/** Creates a new instance of ProjectsListConverter */
public ProjectsListConverter() {
}

public Object getAsObject(FacesContext ctx, UIComponent component, String value)
{

if i just pass through the ArrayList of compenent children i get the error:

Validation Error: Value is not valid

from what i read - i have to override the equals method in the ProjectsList object - which is the only reason i created a ProjectsList class.

If i could just get my hands on an example of a converter implementation using an ArrayList of SelectItems where the SelectItem(obj, label) is the constructor being used.

I can't find very many examples of converters out there ...
Amy
A Saari
Greenhorn

Joined: Mar 31, 2006
Posts: 26
fyi

The method

public Object getAsObject(FacesContext ctx, UIComponent component, String value)

in my custom converter class has the 'value' param set to a call to toString() on the object used in the new SelectItem(obj,label) call.

So - you can send yourself the key to the item selected in your h:selectManyListbox by overriding toString

in my ProjectList class

// override to send the key to the getAsObject
// method in ProjectsListConverter
// shows up in the 'value' parameter
public String toString() {
return String.valueOf(this.getId());
}

and you can use the key to get the value from the collection class you used to hold the items you wanted to display in your listbox.

Amy
A Saari
Greenhorn

Joined: Mar 31, 2006
Posts: 26
Solution

The class I was sending to the new SelectItem(myClass,myLabel) had an Integer as the projectID. I changed that to a String. Tossed out my custom converter, tossed out my validator. Made sure myClass extends SelectItem

public class ProjectsListItem extends javax.faces.model.SelectItem implements java.io.Serializable {

then I added <managed-property tag to the managed bean

<managed-bean>
<managed-bean-name>loginModel</managed-bean-name>
<managed-bean-class>xxxxxxxx.LoginModel</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>projectsList</managed-property>
</managed-bean>

and it works. No validation error. I think the response was sending a String and decode() for the component was trying to find/convert its value and I was getting a validation error because my list of SelectItems (projectsList) contained a class that had an Integer projectID rather than a String.

Amy
Ed Dh
Greenhorn

Joined: Mar 22, 2007
Posts: 4
Is it possible to put/add the value selected to a map/list ?
If so how should I accomplish that ?

The code below works fine when displaying the page. The listbox get shown and is populated with the correct values.



The mapOfUnDimSelectItems is of type Map<String,Collection><SelectItem>).
The SelectItem objects have as value a String and as label a String and are created as follows: new SelectItem(unDimEntry.getSid(),unDimEntry.getDisplayNaam());

However when I submit the form I get an java.lang.IllegalArgumentException: Cannot convert UT-UD-005 of type class java.lang.String to interface java.util.List.
Where UT-UD-005 is the value that I selected.

I've spent hours on this. What am I doing wrong ? Is it possible to add the vallue to a map or list ? How should this be done ?

Thanks,
EDH
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: getAsObject converter
 
Similar Threads
Iterator in Generics
Sorting a Hashtable by values & retriving kay-value pair
generics
Generic Collections
Why are Generics necessary here?