0 Replies Latest reply on Aug 20, 2009 7:06 PM by Chris Chris

    Converter instance created no matter the scopeType ?

    Chris Chris Newbie


      My custom converter is used in a pickList to select amongst users.
      It works just fine when I load the page. The selected users are in the right part of the pickList. The unselected one on the left, as expected.

      But once I submit the form (even if didn't do any change) i receive a nullPointer.
      I know why now. It's because, in my converter, I pass the collection of objects from which the converter should retrieve the right one using the getAsObject, and I store the collection in the Converter.

      But the fact is that, a new converter is created at the time of the submit of my changes.
      And thus when the getAsObject is called, the collection is empty, of course.
      Why is the converter recreated ?
      I used the ScopeType=Conversation.
      Even when using SESSION scope type it gives that problem.

      What obvious am I missing ?

      The idea behind this converter is :
      As I 'm not usint a DB and thus no entityManager, I can't use <s:convertEntity />
      So to retrieve the right object, I need to pass my object collection in some way to the converter for him to store it and retrieve the right object at the time getAsObject is called.

      Use of the converter :

              rendered="#{not empty profileHome.getPotentialUsers()}"
          <s:selectItems value="#{profileHome.getPotentialUsers()}" 
              var="user" label="#{user.lastName} #{user.firstName}"/>
          <own:UserConverter collection="#{profileHome.getPotentialUsers()}" 
              identifier="id" />

      Code of the converter:

      @Install(precedence = Install.FRAMEWORK)
      public class UserConverter implements javax.faces.convert.Converter {
          private Log log = LogFactory.getLog(UserConverter.class);
          private String identifier;
          private List<?> collection;
              log.info("UserConverter::new  CREATING a new one object !! ");
          public Object getAsObject(FacesContext ctx, UIComponent component, String s) {
              if (s == null) {
                   return null;
              if (collection != null) {
                  for (Object item : collection) {
                      String id = getItemIdentifier(item, s);
                      if (id != null && id.equals(s)) {
                          log.info("UserConverter::getAsObject found id [{" + id + "}]");
                          return item;
              } else 
                  log.info("UserConverter::getAsObject collection is null !!");
              return null;
          public String getAsString(FacesContext ctx, UIComponent component, Object o) {
              String id = getItemIdentifier(o, identifier);
              return id;
          protected String getItemIdentifier(Object o, String property) {
              PropertyDescriptor desc;
              Object result;
              try {
                  desc = new PropertyDescriptor(property, o.getClass());
                  result = desc.getReadMethod().invoke(o);
                  return result.toString();
              } catch (Throwable e) {
                  log.error("Unable to get object identifier!", e);
              return null;
          public List<?> getCollection() {
              return collection;
          public void setCollection(List<?> collection) {
               this.collection = collection;
          public String getIdentifier() {
              return identifier;
          public void setIdentifier(String identifier) {
              this.identifier = identifier;