ssm商城教学8.分页

效果

如图所示,每页显示5条数据。

注: 没有分类图片是正常的,在接下来的知识点就会做增加图片功能了

ssm商城教学8.分页

Page.java

新增Page这个类专门为分页提供必要信息
属性:
int start; 开始位置
int count; 每页显示的数量
int total; 总共有多少条数据
String param; 参数(这个属性在后续有用到,但是分类的分页查询里并没有用到,请忽略)

方法:
getTotalPage 根据 每页显示的数量count以及总共有多少条数据total,计算出总共有多少页
getLast 计算出最后一页的数值是多少
isHasPreviouse 判断是否有前一页
isHasNext 判断是否有后一页

package com.how2java.tmall.util;

 

public class Page {

 

    private int start; //开始页数

    private int count; //每页显示个数

    private int total; //总个数

    private String param; //参数

 

    private static final int defaultCount = 5//默认每页显示5条

 

    public int getStart() {

        return start;

    }

    public void setStart(int start) {

        this.start = start;

    }

    public int getCount() {

        return count;

    }

    public void setCount(int count) {

        this.count = count;

    }

 

    public Page (){

        count = defaultCount;

    }

    public Page(int start, int count) {

        this();

        this.start = start;

        this.count = count;

    }

 

    public boolean isHasPreviouse(){

        if(start==0)

            return false;

        return true;

    }

    public boolean isHasNext(){

        if(start==getLast())

            return false;

        return true;

    }

 

    public int getTotalPage(){

        int totalPage;

        // 假设总数是50,是能够被5整除的,那么就有10页

        if (0 == total % count)

            totalPage = total /count;

            // 假设总数是51,不能够被5整除的,那么就有11页

        else

            totalPage = total / count + 1;

 

        if(0==totalPage)

            totalPage = 1;

        return totalPage;

 

    }

 

    public int getLast(){

        int last;

        // 假设总数是50,是能够被5整除的,那么最后一页的开始就是45

        if (0 == total % count)

            last = total - count;

            // 假设总数是51,不能够被5整除的,那么最后一页的开始就是50

        else

            last = total - total % count;

        last = last<0?0:last;

        return last;

    }

 

    @Override

    public String toString() {

        return "Page [start=" + start + ", count=" + count + ", total=" + total + ", getStart()=" + getStart()

                ", getCount()=" + getCount() + ", isHasPreviouse()=" + isHasPreviouse() + ", isHasNext()="

                + isHasNext() + ", getTotalPage()=" + getTotalPage() + ", getLast()=" + getLast() + "]";

    }

    public int getTotal() {

        return total;

    }

    public void setTotal(int total) {

        this.total = total;

    }

    public String getParam() {

        return param;

    }

    public void setParam(String param) {

        this.param = param;

    }

 

}

CategoryMapper.xml

修改CategoryMapper.xml,以提供带分页的查询语句和获取总数的sql语句

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

 

<mapper namespace="com.how2java.tmall.mapper.CategoryMapper">

    <select id="list" resultType="Category">

        select * from   category         order by id desc

        <if test="start!=null and count!=null">

            limit #{start},#{count}

        </if>

 

    </select>

    <select id="total" resultType="int">

        select count(*) from category

    </select>

</mapper>

CategoryMapper

package com.how2java.tmall.mapper;

 

import com.how2java.tmall.util.Page;

import com.how2java.tmall.pojo.Category;

  

import java.util.List;

  

public interface CategoryMapper {

    public List<Category> list(Page page);

 

    public int total();

}

CategoryService

修改CategoryService,提供一个支持分页的查询方法list(Page page)和获取总数的方法total

package com.how2java.tmall.service;

 

import com.how2java.tmall.pojo.Category;

import com.how2java.tmall.util.Page;

import java.util.List;

 

public interface CategoryService{

    int total();

    List<Category> list(Page page);

}

修改CategoryServiceImpl,提供一个支持分页的查询方法list(Page page)和获取总数的方法total。

package com.how2java.tmall.service.impl;

import com.how2java.tmall.util.Page;

import com.how2java.tmall.mapper.CategoryMapper;

import com.how2java.tmall.pojo.Category;

import com.how2java.tmall.service.CategoryService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class CategoryServiceImpl  implements CategoryService {

    @Autowired

    CategoryMapper categoryMapper;

    @Override

    public List<Category> list(Page page) {

        return categoryMapper.list(page);

    }

 

    @Override

    public int total() {

        return categoryMapper.total();

    }

}

CategoryController

修改CategoryController
1. 为方法list增加参数Page用于获取浏览器传递过来的分页信息
2. categoryService.list(page); 获取当前页的分类集合
3. 通过categoryService.total(); 获取分类总数
4. 通过page.setTotal(total); 为分页对象设置总数
5. 把分类集合放在"cs"中
6. 把分页对象放在 "page“ 中
7. 跳转到listCategory.jsp页面

package com.how2java.tmall.controller;

 

import com.how2java.tmall.pojo.Category;

import com.how2java.tmall.service.CategoryService;

import com.how2java.tmall.util.Page;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

 

import java.util.List;

  

@Controller

@RequestMapping("")

public class CategoryController {

    @Autowired

    CategoryService categoryService;

   

    @RequestMapping("admin_category_list")

    public String list(Model model,Page page){

        List<Category> cs= categoryService.list(page);

        int total = categoryService.total();

        page.setTotal(total);

        model.addAttribute("cs", cs);

        model.addAttribute("page", page);

        return "admin/listCategory";

    }

}

listCategory.jsp

本来注释掉的62行,去掉注释

ssm商城教学8.分页

为了便于理解,先来一个简化了的adminPage.jsp

在listCategory.jsp页面包含了分页专用jsp:adminPage.jsp
在其中,依据page对象,进行分页超链元素的显示
完整版的amdinPage 比较复杂,为了便于大家理解,我先把完整版的adminPage.jsp简化一下

首先,分页超链的效果,用的Bootstrap来制作
首页超链:

<li>

<a href="?start=0" aria-label="Previous" >

<span aria-hidden="true">«</span>

</a>

</li>


上一页超链:

<li >

<a href="?start=${page.start-page.count}" aria-label="Previous" >

<span aria-hidden="true">‹</span>

</a>

</li>


下一页超链:

<li >

<a href="?start=${page.start+page.count}" aria-label="Next">

<span aria-hidden="true">›</span>

</a>

</li>


最后一页

<li >

<a href="?start=${page.last}" aria-label="Next">

<span aria-hidden="true">»</span>

</a>

</li>



中间页

<c:forEach begin="0" end="${page.totalPage-1}" varStatus="status">

<li>

<a href="?start=${status.index*page.count}" class="current">${status.count}</a>

</li>

</c:forEach>

完整版的adminPage.jsp

简化版用于帮助大家理解,其存在的问题是,即便是没有下一页的数据了,下一页超链也可以点击,点出来的页面是空白的。(首页,上一页,下一页和最后一页都存在这个问题)

那么所谓的完整版的adminPage.jsp,就是对这些边界进行了处理。当没有下一页的时候,对应超链处于不可点击状态。

比如首页:
当page.hasPreviouse为false的时候,为首页连接套用Bootstrap样式 disabled

<li <c:if test="${!page.hasPreviouse}">class="disabled"</c:if>>

<a href="?page.start=0${page.param}" aria-label="Previous" >

<span aria-hidden="true">&laquo;</span>

</a>

</li>