【发布时间】:2013-08-21 11:07:50
【问题描述】:
我有一个使用 Spring 的项目,我需要使用 jQuery 创建管理页面。我有一个包含所有用户的表,我有一个“删除”按钮。当我点击它时,用户应该从数据库中删除。没有脚本一切正常,但使用脚本我无法弄清楚如何让用户从数据库中删除以及如何将用户登录信息发送到控制器。我只能从表中删除行,但是当我刷新页面时,用户仍然存在。 谁能帮助我如何在脚本中从数据库中删除用户?
表格
<table id="userBase" class="data" border="1" width="100%" cellpadding="2" cellspacing="4">
<tr>
<th>Login</th>
<th>First Name</th>
<th>Last Name</th>
<th>Birthday</th>
<th>Role</th>
<th>Actions</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.login}</td>
<td>${user.firstname}</td>
<td>${user.lastname}</td>
<td>${user.birthday}</td>
<td><c:if test="${user.roleid==1}">Admin</c:if>
<c:if test="${user.roleid==2}">User</c:if></td>
<td><a href="edit/${user.login}">Edit </a>
<a class="confirm" href="delete/${user.login}">Delete</a></td>
</tr>
</c:forEach>
</table>
没有脚本的控制器(现在已经注释了,但效果很好)
@RequestMapping("/delete/{userLogin}")
public String deleteUser(@PathVariable("userLogin") String userLogin) {
userService.remove(userService.findByLogin(userLogin));
return "redirect:/welcome";
}
脚本控制器
@Controller
public class SpringController {
@Autowired
private UserService userService;
@RequestMapping(value = "/delete/{userLogin}", method = RequestMethod.POST)
@ResponseBody
public boolean updateUser(@RequestParam("userLogin") String userLogin) {
userService.remove(userService.findByLogin(userLogin));
return true;
}
}
脚本
<script>
$(document).ready(function() {
$("#userBase .confirm").on("click",function() {
var conBox = confirm("Are you sure ?");
if(conBox){
var tr = $(this).closest('tr');
tr.css("background-color","#000000");
tr.fadeIn(1000).fadeOut(200, function(){
tr.remove();
});
} else {
$(this).dialog("close");
}
});
});
</script>
【问题讨论】:
-
我已更新代码以获得更多说明。希望这可以帮助。我仍然不明白如何从表中获取 userLogin 以将其发送到控制器。
标签: java javascript jquery database spring