Question

I have this class:

The PreApprovalRequest class has a property headings that gets populated automatically in the controller (see code of Controller lower on the page).

public class PreApprovalRequest {
    private Long id;
    private String Description;
    private Collection<Headings> headings; //this property!
}

And the controller:

@Controller
@SessionAttributes({"preApprovalRequest", "productRecommendations"})
public class RequestController {

    @RequestMapping(value = "/submit",  method = RequestMethod.POST)
    public String submitResults(@ModelAttribute(value = "preApprovalRequest") @Valid PreApprovalRequest preApprovalRequest, BindingResult errors) {
        //HERE: It looks like if I have some headings in the preApprovalRequest object already, the call of this method will not delete those, but will append to the existing list.
        //save object in DB.
        return "dashboard";
    }
}

Any idea how to make Spring replace collection objects rather than adding to it?

No correct solution

OTHER TIPS

How I understood your question is, you cann't replace your headings collections to have a new collections of Headings. Can't you do that in the following way?

preApprovalRequest.setHeadings(new ArrayList<Headings>());
preApprovalRequest.getHeadings().add(heading1);

Does it happen even if it's the first time you submit your form? If not, try cleaning your cache after you're done with all actions in your controller.

Add the SessionStatus in your method and call "setComplete()" to clean the attributes. Thus, the second time you start your process, your cache will be empty and you will have to use a new preApprovalRequest

@RequestMapping(value = "/submit",  method = RequestMethod.POST)
public String submitResults(@ModelAttribute(value = "preApprovalRequest") @Valid PreApprovalRequest preApprovalRequest, BindingResult errors, SessionStatus status) {
    /HERE: It looks like if I have some headings in the preApprovalRequest object already, the call of this method will not delete those, but will append to the existing list.
    //save object in DB.

    status.setComplete();
    return "dashboard";
}

If what you are looking for is dynamic shrinking /expanding kind of List, then collection has to be lazy.Apache commons collection provides a Lazy List class which does that.You can get the apache commons collection from : http://commons.apache.org/collections/

So your controller model can be re written as:

public class PreApprovalRequest {
    private Long id;
    private String Description;
    private List headings;

    public PreApprovalRequest()
    {
        headings=LazyList.decorate(
           new ArrayList(),
           FactoryUtils.instantiateFactory(Header.class));
    }

}

Idea behind binding data to list is, keeping track of the list item index with in the html elements name attribute:

<c:forEach item="${modelAttr.list}" varStatus="listItem">

<form:input type="text" path="list[${listItem.index}]" />

</c:forEach>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top