"what are the best spring converter strategies in the case of a string to convert to a set of object?" Code Answer

5

a well-considered question, even it'll scare most people off :)

anyway, i think option (2) is the closest to a practical solution. my first suggestion is that you encapsulate the list of tags into its own model class. this will give the data binding framework a concrete type to register against, whereas list and string are much too general.

so you would have the model classes:

public class entry {
  private string name;
  private taglist taglist;
}


public class taglist {

   private final list<tag> tags;

   public taglist(list<tag> tags) {
      this.tags = tags;
   }

   public list<tag> gettags() {
      return tags;
   }
}

you then have a propertyeditor that knows how to convert to and from a taglist:

public class taglisteditor extends propertyeditorsupport {

   @override
   public void setastext(string text) throws illegalargumentexception {
      taglist taglist = // parse from the text value
      setvalue(taglist);
   }

   @override
   public string getastext() {
      taglist taglist = (taglist) getvalue();
      return taglist.tostring(); // or whatever
   }
}

and finally, you need to tell the controller to use the converter:

@controller
public class entrycontroller {

   @initbinder
   public void initbinder(webdatabinder binder) {
      binder.registercustomeditor(taglist.class, new taglisteditor());
   }

   // request mappings here
}

i'm fairly sure the new spring 3 converter framework would produce a more elegant solution, but i haven't figured it out yet :) this approach, however, i know works.

By Mickael Maison on August 18 2022

Answers related to “what are the best spring converter strategies in the case of a string to convert to a set of object?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.