当前位置:文档之家› 自定义分页标签

自定义分页标签

自定义标签使用如下:
JSP页面引入:
<%@ taglib uri="/htdz-tag" prefix="htdz-tag"%>
在需要摆放翻页的相关按钮处使用:


以下介绍如何自定义标签:

1.首先是针对自定义标签的描述:
创建WEB-INF/tags/htdz-tag.tld标签描述文件:

xmlns:xsi="https://www.doczj.com/doc/b92252054.html,/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.doczj.com/doc/b92252054.html,/xml/ns/j2ee https://www.doczj.com/doc/b92252054.html,/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

ibute>

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;
totalpag

esize = 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", https://www.doczj.com/doc/b92252054.html,ER_PAGESIZE);
request.setAttribute("action", "findUser.action);

//返回视图
return str;
}


public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
https://www.doczj.com/doc/b92252054.html,erService = userService;
}

public List getUsers(){
return users;
}
public void setUsers(List users){
https://www.doczj.com/doc/b92252054.html,ers = 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) {
https://www.doczj.com/doc/b92252054.html,erDao = 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(https://www.doczj.com/doc/b92252054.html,ER_PAGESIZE * (myPagenum - 1));
query.setMaxResults(https://www.doczj.com/doc/b92252054.html,ER_PAGESIZE);
users = query.list();
session.flush();
} catch (Exception e) {
e.printStack

Trace();
} 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;
}

}


posted on 2009-04-04 00:23 §朱家二少§ 阅读(2241) 评论(5) 编辑 收藏 所属分类: J2SE


Feedback
# re: 自定义分页标签 2009-04-04 18:40 popoer
为什么要重新发明轮子呢,我觉得DisplayTag很好用! 回复 更多评论

# re: 自定义分页标签 2009-04-04 20:01 独孤行
太过于复杂,一个存储过程,一个自定义标签类,一个控制器,然后配置一下tld,xml文件即可 回复 更多评论

# re: 自定义分页标签 2009-04-18 13:52 Wolftotem
我想问一下,如果是多条件组合查询时,当点击下一页时怎样使url记住查询页面的查询参数? 回复 更多评论

# re: 自定义分页标签 2009-04-19 23:23 §朱家二少§
@Wolftotem
用request.setAttribute("abc",abc);




相关主题
文本预览
相关文档 最新文档