js+bootstrap实现分页、页码跳转

制作page.jsp,在其他页码引入,只需把最外层的form标签的id设置为myForm;

其中  totalPages:共有多少页;totalElements:共有有多少条记录;currentPage:第几页


<%@ 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>
<link href="${pageContext.request.contextPath}/。。。/bootstrap.css" rel="stylesheet" type="text/css">
<script src="${pageContext.request.contextPath}/。。。/jquery/1.11.1/jquery.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/。。。/bootstrap.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>分页页面</title>
<style type="text/css">
/*翻页*/
.jump{
    margin:0px 0;
    float: right;
    }    
.jump_text{
    float:right;
    margin:0 0 0 5px;
    line-height:33px;
    }
.jump_text input{
    width:40px;
    border:rgba(212,212,212,1.00) 1px solid;
    height:30px;
    line-height:33px;
    background:#fff;}
</style>
<script type="text/javascript">

/*
 * 引用此页面,只需在外面
 */

function goPage(){
    var jumpPage = document.getElementById("jumpPage").value;
    var totalPage = '${totalPages}';
    if(isNaN(jumpPage)){
        alert("请输入数字!");
        return;
    }else if(jumpPage.length==0){
        alert("请输入页码!");
    }else if(jumpPage<=0 || Number(jumpPage)>Number(totalPage)){
        alert("非法的页码【"+jumpPage+"】!");
        document.getElementById("jumpPage").value="";
        return;
    }else{
        var flag = $("input[name='pageNumber']");
        flag.remove();
        $("#myForm").append("<input type='hidden' name='currentPage' value='"+jumpPage+"' />");
        $("#myForm").submit();
    }
} 
function pageTo(pageNumber){
    var jumpPage=1;
    if(pageNumber==-1){
        var curpage='${pageNumber}';
        jumpPage=Number(curpage)-1;
    }else if(pageNumber==-2){
        var curpage='${pageNumber}';
        jumpPage=Number(curpage)+1;
    }else{
        jumpPage=Number(pageNumber);
    }
    var flag = $("input[name='pageNumber']");
    flag.remove();
    $("#myForm").append("<input type='hidden' name='currentPage' value='"+jumpPage+"' />");
    $("#myForm").submit();
}
</script>
</head>
<body>
<!--  分页页码     -->
                    <div style="height: 400px;">
                    
                    </div>
                    <hr>
                    <nav>
                        <ul class="pagination">
                        
                            <!-- 上一页  -->
                            <!-- 当当前页码为1时,隐藏上一页按钮  -->
                            <li <c:if test="${currentPage==1 }">class="disabled"</c:if>>
                            <!-- 当当前页码不等于1时,跳转到上一页  -->
                                <a 
                                <c:if test="${currentPage==1 }">href="javaScript:void(0)"</c:if>
                                <c:if test="${currentPage!=1 }">href="javaScript:pageTo('${currentPage-1 }')"</c:if>
                                >上一页</a>
                            </li>
                            
                            <!-- 页码  -->
                            <!-- 当总页数小于等于7时,显示页码1...7页 -->
                            <c:if test="${totalPages<=7}">
                                <c:forEach begin="1" end="${totalPages}" var="i">
                                    <li <c:if test="${currentPage==i }">class="active"</c:if>>
                                        <a
                                        href="javaScript:pageTo('${i}')">${i}</a>
                                    </li>
                                </c:forEach>
                            </c:if>
                            <!-- 当总页数大于7时 -->
                            <c:if test="${totalPages>7}">
                                <!-- 当前页数小于等于4时,显示1到5...最后一页 -->
                                <c:if test="${currentPage<=4}">
                                    <c:forEach begin="1" end="5" var="i">
                                        <li <c:if test="${currentPage==i }">class="active"</c:if>>
                                            <a
                                            href="javaScript:pageTo('${i}')">${i}</a>
                                        </li>
                                    </c:forEach>
                                    <li><a href="#">...</a></li>
                                    <li
                                        <c:if test="${currentPage==totalPages }">class="active"</c:if>>
                                        <a
                                        href="javaScript:pageTo('${totalPages}')">${totalPages}</a>
                                    </li>
                                </c:if>
                                <!-- 当前页数大于4时,如果当前页小于总页码书-3,则显示1...n-1,n,n+1...最后一页 -->
                                <c:if test="${currentPage>4}">
                                    <c:if test="${currentPage<totalPages-3}">
                                        <li><a
                                            href="javaScript:pageTo('${1}')">${1}</a>
                                        </li>
                                        <li><a href="#">...</a></li>
                                        <c:forEach begin="${currentPage-1 }" end="${currentPage+1 }"
                                            var="i">
                                            <li <c:if test="${currentPage==i }">class="active"</c:if>>
                                                <a
                                                href="javaScript:pageTo('${i}')">${i}</a>
                                            </li>
                                        </c:forEach>
                                        <li><a href="#">...</a></li>
                                        <li
                                            <c:if test="${currentPage==totalPages }">class="active"</c:if>>
                                            <a
                                            href="javaScript:pageTo('${totalPages}')">${totalPages}</a>
                                        </li>
                                    </c:if>
                                </c:if>
                                <!-- 当前页数大于4时,如果当前页大于总页码书-4,则显示1...最后一页-3,最后一页-2,最后一页-1,最后一页 -->
                                <c:if test="${currentPage>totalPages-4}">
                                    <li><a
                                        href="javaScript:pageTo('${1}')">${1}</a>
                                    </li>

                                    <li><a href="#">...</a></li>
                                    <c:forEach begin="${totalPages-3 }"
                                        end="${totalPages }" var="i">
                                        <li <c:if test="${currentPage==i }">class="active"</c:if>>
                                            <a
                                            href="javaScript:pageTo('${i}')">${i}</a>
                                        </li>
                                    </c:forEach>
                                </c:if>
                            </c:if>
                            <!-- 下一页  -->
                            <!-- 当当前页码为最后一页或者最后一页为0时,隐藏下一页按钮
                                               当当前页码不等于总页码时,跳转下一页  -->
                            <li
                                <c:if test="${currentPage==totalPages || totalPages==0}">class="disabled"</c:if>>
                                <a
                                <c:if test="${currentPage==totalPages || totalPages==0 }">href="javaScript:void(0)"</c:if>
                                <c:if test="${currentPage!=totalPages }">href="javaScript:pageTo('${currentPage+1 }')"</c:if>>下一页</a>
                            </li>
                        </ul>
                        
                        <!-- 跳转页 -->
                        <div class="jump">
                            <span class="jump_text">共有${totalPages }页,${totalElements }条记录,跳到
                                <input type="text" name="jumpPage"
                                id="jumpPage"
                                οnkeyup="this.value=this.value.replace(/[^0-9-]+/,'');">页
                                <button type="button" class="btn btn-primary btn-xs"
                                    οnclick="goPage()">GO</button>
                            </span>
                        </div>
                    </nav>

            <div style="clear: both;"></div>
       <hr>
       totalPages:共有多少页;totalElements:共有有多少条记录;currentPage:第几页
</body>
</html>

展示:


package com.org.controller; import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.org.BaseController; import com.org.model.User; import com.org.service.IUserService; /** * @Author:liangjilong * @Date:2014-2-25 * @Version:1.0 * @Description: */ @Controller public class UserController extends BaseController { @Resource private IUserService userService; /*** * 方法一请求使用String * * 请求@RequestMapping匹配的URL request * * @param response * @return * @throws Exception */ @RequestMapping(value = "/userList1.do") public String userList1(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); } // userList指跳转到userList.jsp页面 return "userList"; } /** * 方法二请求使用ModelAndView * * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = "/userList2.do") public ModelAndView userList2(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); List<User> lists = userService.getUserList(); if (lists != null) { request.setAttribute("userList", lists); } // userList指跳转到userList.jsp页面 return new ModelAndView("userList"); } /*** * 自定义标签实现分页 * * @param request * @param response * @param @ResponseBody ajax响应 * @param method={RequestMethod.POST,RequestMethod.GET}表单请求 * @param consumes="application/json; charset=UTF-8"请求格式是json * @return * @throws UnsupportedEncodingException * @throws Exception */ @RequestMapping(value = "/pageList.do" ,method={RequestMethod.POST,RequestMethod.GET}) public @ResponseBody ModelAndView getUserInfo(Model model, @RequestParam(required = false) String username, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) { try { String userName = new String(username.getBytes("ISO-8859-1"),"UTF-8");//处理乱码 Map<String, Object> map = new HashMap<String, Object>(); username=(username==null)?"":username; map.put("username", username);//username必须要和ibatis配置的property=username一致 Integer totalCount = this.userService.getUserCount(map); this.initPage(map, pageNum, pageSize, totalCount); List list = this.userService.getUserLists(map); this.initResult(model, list, map); return new ModelAndView("pagerList"); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 添加用户 * @param type * @param model * @return */ @RequestMapping("/addUser.do") public ModelAndView addUser(@RequestParam String username, Model model) { User user = new User(); user.setUsername(username); this.userService.addUser(user); return this.getUserInfo(model, null, null, null); } /*** * 删除用户 * @param id * @param pageNum * @param pageSize * @param model * @return */ @RequestMapping(value="/delUser.do",method={RequestMethod.POST,RequestMethod.GET},consumes="application/json; charset=UTF-8") @ResponseBody public ModelAndView delUser(@RequestParam(required = true) Integer id, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, Model model, HttpServletRequest request,HttpServletResponse response) { PrintWriter out=null; JSONObject result=new JSONObject(); try { out=response.getWriter(); this.userService.delUserById(id); result.put("flag", true); out.write(result.toString()); } catch (Exception e) { try { result.put("flag", false); out.write(result.toString()); } catch (JSONException e1) { e1.printStackTrace(); } } return null; //return this.getUserInfo(model, null, pageNum, pageSize); } /*** * 编辑用户 * @param id * @param model * @return */ @RequestMapping("/getUserById.do") public ModelAndView getUserById(@RequestParam(required = true) Integer id, Model model) { User u = this.userService.getUserById(id); model.addAttribute("user", u); return new ModelAndView("editUser"); } /*** * 编辑用户 * @param id * @param model * @return */ @RequestMapping("/editUser.do") public ModelAndView editUser(@RequestParam(required = true) Integer id, @RequestParam String username, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, Model model) { User u = new User(); u.setUsername(username); this.userService.editUser(u); return this.getUserInfo(model, null, pageNum, pageNum); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值