首先去Maven仓库(http://mvnrepository.com)下载pager-taglib.jar包,它的官网貌似挂掉了.
当然了,说到分页,当然得建立分页bean,如下图所示:
然后建立存放分页请求参数的类SystemContext,该类中用到了ThreadLocal,它是一个跟当前线程相关的Map,它的key就是当前线程的名字
接着配置SystemContextFilter的过滤器,对所有请求都拦截.
1 <filter> 2 <filter-name>SystemContextFilter</filter-name> 3 <filter-class>com.scitc.page.web.SystemContextFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>SystemContextFilter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping>
在SystemContextFilter中对页面请求参数进行处理,放入ThreadLocal中.
在DAO层,进行分页处理
1 package com.scitc.page.dao; 2 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.apache.ibatis.session.SqlSession; 8 9 import com.scitc.page.domain.Pager; 10 import com.scitc.page.domain.User; 11 import com.scitc.page.util.MyBatisUtil; 12 import com.scitc.page.util.SystemContext; 13 14 public class UserDao { 15 public Pager<User> find(String name) { 16 17 Map<String, Object> params = new HashMap<String, Object>(); 18 if (name != null && !name.equals("")) 19 params.put("name", "%" + name + "%"); 20 int pageSize = SystemContext.getPageSize(); 21 int pageOffset = SystemContext.getPageOffset(); 22 String order = SystemContext.getOrder(); 23 String sort = SystemContext.getSort(); 24 Pager<User> pages = new Pager<User>(); 25 SqlSession session = null; 26 try { 27 session = MyBatisUtil.createSession(); 28 params.put("pageSize", pageSize); 29 params.put("pageOffset", pageOffset); 30 params.put("sort", sort); 31 params.put("order", order); 32 List<User> datas = session.selectList(User.class.getName()+ ".find", params); 33 pages.setDatas(datas); 34 pages.setPageOffset(pageOffset); 35 pages.setPageSize(pageSize); 36 int totalRecord = session.selectOne(User.class.getName()+".find_count", params); 37 pages.setTotalRecord(totalRecord); 38 } finally { 39 MyBatisUtil.closeSession(session); 40 } 41 return pages; 42 } 43 }
Mybatis中的SQL配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="com.scitc.page.domain.User"> 7 8 <!-- 注意namespace不是别名,User的别名在mybatis.config.xml中的<typeAliases> --> 9 10 <insert id="add" parameterType="User"> 11 INSERT INTO t_user (username,password,nickname) 12 VALUES(#{username},#{password},#{nickname}) 13 </insert> 14 <delete id="delete" parameterType="int"> 15 delete from t_user where id=#{id} 16 </delete> 17 <update id="update" parameterType="User"> 18 update t_user set username=#{username} where id=#{id} 19 </update> 20 <select id="load" parameterType="int" resultType="User"> 21 select * from t_user where id=#{id} 22 </select> 23 <!-- List的返回结果仍可为User --> 24 <select id="list" resultType="User"> 25 select * from t_user 26 </select> 27 <!-- #{sx}会使用?进行替代 ,$(xx)会完整将字符串完成替代--> 28 <select id="find" resultType="User" parameterType="map"> 29 select * from t_user 30 <!-- 动态SQL --> 31 <where> 32 <if test="name!=null"> (username like #{name} or nickname like #{name})</if> 33 </where> 34 35 <if test="sort!=null"> 36 order by ${sort} 37 <if test="order!=null"> 38 ${order} 39 </if> 40 </if> 41 limit #{pageOffset},#{pageSize} 42 </select> 43 44 <select id="find_count" parameterType="map" resultType="int"> 45 select count(*) from t_user 46 <if test="name!=null">where (username like #{name} or nickname like #{name})</if> 47 </select> 48 </mapper>
为什么我们要使用ThreadLocal呢?
主要是对跨层数据传输的处理,前面也提到了它是一个当前线程Map,所以使得跨层也可以传输数据
好了,pager-taglib暂时说到这里,接下来说说sitemesh
什么是sitemesh呢?
它是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。
首先下载sitemesh的jar包,下载方法可以去官网,也可以去Maven仓库
接下来配置sitemesh的过滤器
1 <filter> 2 <filter-name>sitemesh</filter-name> 3 <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> 4 </filter> 5 6 <filter-mapping> 7 <filter-name>sitemesh</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
模版页面的配置
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title><decorator:title default="欢迎使用xxx系统"/></title> 9 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/main.css"/> 10 <decorator:head/> 11 </head> 12 <body> 13 <a href="">其他操作</a> 14 <hr/> 15 <h3 align="center"><decorator:title default="详细信息"/></h3> 16 <decorator:body/> 17 <hr/> 18 <div align="center" class="distance"> 19 55555555 20 </div> 21 <div align="center" class="footer"> 22 CopyRight@2012-2015<br/> 23 scitc 24 </div> 25 </body> 26 </html>
最后效果如图