收藏自:http://www.blogjava.net/zhutianxiang/archive/2009/04/18/263837.html
近期在做的S2SH项目,因为多处用到分页,BOSS要求小弟将其抽象出来。小弟不才,实际参与开发的经验也就1年。
于是花了点时间将其做成自定义标签供所有需要分页的业务调用。小结一下,供新手参考
自定义标签使用如下:
JSP页面引入:
在需要摆放翻页的相关按钮处使用:
以下介绍如何自定义标签:
1.首先是针对自定义标签的描述:
创建WEB-INF/tags/htdz-tag.tld标签描述文件:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
htdz tag
htdz tag
1.0
htdz-tag
/htdz-tag
分页控件
PagerTag
com.htdz.util.tag.PagerTag
JSP
pagesize:每页条数
pagesize
true
true
rowcount:总记录数
rowcount
true
true
currpagenum:当前页数
currpagenum
true
true
action:URL请求路径
action
true
true
className:用于客户端确定分页按钮的样式
className
false
2.创建用于将标签解析为页面翻页控件的类
PagerTag.java
public class PagerTag extends TagSupport {
public static final int USER_PAGESIZE = 5;// 礼品搜索--每页记录数
private static final String DEFAULT_BUTTON_CLASS= "button_small"; //翻页按钮默认样式
private static final String DISABLE_BUTTON_CLASS= "button_small_disable"; //失效按钮默认样式
private int pagesize;
private int rowcount;
private int currpagenum;
private String action;
private String className;
public PagerTag() {
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public void setRowcount(int rowcount) {
this.rowcount = rowcount;
}
public void setCurrpagenum(int currpagenum) {
this.currpagenum = currpagenum;
}
public void setClassName(String className) {
this.className = className;
}
public void setAction(String action) {
this.action = action;
}
public int doStartTag() throws JspException {
if (new Integer(pagesize) == null) {
throw new JspException("PagerTag标签中缺乏pagesize属性!");
}else if(pagesize==0){
throw new JspException("PagerTag标签中的pagesize属性无值!");
}
if (new Integer(rowcount) == null) {
throw new JspException("PagerTag标签中缺乏rowcount属性!");
}
if (new Integer(currpagenum) == null) {
throw new JspException("PagerTag标签中缺乏currpagenum属性!");
}
if (action == null) {
throw new JspException("PagerTag标签中缺乏action属性!");
}else if(action.length()==0){
throw new JspException("PagerTag标签中的action属性无值!");
}
//如果页面标签中没写className属性,则让翻页按钮应用默认的按钮样式
if(className==null||className.length()==0){
className = DEFAULT_BUTTON_CLASS;
}
//获取总页数
int totalpagesize = getTotalpagesize(rowcount);
//用以标志是否能上翻
boolean noUp = false;
//用以标志是否能下翻
boolean noDown = false;
//声明应用于'首页','上一页'按钮的样式(因为此俩按钮要么同时失效,要么同时可用)
String buttonClass1 = className;
//声明应用于'下一页','尾页'按钮的样式(同上)
String buttonClass2 = className;
//如果无记录,则设置总页数与当前页数都为1
if(rowcount==0){
currpagenum = 1;
totalpagesize = 1;
}
//如果当前页是第一页
if(currpagenum==1){
noUp = true;
//设置'首页','上一页'按钮失效样式
buttonClass1 = DISABLE_BUTTON_CLASS;
}
//如果当前页是最大页
if(currpagenum==totalpagesize){
noDown = true;
//设置'下一页','尾页'按钮失效样式
buttonClass2 = DISABLE_BUTTON_CLASS;
}
try {
StringBuffer html = new StringBuffer();
html.append(currpagenum+"/"+totalpagesize+"页");
html.append("
if(noUp){
html.append("disabled=\"true\"");
}
html.append("/>");
html.append("
if(noUp){
html.append("disabled=\"true\"");
}
html.append("/>");
html.append("
if(noDown){
html.append("disabled=\"true\"");
}
html.append("/>");
html.append("
if(noDown){
html.append("disabled=\"true\"");
}
html.append("/>");
html.append(currpagenum+"/"+totalpagesize+"页 ");
html.append("页");
html.append("");
pageContext.getOut().println(html.toString());
} catch (Exception e) {
throw new JspException(e.getMessage());
}
return this.SKIP_BODY;
}
/**
* 根据总记录数得到总页数
*
* @param rowcount
* 总记录数
* @return 总页数
*/
public int getTotalpagesize(int rowcount) {
int totalpagesize = 0;
if (rowcount % pagesize == 0) {
totalpagesize = rowcount / pagesize;
} else {
totalpagesize = rowcount / pagesize + 1;
}
return totalpagesize;
}
}
到此为止,自定义标签书已完成。
可应用于项目各处,
只要页面上遵循标签描述规则,后台该给标签属性传值的时候记得传就行了。
以下用一个简单的例子来说明一下,红色字体显示的部分别忘记写就行了。
UserAction.java:
public class UserAction extends ActionSupport {
private UserService userService;
private List users;
public String findUser(){
String str = null;
HttpServletRequest request = ServletActionContext.getRequest();
Map sessionMap = ActionContext.getContext().getSession();
String currpagenum= "1";
try {
String pagenum = request.getParameter("pagenum ");
if(pagenum != null && pagenum .length()!=0){
currpagenum= pagenum ;
}
} catch (Exception e) {
}
//查询用户记录
users= userService.findUser(pageNum);
if(users.size!=0){
request.setAttribute("users", users);
int rowcount = userService.getCount();
request.setAttribute("rowcount ",rowcount );
request.setAttribute("currpagenum",currpagenum);
str = "success";//成功视图
}else{
message = "无记录!"
str = "failure";//失败视图
}
request.setAttribute("pagesize", PagerTag.USER_PAGESIZE);
request.setAttribute("action", "findUser.action);
//返回视图
return str;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public List getUsers(){
return users;
}
public void setUsers(List users){
this.users = users;
}
}
UserService.java:
public class UserService {
private UserDao userDao;
public List findUser(String pageNum){
List userList = userDao.findUser(pageNum);
return userList;
}
public int getCount(){
int count = userDao.getCount();
return count;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
UserDao.java:
public class UserDao extends HibernateDaoSupport {
/**
* 查询用户
* @return User对象集合
*/
public List findUser(String pagenum) {
List users = null;
Session session = null;
try {
int myPagenum= Integer.parseInt(pagenum);
String hql = "from User";
session = this.getSession();
Query query = session.createQuery(hql);
query.setFirstResult(Pager.USER_PAGESIZE * (myPagenum - 1));
query.setMaxResults(Pager.USER_PAGESIZE);
users = query.list();
session.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
return users;
}
/**
* 获取用户总记录数
* @return 用户总记录数
*/
public int getCount(){
String hql ="select count(id) from User";
Session session = null;
int count =0;
try {
session = this.getSession();
Query query = session.createQuery(hql);
List list = query.list();
session.flush();
count = Integer.parseInt(list.get(0).toString());
} catch (Exception e) {
e.printStackTrace();
} finally{
session.close();
}
return count;
}
}