struts2自定义标签+hibernate分页的方法

分页对于数据库的应用来讲是再平常不过了,
不过关于为什么要进行分页,相信各位在搜“struts2自定义标签+hibernate分页的方法”
的时候心里已经明白了,所以我就不多说了,开始上关键代码,求指导:

tld文件描述:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
    <tlibversion>1.0</tlibversion> 
    <jsp-version>1.2</jsp-version> 
    <shortname>mytags</shortname>
    <uri>http://com.fyz/tagslib</uri>
    <info></info>
    <tag> 
<name>pagesproduct</name> 
<tag-class>com.fyz.tag.perpage</tag-class>
<attribute>
    <name>name</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
    <name>url</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
</attribute>
    </tag> 
    <tag>
<name>pagesnew</name> 
<tag-class>com.fyz.tag.perpage_new</tag-class>
<attribute>
    <name>name</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
</attribute>
    </tag>
</taglib>
放在WEB-INF目录下
web.xml描述文件添加:
<jsp-config>
<taglib>
    <taglib-uri>http://com.fyz/tagslib</taglib-uri>
    <taglib-location>/WEB-INF/mypage.tld</taglib-location>
</taglib>
    </jsp-config>

关于struts2的自定义标签来讲,他已经将jstl自定义标签封装起来了
所以在这里,我继承 ComponentTagSupport和Component
代码如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.fyz.tag;

import com.opensymphony.xwork2.util.ValueStack;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

/**
*
* @author RDGFT
*/
public class perpage extends ComponentTagSupport{
    private String name;
    private String url;
    @Override
    public Component getBean(ValueStack vs, HttpServletRequest hsr, HttpServletResponse hsr1) {
return new Pages(vs,hsr);
    }
    //获得参数 
    @Override
    protected void populateParams() { 
        super.populateParams(); 
         
        Pages pages = (Pages)component; 
        pages.setName(name);
pages.setUrl(url);
    } 

    /**
     * @return the name
     */
    public String getName() {
return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
this.name = name;
    }

    /**
     * @return the url
     */
    public String getUrl() {
return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
this.url = url;
    }
   
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.fyz.tag;

import com.fyz.dao.ProductDAO;
import com.fyz.dao.port.ProductPort;
import com.fyz.pojo.Product;
import com.opensymphony.xwork2.util.ValueStack;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Component;

/**
*
* @author RDGFT
*/
public class Pages extends Component {

    private String name;
    private String url;
    private ProductPort product = new ProductDAO();
    //以下的变量都是在分页中要用的的变量
    private boolean isFirst = false;//是否为第一页
    private boolean isLast = false;//是否为最后一页
    private boolean firstislast = false;//只有一页
    private int recordsCount;//记录记录总数
    private int currentPage;//记录当前页面
    private int pagecount;
    private final int recordsInOnePage = 6;//设置每页显示数目
    private HttpServletRequest request;

    /**
     *
     * @param vs
     */
    public Pages(ValueStack vs,HttpServletRequest request) {
super(vs);
this.request=request;
this.recordsCount = product.findAll().size();
pagecount = recordsCount / recordsInOnePage + (recordsCount % recordsInOnePage == 0 ? (0) : (1));
    }

