But if I will do that I will loose a reference to the object and I will
only have the id which is useless alone
Srikanth Madarapu wrote:
Can you try
this.allCategories.add(new SelectItem(category.getId(), category.getTitle()));
The getId() method should return the same value as returned by getAsString(). I
think there is some problem...
Srikanth
-----Original Message-----
From: Kostas Karadamoglou [mailto:[EMAIL PROTECTED]
Sent: Friday, March 18, 2005 10:56 AM
To: MyFaces Discussion
Subject: Re: Problem with the getAsObject of a Converter
Hi, I attached the getter of allcategories, as you can see I return a
list of selectItem objects
public List getAllCategories(){
if(this.allCategories==null){
this.allCategories=new ArrayList();
this.allCategories.add(new SelectItem(new Category(),"Choose
a category..."));
List categories=this.eventRegistry.getCategories();
for(Iterator iter=categories.iterator(); iter.hasNext();){
Category category=(Category)iter.next();
this.allCategories.add(new SelectItem(category,
category.getTitle()));
}
}
return this.allCategories;
}
Srikanth Madarapu wrote:
What is the return type of allCategories ?
The list coming from allCategories should be a list of "SelectItem" objects, I
think in yours case its just a list of Strings
-Srikanth
-----Original Message-----
From: Kostas Karadamoglou [mailto:[EMAIL PROTECTED]
Sent: Friday, March 18, 2005 9:17 AM
To: [email protected]
Subject: Problem with the getAsObject of a Converter
Hi again!!!
I have another problem now, Very strange Can you suggest me a solution?
I created a converter for objects of type Category in order to display
some Category beans in a selectOneMenu tag.
I have debuged the application and I noticed a strange behaviour of JSF
which I cannot understand.
Below I have the sequence explanation of the problem and the content of
each method and jsp fragment.
1)The getAsString method works perfectly and returns the following
strings "0","1","2","3","4","5","6","7"
2)The page is displayed properly, and I choose from the drop down menu a
category.
2)Afterwords JSF invokes getAsObject (I think for the chosen option)
with the str parameter null !!!!!!!
In this situation I return a null object (which is wrong).
4)After that when the page is redisplayed the getAsString throw an
Exception because of the null value
JSP-------------------------------------------------------
<h:selectOneMenu id="selectCategory"
converter="#{categoryConverter}" value="#{eventHandler.selectedCategory}">
<f:selectItems id="allCategories"
value="#{eventHandler.allCategories}"/>
</h:selectOneMenu>
getAsString------------------------------------------------
public String getAsString(FacesContext facesContext, UIComponent
uIComponent, Object obj) throws ConverterException {
String retValue=null;
if(obj==null)
throw new ConverterException("not null object of type
Category expected");
else if(obj instanceof Category){
Category category=(Category)obj;
map.put(new Integer(category.getId()), category);
retValue=String.valueOf(category.getId());
}else{
throw new ConverterException("object of type Category
expected received: "+obj.getClass().getName()+"="+obj.toString());
}
return retValue;
}
getAsObject------------------------------------------------
public Object getAsObject(FacesContext facesContext, UIComponent
uIComponent, String str) throws ConverterException {
Category retValue=null;
if(str!=null){
try{
System.out.println(str);
Integer id=Integer.valueOf(str);
retValue=(Category)map.get(id);
}catch(Exception ex){
throw new ConverterException("a number formatted string
expected");
}
}else{
//throw new ConverterException("a not null string expected");
return null;
}
return retValue;
}