我想使用spring mvc在数据库中显示一个表到jsp。当我刷新页面时,表会一次又一次地重新加载。即使表刷新后,我也只需要显示一次表。任何人都可以帮助我理解为什么重复填充相同的数据以及如何阻止它发生?
JobsDAO:
package com.sample.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.sample.bean.JobsBean;
import com.sample.util.ConnectionProvider;
public class JobsDAO {
static Connection c;
static PreparedStatement ps;
static ResultSet rs;
static List<JobsBean> jobsBeans = new ArrayList<JobsBean>();
public List<JobsBean> jobs() {
try {
c = ConnectionProvider.getConnectionForJobs();
ps = c.prepareStatement(
"select name,age,salary,designation from jobs");
rs = ps.executeQuery();
while (rs.next()) {
if(rs.getInt(2)<=50){
JobsBean mBean = new JobsBean();
mBean.setName(rs.getString(1));
mBean.setAge(rs.getInt(2));
mBean.setSalary(rs.getInt(3));
mBean.setDesignation(rs.getString(4));
jobsBeans.add(mBean);
} else if (rs.getInt(2)>50){
JobsBean mBean = new JobsBean();
mBean.setName(rs.getString(1));
mBean.setAge(rs.getInt(2));
mBean.setSalary(rs.getInt(3));
mBean.setDesignation(rs.getString(4));
jobsBeans.add(mBean);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return jobsBeans;
}
}
JobsService:
package com.sample.service;
import java.util.List;
import com.sample.bean.JobsBean;
import com.sample.dao.JobsDAO;
public class JobsService {
public List<JobsBean> getJobs() {
List<JobsBean> jobs = null;
JobsDAO jobsDAO = new JobsDAO();
jobs = jobsDAO.jobs();
return jobs;
}
}
工作(控制人员):
package com.sample.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.sample.bean.JobsBean;
import com.sample.service.JobsService;
@Controller
public class Jobs {
@RequestMapping("/jobs")
public ModelAndView jobs() {
JobsService jobsService = new JobsService();
List<JobsBean> jobsBeans = jobsService.getJobs();
ModelAndView modelAndView = new ModelAndView("jobs");
modelAndView.addObject("jobsBeans", jobsBeans);
System.out.println("jobsBeans " + jobsBeans);
return modelAndView;
}
}
jobs.jsp
<html>
<body>
<c:choose>
<c:when test="${not empty jobsBeans}">
<div id="table-wrapper">
<div id="table-scroll">
<table class="table table-hover myStyle" id="myT2" border="1">
<col width="500">
<col width="50">
<col width="200">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Designation</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<c:forEach var="listValue" items="${jobsBeans}">
<tr>
<td>${listValue.name}</td>
<td>${listValue.age}</td>
<td>${listValue.designation}</td>
<td>${listValue.salary}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</c:when>
<c:when test="${empty jobsBeans}">
No employee has been registered for today.
</c:when>
</c:choose>
</body>
</html>
答案 0 :(得分:0)
因为你有一个静态变量
static List<JobsBean> jobsBeans
每次刷新页面时都会添加
考虑让它变得不静态并且做
public List<JobsBean> jobs() {
jobsBeans = new ArrayList<JobsBean> ();