扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月30日
关键字:
在本页阅读全文(共3页)
在HTML中按下一页或者上一页的时候有如下代码:
<logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/List.do?action=nextPage">
nextPage
</html:link>
</logic:equal>
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/List.do?action=previousPage">
PreviousPage
</html:link>
</logic:equal>
然后在Action中作如下处理:
String currentPage = request.getParameter("currentPage");
HttpSession session = request.getSession();
EmployeeForm employeeForm = (EmployeeForm) form;
String queryString = null;
String queryCon = null;
String action = employeeForm.getAction();
List list = new ArrayList();
PageBean pb = null;
EmployeeDao employeeDao = new EmployeeDao();
if(action == null || action.equals("null")){
int totalRows = employeeDao.getTotalRows();
pb = new PageBean(totalRows);
session.removeAttribute("page");
queryString = employeeForm.getQueryString();
queryCon = employeeForm.getQueryCon();
session.setAttribute("queryString",queryString);
session.setAttribute("queryCon",queryCon);
list = employeeDao.getAllEmployee(queryString, queryCon,
String.valueOf(pb.getPageStartRow()),
String.valueOf(pb.getPageRecorders()));
}else if(action.equals("nextPage")){
queryString = (String)session.getAttribute("queryString");
queryCon = (String)session.getAttribute("queryCon");
employeeForm.setQueryString(queryString);
employeeForm.setQueryCon(queryCon);
pb = (PageBean)session.getAttribute("page");
pb.nextPage();
list = employeeDao.getAllEmployee(queryString, queryCon,
String.valueOf(pb.getPageStartRow()),
String.valueOf(pb.getPageRecorders()));
}else if(action.equals("previousPage")){
queryString = (String)session.getAttribute("queryString");
queryCon = (String)session.getAttribute("queryCon");
employeeForm.setQueryString(queryString);
employeeForm.setQueryCon(queryCon);
pb = (PageBean)session.getAttribute("page");
pb.previousPage();
list = employeeDao.getAllEmployee(queryString, queryCon,
String.valueOf(pb.getPageStartRow()),
String.valueOf(pb.getPageRecorders()));
}
pb.description();
session.setAttribute("page",pb);
request.setAttribute("admin", "admin");
request.setAttribute("employee", list);
return mapping.findForward("showlist");
然后在数据库查询中有如下代码:
/**
*查询总记录数
*/
public int getTotalRows() {
int totalRows = 0;
String sql = "select count(*) from employee";//假设是员工表
Database db = new Database();
ResultSet rs = db.executeQuery(sql);
try {
while (rs.next()) {
String id = (String) rs.getString(1);
totalRows = Integer.parseInt(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
db.close();
return totalRows;
}
/*
*查询每一页需要查询的页码
*/
public List getAllEmployee(String queryString, String queryCon,String startRow,String num) {
List list = new ArrayList();
String sql = null;
if (queryString == null || queryString.equals("")) {
sql = "select * from employee,dept " +
"where dept.Id = employee.deptId " +
"order by employee.id asc"+ " limit "+startRow+","+num;
} else {
sql = "select * from employee,dept " +
"where dept.Id = employee.deptId order by employee."
+ queryString + " " + queryCon + " limit "+startRow+","+num;
}
Employee employee = null;
Database db = new Database();
ResultSet rs = db.executeQuery(sql);
try {
while (rs.next()) {
String id = (String) rs.getString("employee.id");
String name = (String) rs.getString("employee.name");
String deptId = (String) rs.getString("employee.deptId");
String deptName = (String) rs.getString("dept.deptName");
employee = new Employee();
employee.setId(id);
employee.setName(name);
employee.setDeptId(deptId);
employee.setDeptName(deptName);
list.add(employee);
}
} catch (SQLException e) {
e.printStackTrace();
}
db.close();
return list;
}
这里我用了hibernate进行数据库操作,你也可以用jdbc进行操作,情况类似。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者