• 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

how to cast JSF SelectItem back to original type?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put MyObject to a SelectItem as below:



How can I convert mySelectItem to MyObject?
 
Greenhorn
Posts: 13
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should custom the selectitem class.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

 
reply
    Bookmark Topic Watch Topic
  • New Topic