Announcement Announcement Module
No announcement yet.
How to manage selection... Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to manage selection...

    I'm a newbie and I have the following problem:

    I have three classes:

    Class1 {
    String id;
    Class2 c2;
    Class3 c3;
    ... getters and setters...

    Class2 {
    String id;
    String description;

    Class3 {
    String id;
    String description;

    and I have to use a form to create/modify Class1 instances, using as input a List of Class2 instances and a list of Class3 instances.

    That is, I want the user choose a Class2 instance from a select, and a Class32 instance from another select, to create a Class1 instance and save it to the database. I want to do validation on these choices.

    I'm using a SimpleFormController as controller, but don't know what are the best methods to override to put the lists into the model (referenceData or formBackingObject or ???).

    I suppose I have to use <spring:transform>, but I really haven't understood it.

    Any help would be really appreciated (I'd like to have snippets of the controller, the jsp page, etc...)


  • #2
    The instance of Class1 should be put in the model by the formBackingObject method (just return it there). The two lists (Class2 and Class3 instances) need to be put in the referenceData map.

    Then, suppose you have a setter on Class1

    setClass2Instance&#40;Class2 object&#41;;
    You'd have to bind a property editor in initBinder capable of transforming Class2 instances to and from Strings and bind on the class2Instance path in your JSP. The spring:transform tag will now use the property editor found by the spring:bind tag to transform whatever you give it.

    <spring&#58;bind path="command.class2Instance">
      <c&#58;forEach items="$&#123;class2List&#125;" var="class2">
      <option value="<spring&#58;transform value="$&#123;class2&#125;"/>">
    One thing however, if you're using identifiers in your property editors, but also want to output the name of the class2, you still have to do a manual c:out:

    <option value="<spring&#58;transform value="$&#123;class2&#125;"/>"><c&#58;out value="$&#123;;"/></option>
    Hope this clarifies things a bit.