用bootstrap开发list分页功能

广告位

运用bootstrap分页控件,可以在bootstrap官网上找到相应分页文档 先贴上分页要用到的工具类Pag…

运用bootstrap分页控件,可以在bootstrap官网上找到相应分页文档
先贴上分页要用到的工具类PageUtil
这个工具类是分页的核心

package com.java1234.util;  /**  * 分页工具类  * @author Administrator  *  */ public class PageUtil {  	/** 	 * 生成分页代码 	 * @param targetUrl 目标地址 	 * @param totalNum 总记录数 	 * @param currentPage 当前页 	 * @param pageSize 每页大小 	 * @return 	 */ 	public static String genPagination(String targetUrl,long totalNum,int currentPage,int pageSize,String param){ 		long totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1; 		if(totalPage==0){ 			return "未查询到数据"; 		}else{ 			StringBuffer pageCode=new StringBuffer(); 			pageCode.append("
  • 首页
  • "
    ); if(currentPage>1){ pageCode.append("
  • 上一页
  • "
    ); }else{ pageCode.append("
  • 上一页
  • "
    ); } for(int i=currentPage-2;i<=currentPage+2;i++){ if(i<1||i>totalPage){ continue; } if(i==currentPage){ pageCode.append("
  • "
  • +i+""); }else{ pageCode.append("
  • "
  • +i+""); } } if(currentPage<totalPage){ pageCode.append("
  • 下一页
  • "
    ); }else{ pageCode.append("
  • 下一页
  • "
    ); } pageCode.append("
  • 尾页
  • "
    ); return pageCode.toString(); } } }

    PageBean分页Model类

    package com.java1234.entity;  /**  * 分页Model类  * @author   *  */ public class PageBean {  	private int page; // 第几页 	private int pageSize; // 每页记录数 	private int start;  // 起始页 	 	 	public PageBean(int page, int pageSize) { 		super(); 		this.page = page; 		this.pageSize = pageSize; 	} 	 	public int getPage() { 		return page; 	} 	public void setPage(int page) { 		this.page = page; 	} 	 	public int getPageSize() { 		return pageSize; 	}  	public void setPageSize(int pageSize) { 		this.pageSize = pageSize; 	}  	public int getStart() { 		return (page-1)*pageSize; 	} 	 	 }  

    这个工具类用到了bootstrap里的分页控件,对其重组拼装,搞成我们想要的样子,里面的class=“disabled” class=”active”都是bootstrap提供的,效果大致如下
    用bootstrap开发list分页功能

    然后进行控制器的开发
    IndexController

    package com.java1234.controller;  import java.util.HashMap; import java.util.List; import java.util.Map;  import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest;  import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView;  import com.java1234.entity.Blog; import com.java1234.entity.PageBean; import com.java1234.service.BlogService; import com.java1234.util.PageUtil; import com.java1234.util.StringUtil;  /**  * 主页Contrller  * @author Administrator  *  */ @Controller @RequestMapping("/") public class IndexController {  	@Resource 	private BlogService blogService; 	 	/** 	 * 请求主页 	 * @return 	 */ 	@RequestMapping("/index") 	public ModelAndView index(@RequestParam(value="page",required=false)String page,@RequestParam(value="typeId",required=false)String typeId,@RequestParam(value="releaseDateStr",required=false)String releaseDateStr,HttpServletRequest request)throws Exception{ 		ModelAndView mav=new ModelAndView(); 		if(StringUtil.isEmpty(page)){ 			page="1"; 		} 		PageBean pageBean=new PageBean(Integer.parseInt(page),10); 		Map<String,Object> map=new HashMap<String,Object>(); 		map.put("start", pageBean.getStart()); 		map.put("size", pageBean.getPageSize()); 		map.put("typeId", typeId); 		map.put("releaseDateStr", releaseDateStr); 		List<Blog> blogList=blogService.list(map); 		for(Blog blog:blogList){ 			List<String> imageList=blog.getImageList(); 			String blogInfo=blog.getContent(); 			Document doc=Jsoup.parse(blogInfo); 			Elements jpgs=doc.select("img[src$=.jpg]"); 			for(int i=0;i<jpgs.size();i++){ 				Element jpg=jpgs.get(i); 				imageList.add(jpg.toString()); 				if(i==2){ 					break; 				} 			} 		} 		mav.addObject("blogList", blogList); 		StringBuffer param=new StringBuffer(); 		if(StringUtil.isNotEmpty(typeId)){ 			param.append("typeId="+typeId+"&"); 		} 		if(StringUtil.isNotEmpty(releaseDateStr)){ 			param.append("releaseDateStr="+releaseDateStr+"&"); 		} 		mav.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/index.html", blogService.getTotal(map), Integer.parseInt(page), 10, param.toString())); 		mav.addObject("pageTitle", "java开源博客系统"); 		mav.addObject("mainPage", "foreground/blog/list.jsp"); 		mav.setViewName("mainTemp"); 		return mav; 	} 	 	/** 	 * 源码下载 	 * @return 	 * @throws Exception 	 */ 	@RequestMapping("/download") 	public ModelAndView download()throws Exception{ 		ModelAndView mav=new ModelAndView(); 		mav.addObject("pageTitle", "本站源码下载页面_java开源博客系统"); 		mav.addObject("mainPage", "foreground/system/download.jsp"); 		mav.setViewName("mainTemp"); 		return mav; 	} }  

    页面显示
    list.jsp

    <div> 	<nav> 	  <ul class="pagination pagination-sm">${pageCode }</ul> 	</nav> </div> 

    用EL表达式取出控制器中的绑定数据的pageCode完成分页

    陈晨数据工程师

    关于作者: 陈晨数据工程师

    为您推荐

    广告位

    发表评论