ch4*_*4mp 9 javascript java jquery spring spring-mvc
我有一个Person包含@Controllers 列表的模型属性.
我创建了一些Javascript代码,用于删除html电子邮件列表中的元素.这是纯Javascript客户端代码,没有AJAX调用.
提交后,我不明白为什么我在相应的@Controller方法中获取所有电子邮件,甚至是在html中删除的电子邮件.
有人可以解释一下吗?
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>
Run Code Online (Sandbox Code Playgroud)
控制器:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
当你加载一个页面<form:form modelAttribute="person" ...>,有两种情况:
person不存在,则创建一个空的Personperson已经存在,则使用它在所有情况下,加载页面时都存在person.
提交表单时,Spring MVC 仅使用提交的信息更新此表单.person
因此,在案例1中,如果您提交电子邮件1,2,3和4,Spring MVC将添加4封电子邮件到空person.在这种情况下你没问题.
但是在案例2中(例如,当您编辑现有person的会话时),如果您提交电子邮件1和2,但是人已经有4封电子邮件,那么Spring MVC将只替换电子邮件1和2.电子邮件3和4仍然存在.
可能不是最好的,但它应该工作.
remove在Email类中添加一个布尔值:
...
public class Email {
...
private boolean remove; // Set this flag to true to indicate that
// you want to remove the person.
...
}
Run Code Online (Sandbox Code Playgroud)
在save控制器的方法中,删除已remove设置为true 的电子邮件.
最后,在JSP中添加以下隐藏字段:
<form:hidden path="emails[${status.index}].remove" />
Run Code Online (Sandbox Code Playgroud)
并告诉您的Javascript在用户单击以删除电子邮件时将输入值设置为true.
| 归档时间: |
|
| 查看次数: |
13487 次 |
| 最近记录: |