主要实现简单分页功能,ajax异步响应返回html类型的数据。值得注意的是jquery的运用会使得页面流程更加的合理,减少不必要的跳转也就是所谓的无刷新。spring中bean的注入是否会导致内存溢出,tomcat多次启动后会出现内存溢出的错误,在spring注入bean应该有一些规则,bean什么会实例化,需要进一步的弄清楚。
ajax异步响应的例子:
$(function(){
$("#list").focusout(function(){
$.ajax({
type:"POST", //请求方式
url:"index/InstruList", //请求路径
cache: false, //(默认: true,dataType为script和jsonp时默认为false) jQuery 1.2 新功能,设置为 false 将不缓存此页面。
data:"page="+$("#list").val()+"&flag=list", //传参
dataType: "html", //返回值类型 使用json的话也可以,但是需要在JS中编写迭代的html代码,如果格式样式
success:function(data){
$("#show").html(data);
}
});
}) })
响应端action:
通过发送的page获取页数并调用相应的list
@RequestMapping("/index/InstruList")
public String getIndexFlush(HttpServletRequest request,Model model){
int ipage = 0;
String flag =null;
String page = request.getParameter("page");
flag = request.getParameter("flag");
int spage = (int)request.getSession().getAttribute("currentpage");
if(page != null&&flag.equals("list")){
ipage = Integer.parseInt(page)-1;
request.getSession().setAttribute("currentpage", ipage);
}
if(flag.equals("pre")){
if(spage!= 0)
ipage = spage - 1;
request.getSession().setAttribute("currentpage", ipage);
}
if(flag.equals("next")){
if(spage!= instruinfoservice.getQueryForPage(6,0).getTotalpage()-1)
ipage = spage + 1;
request.getSession().setAttribute("currentpage", ipage);
}
model.addAttribute("icurrentpage", ipage+1);
model.addAttribute("instruinfo", instruinfoservice.getQueryForPage(6,ipage).getRecords());
return "InstruListForPage";
}
InstruListForPage为jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>仪器编号</th>
<th>仪器名称</th>
<th>仪器类型</th>
<th>仪器厂家</th>
<th>借出状态</th>
</tr>
</thead>
<tbody>
<c:if test="${!empty instruinfo }">
<c:forEach items="${instruinfo }" var="i" varStatus="s1">
<tr class="odd gradeX">
<td>${i.instru_num}</td>
<td>${i.instru_name}</td>
<td>${i.instru_type}</td>
<td>${i.instru_maker}</td>
<td>${i.instru_borrowstatus}</td>
</tr>
</c:forEach>
</c:if>
</tbody>
<tr> 第${icurrentpage }页</tr>
</table>
</body>
</html>
service:
@Override
public int getIndexPercent() {
long totalnum = instruinfodao.getAllQueryNum();
long ablenum = instruinfodao.getAbleNum();
double percent = ((totalnum-ablenum)*1.0/totalnum);
System.out.println((totalnum-ablenum)*1.0/totalnum);
int ipercent = (int) (percent*100);
return ipercent;
}
@Override
public PageView getQueryForPage(int PageSize, int StartPage) {
return instruinfodao.getQueryForPage(PageSize, StartPage);
}
dao层:
@Override
public long getAllQueryNum() {
String hql = "select count(*) as num from InstruInfo";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
Long num = (Long)query.uniqueResult();
return num.longValue();
}
@Override
public long getAbleNum() {
String hql = "from InstruInfo as i where i.instru_borrowstatus = ?";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, "2");
long num = query.list().size();
return num;
}
@Override
public PageView getQueryForPage(int PageSize, int StartPage) {
String hql = "from InstruInfo";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
PageView pv = new PageView(PageSize,StartPage);
query.setFirstResult(pv.getCurrentpage()*pv.getMaxresult());
query.setMaxResults(pv.getMaxresult());
pv.setRecords(query.list());
pv.setTotalrecord(this.getAllQueryNum());
return pv;
}
pagebean:
package net.zmqc.beans.page;
import java.util.List;
public class PageView<T> {
/** 总页数 **/
private long totalpage = 1;
/** 每页显示记录数 **/
private int maxresult = 6;
/** 当前页 **/
private int currentpage = 1;
/** 总记录数 **/
private long totalrecord;
/** 页码数量 **/
private int pagecode = 3;
/**页面显示数据**/
private List<T> records;
public PageView(int maxresult, int currentpage) {
this.maxresult = maxresult;
this.currentpage = currentpage;
}
public long getTotalpage() {
return totalpage;
}
public void setTotalpage(long totalpage) {
this.totalpage = totalpage;
}
public int getMaxresult() {
return maxresult;
}
public void setMaxresult(int maxresult) {
this.maxresult = maxresult;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public long getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(long totalrecord) {
this.totalrecord = totalrecord;
setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1);
}
public int getPagecode() {
return pagecode;
}
public void setPagecode(int pagecode) {
this.pagecode = pagecode;
}
public List<T> getRecords() {
return records;
}
public void setRecords(List<T> records) {
this.records = records;
}
}
这是采用以前无框架时写分页用的,借鉴下方的衔接后并暂时采用了它。
分页实际是采用
setFirstResult
和
setMaxResults
实现,在mysql中是limit,参看过
点击打开链接
http://www.cnblogs.com/fzx888888/archive/2011/05/15/hibernate_Paging.html
Spring 整合 Hibernate 时候用的 HibernateTemplate 不支持分页,因此需要自己包装一个类进行分页,具体实现如下...使用spring的hibernateTemplate的回调机制扩展hibernateTemplate的功能实现分页
大致截图: