Announcement Announcement Module
Collapse
No announcement yet.
Remove an item from a Set form a web form in Spring MVC 3 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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
    Code:
    @ModelAttribute("requirement_details")
    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(
                    HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
    
        // Get the requirementdetails from business layer
        if(pathVariables != null && pathVariables.containsKey("requirementId")){
            requirementId = Integer.parseInt((String) pathVariables.get("requirementId"));
    
            requirementDetails = 
                myBusinessService.getRequirementDetailsById(requirementId);
        }
        // Else return new empty RequirementDetails
        else{
            requirementDetails = new RequirementDetails();
        }
    
        return requirementDetails;
    }
    Following : How I handle the form submit

    Code:
    @RequestMapping(
        method=RequestMethod.POST, 
        value="/projects/{projectRef}/{planType}-plan/edit/v{version}/requirement/{requirementId}/edit"
    )
    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
        //...
    
        myBusinessService.saveRequirementDetails(requirementDetails);
    
        //...       
        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.

    Comment


    • #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 :
      Code:
      List<MyClass> =  new AutoPopulatingList<MyClass>()
      But I use a Set collection because I want to avoid duplication.
      How to use AutopopulatingList with a Set ?

      Comment


      • #4
        I've try the following, but it doesn't works :
        Code:
        private Set<Compliance> compliancesList = 
        		new HashSet<Compliance>();
        
        public Collection<Compliance> getCompliancesList(){
        	List<Compliance> list = new AutoPopulatingList<Compliance>(Compliance.class);
        	list.addAll(compliancesList);
        	return list; 
        }
        
        public void setCompliancesList(Collection<Compliance> compliancesList){
        	this.compliancesList.clear();
        		
        	for (Compliance complianceDetails : compliancesList) {
        		this.compliancesList.add(complianceDetails);
        	}
        }
        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...

        Comment


        • #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 ?

          Comment


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

            Comment

            Working...
            X