jsp 数据分页导出下载 显示进度条


1、思路

分页数据查询,同时缓存设置进度(为当前取出数据点总数据比例)

2、界面进度条 为 异步请求缓存比例


3、代码

css:

body{
    position:relative;
}

.mask{
    position:absolute;
    left:0px;
    top:0px;
    height:100%;
    width:100%;
    background-color: #eee;
    display:none;
    filter:alpha(opacity=50); /*IE滤镜,透明度50%*/
    -moz-opacity:0.5; /*Firefox私有,透明度50%*/
    opacity:0.9;/*其他,透明度50%*/
    z-index:999;
}
.out{
    margin:auto;
    margin-top:20%;
    text-align:center;
    height:30px;
    width:500px;
    background-color: #fff;
    border:1px solid red;
    position:relative;
}
.in{
    position:absolute;
    left:-1px;
    top:0px;
    height:28px;
    width:0px;
    background-color: red;
}
.num{
    position:absolute;
    left:0px;
    top:0px;
    height:30px;
    line-height:30px;
    width:500px;
    text-align:center;
    position:relative;
}

js:

$(function(){

          $(.donwload).click(function(){

                 var ulr = ----;

       var key = new Date().getTime();
        url+="?key="+key;

        downloadFile(url);
        process(key);

    });
});

function downloadFile(url) {
    try{
        var elemIF = document.createElement("iframe")
        elemIF.style.display = "none";
        document.body.appendChild(elemIF);
        elemIF.src = url;
    }catch(e){

    }
}

function process(key){
    $.ajax({
        type: 'GET',
        url: ----------,
        async:true,
        success:function (data) {
            if(data){
                data = parseFloat(data).toFixed(2);
                $(".in").css("width",(data/100*500)+"px");
                $(".num").text(data+"%");
                if(data >= 100){
                    setTimeout(function(){
                        $(".mask").hide();
                    }, 3000);
                }else{
                    setTimeout(function(){
                        process(key);
                    }, 1000);
                }
            }
        },
        error:function () {
            
        }
    });
}



downaction 

try{
redisTemplate.opsForValue().set("down_process_"+qm.getKey(),0.1);
int i = 0;
while(true){
    qm.getPage().setNowPage(i++);
    qm.getPage().setPageSize(100);
    Page dbpage = 取当前页数据
    List<> list = (List<>) dbpage.getResult();
    if(list != null && list.size() > 0){
        if(list.size() < 100) {
            redisTemplate.opsForValue().set("down_process_"+qm.getKey(), 100);
        }else{
            double process = i * 100.0 / dbpage.getTotalCount() * 100;
            if(process < 0.1)process=0.1d;
            redisTemplate.opsForValue().set("down_process_"+qm.getKey(), process);
        }
    }
    logger.info("-------value:" + redisTemplate.opsForValue().get("down_process_"+qm.getKey()));
    if(list == null || list.size() < 100) {
        redisTemplate.opsForValue().set("down_process_"+qm.getKey(),100);
        break;
    }
    logger.info("-------value:" + redisTemplate.opsForValue().get("down_process_"+qm.getKey()));
    continue;
}
}catch(){

}finally{
   redisTemplate.opsForValue().set("down_process_"+qm.getKey(),100);
   redisTemplate.expire("down_process_"+qm.getKey(),5, TimeUnit.MINUTES);
}

public double process(HttpServletRequest request, HttpServletResponse response,@PathVariable(value ="key") String key) throws IOException {
    Double process = 0.1d;
    try{
        int i = 0;
        while(true) {
            if(i>=2){
                process = 100d;
                break;
            }
            i++;
            if (redisTemplate.opsForValue().get("down_process_"+key) == null) {
                Thread.currentThread().sleep(1000l);
                continue;
            }else{
                process =  Double.valueOf(redisTemplate.opsForValue().get("down_process_"+key).toString());
                break;
            }
        }
    }catch (Exception e){
         process = 0.1d;
         e.printStackTrace();
    }finally {
        return process;
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSP实现分页显示数据的步骤如下: 1. 在JSP页面使用JSTL标签库的<c:forEach>标签或者JSP自带的<% %>标签,遍历数据集合并将数据显示在页面上。 2. 在页面上添加分页导航条,用于用户切换不同页面的数据。可以通过<a>标签或者<button>标签来实现。 3. 在后台代码,根据用户请求的当前页码以及每页显示数据量,计算出要显示数据的起始位置和结束位置。 4. 从数据查询数据,并根据起始位置和结束位置来限制查询结果的数量。 5. 将查询结果封装成JavaBean对象,然后将JavaBean对象放入请求作用域(request),以便在JSP页面遍历和显示。 6. 在JSP页面根据分页导航条传递的参数,重新查询数据显示在页面上。 示例代码如下: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSP分页显示数据</title> </head> <body> <h1>用户列表</h1> <table> <tr> <th>ID</th> <th>用户名</th> <th>邮箱</th> </tr> <c:forEach var="user" items="${users}"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.email}</td> </tr> </c:forEach> </table> <br> <div> <c:if test="${currentPage > 1}"> <a href="list.jsp?page=${currentPage - 1}">上一页</a> </c:if> <c:forEach begin="1" end="${totalPage}" varStatus="status"> <c:choose> <c:when test="${status.index == currentPage}"> <span>${status.index}</span> </c:when> <c:otherwise> <a href="list.jsp?page=${status.index}">${status.index}</a> </c:otherwise> </c:choose> </c:forEach> <c:if test="${currentPage < totalPage}"> <a href="list.jsp?page=${currentPage + 1}">下一页</a> </c:if> </div> </body> </html> ``` 在后台代码,我们需要根据参数page和pageSize来计算出要查询的数据的起始位置和结束位置,然后从数据查询数据,并将查询结果封装成JavaBean对象放入请求作用域。 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="com.example.User" %> <% int currentPage = Integer.parseInt(request.getParameter("page")); int pageSize = 10; int totalCount = 100; int totalPage = (totalCount + pageSize - 1) / pageSize; int startIndex = (currentPage - 1) * pageSize; int endIndex = startIndex + pageSize; List<User> users = new ArrayList<User>(); for (int i = startIndex; i < endIndex && i < totalCount; i++) { User user = new User(); user.setId(i + 1); user.setUsername("user" + (i + 1)); user.setEmail("user" + (i + 1) + "@example.com"); users.add(user); } request.setAttribute("users", users); request.setAttribute("currentPage", currentPage); request.setAttribute("totalPage", totalPage); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSP分页显示数据</title> </head> <body> <%@ include file="list.jsp" %> </body> </html> ``` 在JSP页面,我们需要根据请求作用域数据来渲染分页导航条和数据列表。 注意:为了防止SQL注入等安全问题,我们在实际开发应该使用PreparedStatement来查询数据,并对用户输入的参数进行过滤和验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值