Http请求纯后台简单实现分页并返回JSON格式

1、实体层

package com.eccap.es.model;

import java.util.List;

/**
 * 分页信息
 * 
 * @author chaohui.yan
 * @date 2019-06-10
 *
 */
public class Pagefenye<T> {
    // 你页面需要展示的集合
    private List<T> list;
    // 总页数
    private int totalPage;

    // 当前页
    private int currentPage;
    // 每页显示的数目 (为了防止硬编码可以写在配置文件中)
    private int pageSize;

    public Pagefenye(int totalNum, int currentPage, int pageSize, int pageNo) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        /*this.pageNo = pageNo;*/

        // 计算总页数
        if (totalNum % this.pageSize == 0)
            this.totalPage = totalNum / this.pageSize;
        else
            this.totalPage = (totalNum / this.pageSize) + 1;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

}

2、Dao层select * from 表名 查询

3、service层

    @Autowired
    private EsEntPermitManager esEntPermitManager;
    
    public Pagefenye<T> findAllUsersByPage(int currentPage, int pageSize) {
        List<T> users =“调用查询方法”;//esEntPermitManager.fenye();
        Pagefenye<T> pageInfo = new Pagefenye<>(users.size(), currentPage, pageSize, 5);
        
        //判断是否是最后一页,如果最后一页就显示最后一页的几条,不是最后一页就显示pageSize条
        if(currentPage==pageInfo.getTotalPage())
            pageInfo.setList(users.subList((currentPage-1)*pageSize, users.size()));
        else {
            pageInfo.setList(users.subList((currentPage-1)*pageSize, (currentPage-1)*pageSize+pageSize));
        }
        return pageInfo;
    }

4、controller层

    public void Pagefenye() {
               

      //将浏览器的编码设置为Utf-8
      this.getResponse().setContentType("text/plain; charset=UTF-8");

     //接收当前页
        String pageNo = this.getParameter("pageNo");
                //String 转 int
        int isd = Integer.parseInt(pageNo);
        String stg = null;
                //调用业务层方法并传入当前页,每页展示多少条
        Pagefenye<T> list = pagefenyeManager.findAllUsersByPage(isd,100);
        try {    
                       //获取查询结果转为json并展示空值
            stg = JSON.toJSONString(list,SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
            System.out.println(stg);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     //打印输出文本格式的(包括html标签)页面展示值

      this.getResponse().getWriter().write(stg);
      this.getResponse().getWriter().flush();
      this.getResponse().getWriter().close();

    }        

 

转载于:https://www.cnblogs.com/yanchaohui/p/10996433.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值