jsp分页查询中 加入一段代码实现输入页码跳转
pageSize,每页记录条数
currentPage,当前页码
totalPage 总页数
求具体代码 展开
希望能帮到你,加油
Java Bean:
public class WebPage {
private int currentPage;//当前页码
private int totalPages;//总页数
private int pageShow=10;//每页显示数量,可以自己设置20,30,50
private int totalRecords;//总记录条数
......自动生成set,get
}
servlet中方法://相信你能看得明白吧,这里面是需要修改成你的
private void toList(HttpServletRequest request, HttpServletResponse response) {
String key=request.getParameter("ukey");
User u=new User();
WebPage wp=new WebPage();
u.setName(key);
request.setAttribute("ukey", key);
int currentPage=1;
if(request.getParameter("currentPage")!=null){
currentPage=Integer.parseInt(request.getParameter("currentPage"));
}
try {
int count=new UserDaoProxy().getCount(u);
if(count==0){
currentPage=0;
}
wp.setTotalRecords(count);
int totalPages=count%wp.getPageShow()==0?count/wp.getPageShow():count/wp.getPageShow()+1;
wp.setTotalPages(totalPages);
wp.setCurrentPage(currentPage);
int start=(currentPage-1)*wp.getPageShow();
int end=start+wp.getPageShow();
request.setAttribute("wp", wp);
//获取列表
List<User> list=new UserDaoProxy().getList(u, start, end);
//列表传到页面
request.setAttribute("ulist", list);
//跳转页面
request.getRequestDispatcher("/views/list.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
Jsp中,JQuery:
//跳转页数判断
$().ready(function(){
$("#goId").keyup(function(){
var goValue=$(this).val();
var pages=${wp.totalPages};
if(goValue>0&&goValue<=pages){
$("#goBut").prop("disabled","");
}else{
$("#goBut").prop("disabled","disabled");
}
});
}
//页数跳转
function goPage(page){
var ukey=$("input[name=ukey]").val();
window.location="${ctx}/user?flag=toList¤tPage="+page+"&ukey="+ukey;//双引号中改成自己的url
}
Jsp页面:
<div style="position:absolute;left:25%;top:80%">
<input style="background-color:#66FF66 ;"type="button" value="首页" onclick="goPage(1)"
<c:if test="${wp.currentPage <=1 }">disabled</c:if>
/>
<input style="background-color:#00FFFF ;" type="button" value="上一页" onclick="goPage(${wp.currentPage-1})"
<c:if test="${wp.currentPage <=1 }">disabled</c:if>
/>
<input id="goId" type="text" value="${wp.currentPage}" style="width:30px;" />
<input id="goBut" style="background-color:#0000FF ;" type="button" value="GO" onclick="goPage(goId.value)"/>
<input style="background-color:#00FFFF ;" type="button" value="下一页" onclick="goPage(${wp.currentPage+1})"
<c:if test="${wp.currentPage >=wp.totalPages}">disabled</c:if>
/>
<input style="background-color:#FF1493 ;" type="button" value="尾页" onclick="goPage(${wp.totalPages})"
<c:if test="${wp.currentPage >=wp.totalPages}">disabled</c:if>
/>
</div>
<div style="position:absolute;left:15%;top:85%">
共 <span style="color:red;font-weight: bold">${wp.totalRecords}</span> 条记录,
每页显示 <span style="color:red;font-weight: bold">${wp.pageShow}</span> 条,
共 <span style="color:red;font-weight: bold">${wp.totalPages}</span> 页 ,
当前显示第 <span style="color:red;font-weight: bold">${wp.currentPage}</span> 页,
第 <span style="color:red;font-weight: bold">
<c:if test="${wp.totalRecords!=0}">${(wp.currentPage - 1)*wp.pageShow + 1 }</c:if>
<c:if test="${wp.totalRecords==0}">0</c:if>
</span> →
<span style="color:red;font-weight: bold">
<c:if test="${wp.currentPage==wp.totalPages}">${wp.totalRecords}</c:if>
<c:if test="${wp.currentPage!=wp.totalPages}">${wp.currentPage*wp.pageShow}</c:if>
</span>条记录
</div>
望采纳!
上面有页数跳转的Jquery方法,里边路径有个currentPage,这个currentPage可以在servlet中获取jsp当前页数,仔细看看,上边有的