自界说分页标签
副标题#e#
近期在做的S2SH项目,因为多处用到分页,BOSS要求小弟将其抽象出来。小弟鄙人,实际参加开拓的履历也就1年。
于是花了点时间将其做成自界说标签供所有需要分页的业务挪用。小结一下,供新手参考
自界说标签利用如下:
JSP页面引入:
<%@ taglib uri="/htdz-tag" prefix="htdz-tag"%>
在需要摆放翻页的相关按钮处利用:
<htdz-tag:PagerTag pagesize="${pagesize}" rowcount="${rowcount}" currpagenum="${currpagenum}" action="${action}"/>
以下先容如何自界说标签:
1.首先是针对自界说标签的描写:
建设WEB-INF/tags/htdz-tag.tld标签描写文件:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
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">
<description>htdz tag</description>
<display-name>htdz tag</display-name>
<tlib-version>1.0</tlib-version>
<short-name>htdz-tag</short-name>
<uri>/htdz-tag</uri>
<tag>
<!--分页控件利用说明:
1.最全参数用法:<htdz-tag:PagerTag pagesize="${pagesize}" rowcount="${rowcount}" currpagenum="${currpagenum}" action="${action}" className="button_small"/>
1.最简参数用法:<htdz-tag:PagerTag pagesize="${pagesize}" rowcount="${rowcount}" currpagenum="${currpagenum}" action="${action}"/>
参数说明:
1. pagesize为每页记录数(必写)
2. rowcount为总记录数(必写)
3. currpagenum为当前页数(必写)
4. className为分页按钮样式,假如不写,则为默认样式
5. action为URL请求路径(必写)
-->
<description>分页控件</description>
<!--JSP里利用时的标签名-->
<name>PagerTag</name>
<!--用以将自界说标签理会成控件的Java类-->
<tag-class>com.htdz.util.tag.PagerTag</tag-class>
<body-content>JSP</body-content>
<!--每个attribute代表标签的一个属性-->
<attribute>
<description>pagesize:每页条数</description>
<!--属性名-->
<name>pagesize</name>
<!--是否必填属性-->
<required>true</required>
<!--此属性值是否接管EL,<%= 之类的形式-->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>rowcount:总记录数</description>
<name>rowcount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>currpagenum:当前页数</description>
<name>currpagenum</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>action:URL请求路径</description>
<name>action</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>className:用于客户端确定分页按钮的样式</description>
<name>className</name>
<required>false</required>
</attribute>
</tag>
</taglib>
#p#副标题#e#
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("<input class="+buttonClass1+" type=\"button\" value=\"首页\" onclick=\"turnPage('first','"+currpagenum+"','"+totalpagesize+"','"+action+"')\" "); if(noUp){ html.append("disabled=\"true\""); } html.append("/>"); html.append("<input class="+buttonClass1+" type=\"button\" value=\"上一页\" onclick=\"turnPage('up','"+currpagenum+"','"+totalpagesize+"','"+action+"')\" "); if(noUp){ html.append("disabled=\"true\""); } html.append("/>"); html.append("<input class="+buttonClass2+" type=\"button\" value=\"下一页\" onclick=\"turnPage('down','"+currpagenum+"','"+totalpagesize+"','"+action+"')\""); if(noDown){ html.append("disabled=\"true\""); } html.append("/>"); html.append("<input class="+buttonClass2+" type=\"button\" value=\"尾页\" onclick=\"turnPage('last','"+currpagenum+"','"+totalpagesize+"','"+action+"')\" "); if(noDown){ html.append("disabled=\"true\""); } html.append("/>"); html.append(currpagenum+"/"+totalpagesize+"页 "); html.append("<input type=\"text\" maxlength=\"3\" id=\"text\" size=\"3\" onkeypress=\"return checkInput(event);\" />页"); html.append("<input class="+className+" type=\"button\" value=\"GO\" onclick=\"turnPage('to','"+currpagenum+"','"+totalpagesize+"','"+action+"')\" />"); 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:
#p#分页标题#e#
public class UserAction extends ActionSupport {
private UserService userService;
private List<User> 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<User> getUsers(){
return users;
}
public void setUsers(List<User> users){
this.users = users;
}
}
UserService.java:
#p#分页标题#e#
public class UserService {
private UserDao userDao;
public List<User> findUser(String pageNum){
List<User> 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<User> findUser(String pagenum) {
List<User> 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;
}
}