Announcement Announcement Module
No announcement yet.
Remove an item from a Set form a web form in Spring MVC 3 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Remove an item from a Set form a web form in Spring MVC 3

    Hi everybody,

    I try to add/remove items from a collection, and it's make me crazy

    I have a class "RequirementDetails" with a Set of "Compliance". I need to update this class from a web form. In my database I've a RequirementDetails with two Compliances, and I want to remove one of them. I do it by removing table row with javascript. The generated HTML seems correct, but when I get the ModelAttribute in my form handler function, I always have the two Compliances ! However the others fields are updated... I don't understand how it works.

    I think it get them back when I populate requirementDetails ModelAttribute...

    Following : How I populate the ModelAttribute
    public RequirementDetails populateRequirementDetails(HttpServletRequest servletRequest) {
        RequirementDetails requirementDetails = null;
        int requirementId = 0;
        // Check if an id is given in a path variable
        Map<?, ?> pathVariables =
            (Map<?, ?>) servletRequest.getAttribute(
        // Get the requirementdetails from business layer
        if(pathVariables != null && pathVariables.containsKey("requirementId")){
            requirementId = Integer.parseInt((String) pathVariables.get("requirementId"));
            requirementDetails = 
        // Else return new empty RequirementDetails
            requirementDetails = new RequirementDetails();
        return requirementDetails;
    Following : How I handle the form submit

    public @ResponseBody RequirementDetails submitRequirementDetailsFormEdit(
            @ModelAttribute("requirement_details") RequirementDetails requirementDetails,
            HttpServletRequest servletRequest) {
        // Here "requirementDetails" which have 2 Compliances always have 2 compliances,
        // even if I remove one of them in the form
        return requirementDetails; 
    Please help me.

  • #2
    Couple of things could be doing this. First - are you also storing your model attribute as a @SessionAttribute? Second, take a look for or search around to learn about Spring's AutoPopulatingList. I went though this same painful experience a while ago.


    • #3
      Thank you for your answer sbirnie.
      My model isn't stored in session attributes.
      So I've search for AutopopulatingList. From some examples I've read, we use it like following :
      List<MyClass> =  new AutoPopulatingList<MyClass>()
      But I use a Set collection because I want to avoid duplication.
      How to use AutopopulatingList with a Set ?


      • #4
        I've try the following, but it doesn't works :
        private Set<Compliance> compliancesList = 
        		new HashSet<Compliance>();
        public Collection<Compliance> getCompliancesList(){
        	List<Compliance> list = new AutoPopulatingList<Compliance>(Compliance.class);
        	return list; 
        public void setCompliancesList(Collection<Compliance> compliancesList){
        	for (Compliance complianceDetails : compliancesList) {
        In the function populateRequirementDetails(), if I return "new RequirementDetails()" (with an empty Set of compliances), then "requirementDetails.getCompliancesList().size( )" equals 0 in the function submitRequirementDetailsFormEdit().
        If I return the RequirementDetails from database (with a Set of 2 compliances), then "requirementDetails.getCompliancesList().size( )" equals 2 in the function submitRequirementDetailsFormEdit().

        So what I do in my form (add/remove compliances) is not taken into consideration...


        • #5
          I've just seen that in the getter getCompliancesList(), I create a new AutoPopulatingList and return it. So it's normal that the add/remove actions on this list are not tken in consideration in the object requirementDetails.

          I need Set to remove duplication (with hibernate).

          So how to do ?


          • #6
            May be I've to user custom binding... But How ?