您好我有一个spring mvc网络应用程序。当调用控制器时,它会调用sql数据库来获取所有用户。然后它将此列表传递给jasp并添加到表中。但是,当我重新加载pagwe时,之前的列表数据仍然存在并且新数据被附加到它。任何人都可以提供这个问题的解决方案。非常感谢提前
控制器方法
@RequestMapping(value = "/getUsersTable", method = RequestMethod.GET)
public ModelAndView getUses() {
List<User> list = new ArrayList<User>();
list.addAll(service.getUsers());
logger.debug("[User Controller] getUsersTable: Button clicked on user page");
ModelAndView model = new ModelAndView("usersTable");
正在填充的表
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="table-responsive" id="userTable">
<table class="table table-striped table-hover wrapped-table">
<thead>
<tr>
<th>User Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Password</th>
<th></th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${userList}">
<tr>
<td><c:out value="${user.username}"/></td>
<td><c:out value="${user.firstName}"/></td>
<td><c:out value="${user.lastName}"/></td>
<td><c:out value="${user.emailAddress}"/></td>
<td><c:out value="${user.password}"/></td>
<td class="align-center">
<div class="btn-group editBtn">
<button type="button" title="Edit" data-id="${user.userId}" data-username="${user.username}" data-firstname="${user.firstName}"
data-lastname="${user.lastName}" data-emailaddress="${user.emailAddress}" data-password="${user.password}"class="btn btn-primary editUser">
<span class="glyphicon glyphicon-pencil"></span>
</button>
</div>
<div class="btn-group deleteBtn">
<button type="button" id="deleteUser=${user.username}" title="Delete" data-id="${user.userId}" class="btn btn-danger deleteUser">
<span class="glyphicon glyphicon-trash"></span>
</button>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
正在使用的javascript
$('#userTable').load(contextpath +'/getUsersTable')
用户jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<spring:url value="/resources/css/bootstrap.min.css" var="bootMinCss"/>
<spring:url value="/resources/css/main.css" var="mainCss" />
<spring:url value="/resources/js/jquery-3.1.1.min.js" var="jqueryJs" />
<spring:url value="/resources/js/jquery.editable.min.js" var="jqueryEditJs" />
<spring:url value="/resources/js/main.js" var="mainJs" />
<spring:url value="/resources/js/bootstrap.min.js" var="bootstrapMinJs"/>
<link href="${bootMinCss}" rel="stylesheet"/>
<link href="${mainCss}" rel="stylesheet" />
<script src="${jqueryJs}"></script>
<script src="${jqueryEditJs}"></script>
<script src="${bootstrapMinJs}"></script>
<script src="${mainJs}"></script>
<script>
var contextpath='${contextpath}';
</script>
</head>
<body>
<%@ include file="userModal.jsp"%>
<%@ include file="navbar.jsp"%>
<div class="btn-group editBtn">
<button type="button" class="btn btn-primary showTable">
<span class="glyphicon glyphicon-pencil"></span>
</button>
</div>
<div id="userTable"></div>
</body>
</html>
答案 0 :(得分:0)
在每次加载之前清除你的html元素
$('#userTable').empty();
$('#userTable').load(contextpath +'/getUsersTable')