Description
When you load a page with <form:form modelAttribute="person" ...> , there are two cases:
- case 1: if
person does not exist, it creates an empty person - case 2: if
person already exists, he uses it
In all cases when the page loads, an existing person exists.
When you submit the form, Spring MVC only updates this existing person with the information provided.
So, in case 1, if you send emails 1, 2, 3 and 4, Spring MVC will add 4 letters to the empty person . No problem for you in this case.
But in case 2 (for example, when editing an existing person in a session), if you send emails 1 and 2, but the person already has 4 letters, then Spring MVC will simply replace email 1 and 2. Email 3 and 4 are still exist.
Possible Solution
Probably not the best, but it should work.
Add remove boolean to the Email class:
... public class Email { ... private boolean remove;
In the save method of your controller, delete messages for which 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 letter.
source share