分页
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <script type="text/javascript"> //全选与全不选 function fun1(){ var flag= document.getElementById("oper").checked; var ckArray=document.getElementsByName("ck"); for(var i=0;i<ckArray.length;i++){ ckArray[i].checked = flag; } } //鼠标悬停事件 function fun2(param){ param.style.background='red'; } //鼠标移除 function fun3(param){ param.style.background='white'; } //数据行状态影响标题行状态 function fun4(){ var ckArray=document.getElementsByName("ck"); var checkedNum =0; for(var i=0;i<ckArray.length;i++){ if(ckArray[i].checked == true){ checkedNum++; } } if(checkedNum ==ckArray.length){ document.getElementById("oper").checked = true; }else{ document.getElementById("oper").checked = false; } } // 根据行驶公里数进行升序排列,注册时间进行降序排列 function fun5(param){ var tableObj=document.getElementById("table1"); var tBodyObj=tableObj.tBodies[0]; var rowObj=tBodyObj.rows; var trArray = new Array(); for(var i=0;i<rowObj.length;i++){ trArray[i]=rowObj[i]; } trArray.sort( function compare(tr1,tr2){ var value1 = tr1.cells[param].innerHTML; var value2 = tr2.cells[param].innerHTML; if(param==4){//行驶公里数进行升序排列 alert("value1="+value1+" value2="+value2); if(parseInt(value1)>parseInt(value2)){ //b前a后 return1 return 1; }else if(parseInt(value1)<parseInt(value2)){// a前b后 return -1 return -1; }else { return 0; } }else if(param==5){ //注册时间进行降序排列 2016 > 2014 // value yyyy-MM-dd 2015-01-19 == parseInt(20150119) var data1 = value1.split('-'); var data2 = value2.split('-'); var time1=''; var time2=''; for(var i=0;i<data1.length;i++){ time1+=data1[i]; time2+=data2[i]; } alert("time1="+time1+" time2="+time2); if(parseInt(time1) > parseInt(time2)){ //a前b后 return -1 return -1; }else if(parseInt(time1) < parseInt(time2)){//b前a后 return 1 return 1; }else { return 0; } } } ); var fragment = document.createDocumentFragment(); for(var i=0;i<trArray.length;i++){ fragment.appendChild(trArray[i]); } tableObj.appendChild(fragment); } //首页 function firstPage(){ document.getElementById("pageNo").value=1; } //尾页 function lastPage(){ document.getElementById("pageNo").value=${requestScope.totalPage}; } //上一页 function prePage(){ var currentPage = ${requestScope.pageNo}; currentPage--; document.getElementById("pageNo").value=currentPage; } //下一页 function nextPage(){ var currentPage = ${requestScope.pageNo}; currentPage++; document.getElementById("pageNo").value=currentPage; } //快速定位页数 function selectPage(){ var currentPage =document.getElementById("selPage").value; document.getElementById("pageNo").value=currentPage; document.getElementById("form1").submit(); } //数据行选中状态,决定控制按钮状态 function fun6(){ var ckArray=document.getElementsByName("ck"); var checkedNum =0; for(var i=0;i<ckArray.length;i++){ if(ckArray[i].checked == true){ checkedNum++; } } if(checkedNum ==1){ document.getElementById("delBtn").disabled=false; document.getElementById("upBtn").disabled=false; }else if(checkedNum >1){ document.getElementById("delBtn").disabled=false; document.getElementById("upBtn").disabled=true; }else{ document.getElementById("delBtn").disabled=true; document.getElementById("upBtn").disabled=true; } } </script> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>竞拍管理系统</title> <link href="/carSales/css/admin.css" rel="stylesheet" type="text/css" /> </head> <body > <div id="container"> <div class="top center"> <ul> <li class="admin_logo"><img src="/carSales/images/adminlogo.jpg" width="387" height="60"/></li> <li class="admin_prompt"> <br clear="all" /> <div class="txt_pad"> 用户名:admin | <a href="login.html" class="white">退出登录</a> </div> </li> </ul> </div> <div class="main"> <div id="navigation"> <div id="left-nav"> <div class="top-level"> <a href="javascript:location='carInfoMenu.html'" class="active">车辆管理</a> <a href="javascript:location='auctionMenu.html'">竞拍管理</a> <a href="javascript:location='carBidMenu.html'">举牌竞拍现场</a> <a href="javascript:location='accountMenu.html'">结算管理</a> <a href="javascript:location='memberMenu.html'">会员管理</a> <a href="javascript:location='bacthMenu.html'">接口管理</a> <a href="javascript:location='systemMenu.html'">系统管理</a></div> </div> </div> <div id="content"> <!--start:内容标题--> <div class="top_title"> <div class="left"> <h3><img src="images/local_ico.jpg" width="20" height="20" align="absmiddle" /> <a href="carInfoMenu.html">车辆管理</a> >> 车辆信息查询</h3> </div> <div class="right"> 当前时间:<script language=JavaScript> var enable=0; today=new Date(); var day; var date; var clocktext; var time_start = new Date(); var clock_start = time_start.getTime(); if(today.getDay()==0) day="星期日 " if(today.getDay()==1) day="星期一 " if(today.getDay()==2) day="星期二 " if(today.getDay()==3) day="星期三 " if(today.getDay()==4) day="星期四 " if(today.getDay()==5) day="星期五 " if(today.getDay()==6) day="星期六 " date=(today.getFullYear())+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日 "; sec = today.getSeconds(); hr = today.getHours(); min = today.getMinutes(); if (hr <= 9) hr = "0" + hr; if (min <= 9) min = "0" + min; if (sec <= 9) sec = "0" + sec; clocktext = hr + ":" + min + ":" + sec; document.write(date+day+clocktext); </script> </div> </div> <!--end:内容标题--> <div class="caradmin_title"> <div class="caradmin_left"><h1>车辆信息查询</h1></div> </div> <div class="rightContent center"> <div > <form id="form1" action="/carSales/carInfo/find.action" method="post"> <table border="2"> <tr> <td><input type="submit" value="首页" ${requestScope.pageNo eq 1?"disabled":"" } onclick="firstPage()"/></td> <td><input type="submit" value="上一页" ${requestScope.pageNo eq 1?"disabled":"" } onclick="prePage()"/></td> <td>${requestScope.pageNo }/${requestScope.totalPage }</td> <td><input type="submit" value="下一页" ${requestScope.pageNo eq requestScope.totalPage?"disabled":"" } onclick="nextPage()"/></td> <td><input type="submit" value="尾页" ${requestScope.pageNo eq requestScope.totalPage?"disabled":"" } onclick="lastPage()"/></td> <td><select id="selPage" onchange="selectPage()"> <c:forEach var="i" begin="1" end="${requestScope.totalPage}"> <option value="${i}" ${i eq requestScope.pageNo?"selected":""}>第${i}页</option> </c:forEach> </select></td> <td><input type="submit" id="upBtn" disabled value="更新"/></td> <td><input type="submit" id="delBtn" disabled value="删除"/></td> </tr> </table> <!-- 用于向服务器端传送请求页数的表单域控件 --> <input type="hidden" id="pageNo" name="pageNo"/> </form> </div> <table id="table1" width="100%" cellspacing="1" cellpadding="0" border="0" class="contentTab"> <thead> <tr class="writebg" style="margin:5px 0"> <th align="center" class="bluebg" ><input type="checkbox" id="oper" onclick="fun1();fun6()"/></th> <th align="center" class="bluebg" >车辆ID</th> <th align="center" class="bluebg" >车辆品牌</th> <th align="center" class="bluebg" >起拍价格</th> <th align="center" class="bluebg" onclick="fun5(4)">行驶公里数</th> <th align="center" class="bluebg" onclick="fun5(5)">注册时间</th> <th align="center" class="bluebg" >变速箱类型</th> </tr> </thead> <tbody> <!-- 数据展示行 --> <c:forEach items="${requestScope.listKey}" var="car"> <tr class="writebg" onMouseOver="fun2(this)" onMouseOut="fun3(this)"> <td align="center"><input type="checkbox" name="ck" value="${car.carId}" onclick="fun4();fun6()"/></td> <td align="center">${car.carId}</td> <td align="center">${car.brand}</td> <td align="center">${car.price}元</td> <td align="center">${car.distance}公里</td> <td align="center">${car.registTime}</td> <td align="center">${car.gearBox}</td> </tr> </c:forEach> </tbody> </table> <div class="bid_price"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <td height="40" align="middle"> <input type="button" value=" 返回 " name="submit" class="btn btn_small" onclick="location.href='carInfoSearch.html'"> </td> </tr> </table> </div> </div> </div> </div> </div> </body> </html>
package com.pzjd.controller; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.pzjd.entity.CarInfoVO; public class CarInfoFindServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //0.局部变量 String strPage=null; int startLine =0; String sql="select * from carInfo limit ?,3"; Connection con=null; PreparedStatement ps=null; ResultSet rs=null; List<CarInfoVO> carList = new ArrayList(); String sql_Count="select count(*) from carInfo "; PreparedStatement ps_Count=null; ResultSet rs_Count=null; int totalCount =0; int totalPage=0; //1.获得用户请求页数 strPage=request.getParameter("pageNo"); int pageNo = strPage==null?1:Integer.valueOf(strPage); //2.计算当前页起始行 startLine =(pageNo-1)*3; //3.JDBC查询当前页关联的数据集合 try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3366/bjpowernode", "root", "111"); ps=con.prepareStatement(sql); ps.setInt(1, startLine); rs=ps.executeQuery(); }catch(Exception ex){ ex.printStackTrace(); } //4.Result-------list try{ while(rs.next()){ CarInfoVO car = new CarInfoVO(); car.setCarId(rs.getInt("carId")); car.setBrand(rs.getString("brand")); car.setDistance(rs.getInt("distance")); car.setPrice(rs.getInt("price")); car.setGearBox(rs.getString("gearBox")); car.setRegistTime(rs.getDate("registTime")); carList.add(car); } }catch(Exception ex){ ex.printStackTrace(); } //5.JDBC查询当前表总行数,计算送页数 try{ ps_Count = con.prepareStatement(sql_Count); rs_Count=ps_Count.executeQuery(); rs_Count.next(); totalCount=rs_Count.getInt("count(*)"); totalPage = totalCount%3==0? totalCount/3:totalCount/3+1; }catch(Exception ex){ ex.printStackTrace(); } //6.将[当前页,list,总页数]保存到request request.setAttribute("pageNo", pageNo); request.setAttribute("listKey", carList); request.setAttribute("totalPage", totalPage); //7.请求转发,调用JSP展示数据 request.getRequestDispatcher("/carInfoSearchList.jsp").forward(request, response); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }