( 该部分内容是自写自用代码)
Action部分:
//查看车辆信息(分页)
public ActionForward query(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
List list = carService.queryAll();
int pages = list.size();
System.out.println(pages);
request.getSession().setAttribute("pages", pages);//记录总数放进Session中
String a = request.getParameter("I");
String b = request.getParameter("J");
if(null==a&&null==b) {
request.getSession().setAttribute("v", 1);
request.getSession().setAttribute("v2", 5);
request.getSession().setAttribute("pages", pages);//记录总数放进Session中
int pageCount = carService.getPage(pages, 5);//初始化的时候有多少页
request.getSession().setAttribute("PageCount", pageCount);
List syuser = carService.getApply(1, 5);//初始的时候默认在第1页,第页显示5条
request.getSession().setAttribute("carsMessage", syuser);
return mapping.findForward("query");
}
else{
int i = Integer.parseInt(request.getParameter("I").trim());
int j = Integer.parseInt(request.getParameter("J"));
System.out.println(i+"...."+j);
int pageCount = carService.getPage(pages,j);//初始化的时候有多少页
if(i<=pageCount&&i>0){
request.getSession().setAttribute("PageCount", pageCount);
request.getSession().setAttribute("v", i);
request.getSession().setAttribute("v2", j);
List syuser2 = carService.getApply(i, j);
request.getSession().setAttribute("carsMessage", syuser2);
return mapping.findForward("query");
}
else{
return mapping.findForward("query");
}
}
}
Service部分:
// 车辆查询
public List queryAll() {
return this.getHibernateTemplate().loadAll(SrcmCar.class);
}
// 查询出指定记录数的信息
public List getApply(int i, int j) {
Criteria c = this.getSession().createCriteria(SrcmCar.class);
c.setFirstResult((i - 1) * j);
int p = queryAll().size();
int q = p / j + 1;
if (p % j != 0 && i == q) {
// c.setFetchSize(p - ((i - 1) * j));
c.setMaxResults(p - ((i - 1) * j));
}
else {
// c.setFetchSize(j);
c.setMaxResults(j);
}
List list = c.list();
return list;
}
// 初始化的时候用来判断分页的页数
public int getPage(int i, int j) {
int mod = i % j;
int pageCount;
if (mod == 0) {
pageCount = i / j;
}
else if (i < j) {
pageCount = 1;
}
else {
pageCount = i / j + 1;
}
return pageCount;
}
页面部分:
<tr>
<td colspan="8" align="right">
共有${pages}条记录|总计
<span id="divid">${PageCount}</span>
页|每页显示
<select id="page" onchange="cutPage()">
<c:if test="${v2==5}">
<option selected>5</option>
<option>10</option>
<option>15</option>
</c:if>
<c:if test="${v2==10}">
<option>5</option>
<option selected>10</option>
<option>15</option>
</c:if>
<c:if test="${v2==15}">
<option>5</option>
<option>10</option>
<option selected>15</option>
</c:if>
</select>条
|<a href="<%=basePath%>car.do?method=query&I=1&J=${v2}"">首页</a>
|<a href="<%=basePath%>car.do?method=query&I=${v-1}&J=${v2}">上一页</a>
|<a href="<%=basePath%>car.do?method=query&I=${v+1}&J=${v2}">下一页</a>
|<a href="<%=basePath%>car.do?method=query&I=${PageCount}&J=${v2}">末页</a>
|跳到第<input type="text" id="text1" size="2" value="${v}">页
<input type="button" value="Go" onclick="cutPage()">
</td>
</tr>
javascript部分:
<script type="text/javascript">
var value1,c1,c2;
function cutPage(){
var op = document.getElementById("page");
for(var i =0;i < op.options.length;i++){
if(op.options[i].selected==true){
value1 = op.options[i].text;
}
}
var x = document.getElementById("text1").value;//第几页
var n = value1;//每页显示几条
var m = ${pages};//记录总数
if(n*x-m>n){
alert("没有这么多页!");
document.getElementById("text1").value = ${v};
return;
}
else {
location.href = "<%=basePath%>car.do?method=query&I="+x+"&J="+value1;
}
}
</script>
posted on 2007-10-29 12:23
末日风情 阅读(5434)
评论(3) 编辑 收藏 所属分类:
struts