Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC and form binding : how to remove an item from a List?

I have a Person model attribute that contains a list of emails.

I've created some JavaScript code that deletes elements from an HTML list of emails. This is pure JavaScript client side code, no AJAX call.

After submitting, I don't understand why I get all the emails in the corresponding @Controller method, even the ones that were deleted in the HTML.

Can anyone please explain?


JSP

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
    <link rel="stylesheet" href="<c:url value="/styles/resume.css"/>" type="text/css"></link>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"></link>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    <script src="/resume/js/jquery.editable-1.0.1.js"></script>
    <title>Resumes manager</title>

    <script>
    $(document).ready(function() {
        $('.trash').click(function() {
            $(this.parentNode).remove();
        });

    });

    </script>
</head>

<body>
    <h1>Personal data</h1>
    <form:form modelAttribute="person" action="/resume/person/edit/save" id="personForm" method="post" >
        <table>
            <tr>
                <td>Email addresses:</td>
                <td colspan="4">
                    <ol id="emails">
                        <c:forEach items="${person.emails}" varStatus="status">
                            <li><form:hidden path="emails[${status.index}].order" class="emailsDisplayOrder"></form:hidden><form:input path="emails[${status.index}].label"></form:input><form:input type="email" path="emails[${status.index}].value"></form:input><input type="image" src="/resume/images/trash.png" class="trash" value="${status.index}"></input></li>
                        </c:forEach>
                    </ol>
                </td>
            </tr>
        </table>
    </form:form>
</body>

</html>

Controller

@Controller
@SessionAttributes(types={Person.class}, value={"person"})
public class PersonController {

    private final static String PERSON_VIEW_NAME = "person-form";

    private ResumeManager resumeManager;

    @Autowired()
    public PersonController(ResumeManager resume) {
        this.resumeManager = resume;
    }

    @InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        dataBinder.setDisallowedFields("id");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping(value="/person/edit/save")
    public String save(@ModelAttribute(value="person") Person p, BindingResult result, SessionStatus status) {
        new PersonValidator().validate(p, result);
        Collections.sort(p.getEmails()); //this collection still contains client-side dropped objects

        this.resumeManager.savePerson(p);

        return PERSON_VIEW_NAME;
    }
}
like image 992
ch4mp Avatar asked Oct 12 '12 13:10

ch4mp


1 Answers

Explanation

When you load a page with <form:form modelAttribute="person" ...>, there are two cases :

  • case 1 : if person doesn't exist, it creates an empty Person
  • case 2 : if person already exists, it uses it

In all cases, when a page is loaded, there is an existing person.
When you submit a form, Spring MVC updates this existing person only with the submitted information.

So in case 1, if you submit email 1, 2, 3 and 4, Spring MVC will add 4 emails to the empty person. No problem for you in this case.

But in case 2 (for example when you edit an existing person in session), if you submit email 1 and 2, but person has already 4 emails, then Spring MVC will just replace email 1 and 2. Email 3 and 4 still exist.


A possible solution

Probably not the best one, but it should work.

Add a remove boolean to the Email class :

...
public class Email {

    ...

    private boolean remove; // Set this flag to true to indicate that 
                            // you want to remove the person.

    ...

}

In the save method of your controller, remove the emails that have remove set to true.

Finally, in your JSP, add this hidden field :

<form:hidden path="emails[${status.index}].remove" />

And tell your Javascript to set the input value to true when the user clicks to delete the email.

like image 199
Jerome Dalbert Avatar answered Sep 28 '22 02:09

Jerome Dalbert