    @Override
    public boolean start(Writer writer) {
boolean result = super.start(writer);
StringBuilder str = new StringBuilder();
StringBuilder str2 = new StringBuilder();
boolean isValid = true;
if (isValid) {
    if (this.name != null && this.name.equals(""));
    this.name = (String) this.getStack().findValue(name);
} else {
    isValid = false;
}

if (isValid) {
    if (request.getParameter("pageNumber") == null)//判断是否是第一页
    {
currentPage = 1;
isFirst = true;
System.out.println("perpage中");

    } else {
currentPage = Integer.parseInt(request.getParameter("pageNumber"))==0?1:Integer.parseInt(request.getParameter("pageNumber"));
    }
    List<Product> list = product.getproductbyfenye(recordsInOnePage, currentPage);

    //考虑最后是否是最后一页,分两种情况
    //如果总记录数能够被每页记录数整除,页码为倒数第二页码即可作为最后一页
    if (recordsCount % recordsInOnePage == 0 && currentPage == recordsCount / recordsInOnePage) {
isLast = true;
if (recordsCount == 6) {
    firstislast = true;
}


    } //如果总记录数不能够被每页记录数整除,页码为倒数第一页码才作为最后一页
    else if (recordsCount % recordsInOnePage != 0 && currentPage == recordsCount / recordsInOnePage + 1) {
isLast = true;
if (recordsCount < 6) {
    firstislast = true;
}


    } else {
isLast = false;
    }
    for (Product p : list) {
System.out.println(p.getName());
str.append("<tr>"
+ "<td rowspan='2' align='center' class='altbg2'>"
+ p.getNumber()
+ "</td>"
+ "<td class='altbg2' rowspan='2' align=center'>"
+ p.getName()
+ "</td>"
+ "<td class='altbg2' rowspan='2' align='center'>"
+ p.getBrand()
+ "</td>"
+ "<td class='altbg2' rowspan='2' align='center'>"
+ p.getMode()
+ "</td>"
+ "<td class='altbg2' rowspan='2' align='center'>"
+ p.getPrice()
+ "</td>"
+ "<td class='altbg2' rowspan='2' align='center'>"
+ "<a href='admin/updateProduct.jsp?id=" + p.getId() + "&number=" + p.getNumber() + "&name=" + p.getName() + "&brand=" + p.getBrand() + "&model=" + p.getMode() + "&price=" + p.getPrice() + "&picture=" + p.getPicture() + "&des=" + p.getDes() + "'>修改</a>"
+ "</td>"
+ "<td class='altbg2' rowspan='2' align='center'>"
+ "<a href='deleteproduct.action?id=" + p.getId() + "'>删除</a>"
+ "</td>"
+ "</tr>"
+ "<tr>"
+ "</tr>");
// str.append("<tr><td rowspan='2' align='center' class='altbg2'>").append(p.getNumber()).append("</td><td class='altbg2' rowspan='2' align='center'>").append(p.getName()).append("</td><td class='altbg2' rowspan='2' align='center>").append(p.getBrand()).append("</td><td class='altbg2' rowspan='2' align='center>").append(p.getMode()).append("</td><td class='altbg2' rowspan='2' align='center>").append(p.getPrice()).append("</td><td class='altbg2' rowspan='2' align='center>" + "<a href='updateproduct.action?id=").append(p.getId()).append("'>修改</a></td><td class='altbg2' rowspan='2' align='center'><a href='deleteproduct.action?id=").append(p.getId()).append("'>删除</a>"
// + "</td>"
// + "</tr><tr></tr>");
    }
    str2.append("<font color='red'><center>"
    + "共"+pagecount+"页"
    +"第"+currentPage+"页"
    +"</center></font>");
    if(firstislast){

    }
    if(isLast){
str2.append("<center>"
+ "<a href='admin/manageProduct.jsp?pageNumber=1'>首页</a>"
+ "<a href='admin/manageProduct.jsp?pageNumber=" + (currentPage - 1) + "'>上一页</a>"
+ "<a href='admin/manageProduct.jsp?pageNumber=" + pagecount + "'>最后一页</a>"
+ "</center>");
    }
    if (!isLast) {
str2.append("<center>"
+ "<a href='admin/manageProduct.jsp?pageNumber=1'>首页</a>"
+ "<a href='admin/manageProduct.jsp?pageNumber=" + (currentPage - 1) + "'>上一页</a>"
+ "<a href='admin/manageProduct.jsp?pageNumber=" + (currentPage + 1) + "'>下一页</a>"
+ "<a href='admin/manageProduct.jsp?pageNumber=" + pagecount + "'>最后一页</a>"
+ "</center>");
    }
   
    try {
writer.write(str.toString());
writer.write(str2.toString());
writer.flush();
    } catch (IOException ex) {
Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);
    }

}
return result;
    }

    /**
     * @return the name
     */
    public String getName() {
return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
this.name = name;
    }

    /**
     * @return the url
     */
    public String getUrl() {
return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
this.url = url;
    }
}



hibernate那部分如下:



  @Override
    public List getproductbyfenye(int onepagesize, int currentpage) {
String sql="from Product as p order by p.id";
Query query=super.getSession().createQuery(sql);
query.setFirstResult((currentpage-1)*6);
query.setMaxResults(onepagesize);
return query.list();
    }





页面引用:


<%@ page  contentType = " text/html; charset=gb2312 " pageEncoding="gb2312"%>
<%@taglib prefix="mytags" uri="http://com.fyz/tagslib"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
    <head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>管理人员-管理</title>
<link rel="stylesheet" type="text/css" href="${basePath}css/style_admin.css">
    </head>
    <body>
<table cellspacing="1" cellpadding="4" width="100%"
       class="tableborder" id="table3">
    <form name="form1" action="showUserList.jsp" method="post">
<tr>
    <td colspan="15" class="header">
商品管理
    </td>
</tr>
<tr>
    <td align="center" class="altbg1">
<b>商品编号</b>
    </td>
    <td align="center" class="altbg1">
<b>商品名称</b>
    </td>
    <td align="center" class="altbg1">
<b>商品商标</b>
    </td>
    <td align="center" class="altbg1">
<b>商品型号</b>
    </td>
    <td align="center" class="altbg1">
<b>商品价格</b>
    </td>
    <td align="center" class="altbg1">
<b>修改</b>
    </td>
    <td align="center" class="altbg1">
<b>删除</b>
    </td>
</tr>
<mytags:pagesproduct name="product" url="admin/manageProduct.jsp"></mytags:pagesproduct>
</table>
    </body>
</html>


至此,自定义标签已经开发完成,希望各位走过路过的大神能指点一二





你可能感兴趣的:(html,sql,Hibernate,jsp,struts)