Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.1 Form and List Binding Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.1 Form and List Binding

    I have not been able to find a solution on this yet after searching and searching.

    Using Spring 3.1

    I have a form where I would like the user to be able to add multiple point of contacts (poc). In my JSP I have a submit button for adding a poc. It goes to a Spring Controller, gets the current list of pocs from the domain object (which will be empty at first), adds the poc to the list and then puts the domain object back into the model before returning to the same view. Then the user will ultimately submit the entire page which will go to the controller and save the entire object to persistence.

    I have tried various attempts and come up with different results from only being able to add one poc with a new one overwriting the existing, to not being able to get the entered poc displayed on the form. I will put the code I currently have.

    JSP:

    Code:
    <form:form method="post" commandName="request">
    
       ...
    
       <h2>POC:</h2>
       <input type="text" name="newPoc"/>
       <input type="submit" name="addPoc" value="Add POC"/>
    
       <table>
            ...
            <c:forEach items="${request.pointOfContacts}" var="poc" varStatus="vs">
                 <tr>
                     <td><form:label path="pointOfContacts[${vs.index}].name/></td>
                      .....
            </c:forEach>
    
            ......
    
        </table>
    
    </form:form>
    Spring Controller:

    Code:
    @RequestMapping(value="/request", method=RequestMethod.POST, param="addPoc")
    public Sring addPoc(@RequestParam String newPoc, MyRequest req, Model model) {
       PointOfContact poc = new PointOfContact();
       poc.setName(newPoc);
       List<PointOfContact> pocs = req.getPointOfContacts();
       pocs.add(poc);
       req.setPointOfContacts(pocs);
       model.addAttribute("request", req);
       return "requestForm";
    }
    Domain Objects

    Code:
    @Entity
    public class MyRequest {
       ...
    
       @OneToMany(Cascade=CascadeType.ALL)
       private List<PointOfContact> pointOfContacts = new ArrayList<PointOfContact>();
    
       .....
    }
    
    @Entity
    public class PointOfContact {
       ...
    
       private String name;
    
      ....
    }
    Does anybody have any solutions? I have seen various posts about AutoPopulatingList, is this a solution? If so how would I use it in this example.

  • #2
    For starters use a ModelAtrribute currently you aren't and your MyRequest is recreated each time (so annotate the argument with @ModelAttribute). You probably also want to store the MyRequest in the session so use @SessionAttributes to do so, that way you always work with the same object.

    Also you should never get/set a collection if you use ORM as that can break your ORM management. Create an add/remote mouthed on the MyRequest to add a PointOfContact.

    MyRequest class
    Code:
    public class MyRequest {
    
      public void addPointOfContact(String name) {
        PointOfContact poc = new PointOfContact();
        poc.setName(name);
        pointOfContacts.add(poc);
      }
    
    }

    Your controller (which is also cleaned up now).

    Code:
    @Controller
    @SessionAttributes("request")
    public void MyRequestController {
    
      @RequestMapping(value="/request", method=RequestMethod.POST, param="addPoc")
      public Sring addPoc(@RequestParam String newPoc, @ModelAttribute MyRequest req, Model model) {
        req.addPointOfContact(newPoc);
        return "requestForm";
      }
    }

    Comment


    • #3
      Thank you so much for solving this. I have been benefited out of it. This is a great forum really.

      Comment

      Working...
      X