• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to cast JSF SelectItem back to original type?

 
Kamila Rutkowski
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put MyObject to a SelectItem as below:



How can I convert mySelectItem to MyObject?
 
yun hu
Greenhorn
Posts: 13
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your must write a Converter. Here is the example:
public class CustomConverter implements Converter {

public Object getAsObject(FacesContext facesContext, UIComponent component, String string)
throws ConverterException {
if (StringUtil.isStrEmpty(string))
return null;

try {
Integer.valueOf(string);
} catch (Exception e) {
return string;
}
return Integer.valueOf(string);
}

public String getAsString(FacesContext facesContext, UIComponent component, Object object)
throws ConverterException {
if (object == null)
return null;
if (object instanceof java.util.Date) {
return DateUtil.formatDate((Date) object, "dd/MM/yyyy");
}

return String.valueOf(object);
}
}
 
Kamila Rutkowski
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly I need the cast within method getAsObject of converter. There is a list of SelectItem(MyObject) as a property of my backing bean which I fill it using DAO. So I want to iterate over this list to return appropriate object.

yun hu wrote:your must write a Converter. Here is the example:
public class CustomConverter implements Converter {

public Object getAsObject(FacesContext facesContext, UIComponent component, String string)
throws ConverterException {
if (StringUtil.isStrEmpty(string))
return null;

try {
Integer.valueOf(string);
} catch (Exception e) {
return string;
}
return Integer.valueOf(string);
}

public String getAsString(FacesContext facesContext, UIComponent component, Object object)
throws ConverterException {
if (object == null)
return null;
if (object instanceof java.util.Date) {
return DateUtil.formatDate((Date) object, "dd/MM/yyyy");
}

return String.valueOf(object);
}
}
 
yun hu
Greenhorn
Posts: 13
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should custom the selectitem class.
 
Rory Evans
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should be able to call getValue() on your SelectItem and cast the result to your custom object:

SelectItem mySelectItem = new SelectItem(new MyObject("1", "kamila"));

MyObject myObject = (MyObject) mySelectItem.getValue();
 
Kamila Rutkowski
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so very much

Rory Evans wrote:I think you should be able to call getValue() on your SelectItem and cast the result to your custom object:

SelectItem mySelectItem = new SelectItem(new MyObject("1", "kamila"));

MyObject myObject = (MyObject) mySelectItem.getValue();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!