您的位置:首页 > 编程语言 > Java开发

struts2、hibernate 分页(自定义标签)

2011-01-14 14:16 429 查看
class :PagerTag 是用来实现自定义分页标签,继承SimpleTagSupport实现自定义标签

view plaincopy to clipboardprint?

public class PagerTag extends SimpleTagSupport {

private String uri;//分页要执行的action路径
private Integer curpage;//当前页
private Integer pagesize;//每页显示的记录数
private Integer pagecount;//总页数
private Integer rowcount;//总记录数

public Integer getCurpage() {
return curpage;
}
public void setCurpage(Integer curpage) {
this.curpage = curpage;
}
public Integer getPagesize() {
return pagesize;
}
public void setPagesize(Integer pagesize) {
this.pagesize = pagesize;
}
public Integer getPagecount() {
return pagecount;
}
public void setPagecount(Integer pagecount) {
this.pagecount = pagecount;
}
public Integer getRowcount() {
return rowcount;
}
public void setRowcount(Integer rowcount) {
this.rowcount = rowcount;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}

//每次执行标签时会调用toTag
public void doTag() throws JspException, IOException {

//获得页面的输出流
JspWriter out  = this.getJspContext().getOut();

//通过流往页面写数据
out.println("<div class=/"pager/">");
out.println("共" +rowcount+ "行记录,每页");
out.println("<input value=/""+ pagesize + "/" size=/"2/" />条");
out.println("当前第<input value=/"" + (curpage +1 )+"/" size=/"2/"/>页/共"+ pagecount + "页");
out.println("<a href="/" mce_href="/""" + uri+"&curpage=0/">第一页</a>");

if(curpage > 0) {
out.println("<a href="/" mce_href="/""" + uri+"&curpage="+(curpage-1)+"/">上一页</a>");
}
if(curpage < pagecount -1) {
out.println("<a href="/" mce_href="/""" + uri+"&curpage=" + (curpage+1)+"/">下一页</a>");
}
out.println("<a href="/" mce_href="/""" + uri+"&curpage=" +(pagecount-1)+"/">最后一页</a>");
out.println("</div>");
}

}
public class PagerTag extends SimpleTagSupport {

private String uri;//分页要执行的action路径
private Integer curpage;//当前页
private Integer pagesize;//每页显示的记录数
private Integer pagecount;//总页数
private Integer rowcount;//总记录数

public Integer getCurpage() {
return curpage;
}
public void setCurpage(Integer curpage) {
this.curpage = curpage;
}
public Integer getPagesize() {
return pagesize;
}
public void setPagesize(Integer pagesize) {
this.pagesize = pagesize;
}
public Integer getPagecount() {
return pagecount;
}
public void setPagecount(Integer pagecount) {
this.pagecount = pagecount;
}
public Integer getRowcount() {
return rowcount;
}
public void setRowcount(Integer rowcount) {
this.rowcount = rowcount;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}

//每次执行标签时会调用toTag
public void doTag() throws JspException, IOException {

//获得页面的输出流
JspWriter out  = this.getJspContext().getOut();

//通过流往页面写数据
out.println("<div class=/"pager/">");
out.println("共" +rowcount+ "行记录,每页");
out.println("<input value=/""+ pagesize + "/" size=/"2/" />条");
out.println("当前第<input value=/"" + (curpage +1 )+"/" size=/"2/"/>页/共"+ pagecount + "页");
out.println("<a href="/" mce_href="/""" + uri+"&curpage=0/">第一页</a>");

if(curpage > 0) {
out.println("<a href="/" mce_href="/""" + uri+"&curpage="+(curpage-1)+"/">上一页</a>");
}
if(curpage < pagecount -1) {
out.println("<a href="/" mce_href="/""" + uri+"&curpage=" + (curpage+1)+"/">下一页</a>");
}
out.println("<a href="/" mce_href="/""" + uri+"&curpage=" +(pagecount-1)+"/">最后一页</a>");
out.println("</div>");
}

}


标签的tld文件 :my.tld

view plaincopy to clipboardprint?

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1">

<tlib-version>1.1</tlib-version>
<short-name>my</short-name>

<!-- uri用来定位标签库  -->
<uri>http://java.pojo.com/tag</uri>

<!-- tag用来声明一个标签
name:界面中标签的名字
tag-class:标签的类名
bodyContent:表示标签体的内容的形式
attribute: 用来声明标签类具有的属性
name:属性名
required:是否一定要赋值
rtexprvalue:是否允许使用el表达式
-->
<tag>
<name>pager</name>
<tag-class>com.pojo.web.tag.PagerTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>uri</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>curpage</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pagesize</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pagecount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>rowcount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

</taglib>
<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1">

<tlib-version>1.1</tlib-version>
<short-name>my</short-name>

<!-- uri用来定位标签库  -->
<uri>http://java.pojo.com/tag</uri>

<!-- tag用来声明一个标签
name:界面中标签的名字
tag-class:标签的类名
bodyContent:表示标签体的内容的形式
attribute: 用来声明标签类具有的属性
name:属性名
required:是否一定要赋值
rtexprvalue:是否允许使用el表达式
-->
<tag>
<name>pager</name>
<tag-class>com.pojo.web.tag.PagerTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>uri</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>curpage</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pagesize</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pagecount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>rowcount</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

</taglib>


模糊查询加分页的展示jsp界面

view plaincopy to clipboardprint?
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.pojo.com/tag"  prefix="my" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>管理电影档期</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
<link rel="stylesheet" type="text/css" href="/project/css/style_admin.css" mce_href="project/css/style_admin.css">
<mce:script type="text/javascript" src="/project/js/date.js" mce_src="project/js/date.js"></mce:script>
</head>

<body>
<form action="Release_findAllPaging.action" method="post" name="form1">
<table cellspacing="0" cellpadding="4" width="100%" class="tableborder" id="table3">
<tr>
<td class="header" colspan="7">
信息查询
</td>
</tr>
<tr align="left">
<td width="52px">影片名称</td>
<td width="60px"><input type="text" name="filmInfo.fname" /></td>
<td width="52px">影厅名称</td>
<td width="60px"><s:action name="Cinemainfo2_findAll" namespace="/" executeResult="true" ignoreContextParams="true" /></td>
<td width="26px">日期</td>
<td width="60px"><INPUT name="rdate" onFocus="this.select();" readonly="readonly" onClick="fPopCalendar(event,this,this);" size="20px"  /></td>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
</form>

<table cellspacing="1" cellpadding="4" width="100%" class="tableborder" id="table3">
<tr>
<td colspan="9" 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>
<td align="center" class="altbg1">
<b>删除</b>
</td>
<td align="center" class="altbg1">
<b>查看电影订票情况</b>
</td>
</tr>
<s:iterator value="#request.list">
<tr>
<td align="center" class="altbg2">
<img src="<s:property value=" mce_src="<s:property value="filmInfo.image" />" width="100px" height="100px" />
</td>
<td class="altbg2" align="center">
<s:property value="filmInfo.fname" />
</td>
<td class="altbg2" align="center">
<s:date name="rdate" format="yyyy-MM-dd" />
</td>
<td class="altbg2" align="center">
<s:date name="rtime" format="HH:mm" />
</td>
<td class="altbg2" align="center">
<s:property value="cinemaInfo.cname" />
</td>
<td class="altbg2" align="center">
<s:property value="filmInfo.price" />元
</td>
<td class="altbg2" align="center">
<a href="javascript:if(confirm('确实要删除吗?'))window.location='Release_delete.action?rid=<s:property value=" mce_href="javascript:if(confirm('确实要删除吗?'))window.location='Release_delete.action?rid=<s:property value="rid" ></a>'">删除</a>
</td>
<td class="altbg2" align="center">
<a href="Release_show.action?rid=<s:property value=" mce_href="Release_show.action?rid=<s:property value="rid" ></a>">修改</a>
</td>
<td class="altbg2" align="center">
<a href="SaleAction_getSaleByCondition.action?cid=<s:property value=" mce_href="SaleAction_getSaleByCondition.action?cid=<s:property value="cinemaInfo.cid"></a>&fid=<s:property value="filmInfo.fid"/>&rdate=<s:date name="rdate" format="yyyy-MM-dd"/>&rtime=<s:property value="rtime"/>">查看</a>
</td>
</tr>
</s:iterator>
<tr style="font-size:12px" mce_style="font-size:12px" align="right">
<th colspan="100" class="pager">
<my:pager uri="Release_findAllPaging.action?temp=1&filmInfo.fname=${fname}&cinemaInfo.cid=${cid}&rdate=${rdate}"
curpage="${ curpage }"
pagesize="${ pagesize}"
pagecount="${ pagecount}"
rowcount="${rowcount }"
/>
</th>
</tr>
</table>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.pojo.com/tag"  prefix="my" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>管理电影档期</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
<link rel="stylesheet" type="text/css" href="/project/css/style_admin.css" mce_href="project/css/style_admin.css">
<mce:script type="text/javascript" src="/project/js/date.js" mce_src="project/js/date.js"></mce:script>
</head>

<body>
<form action="Release_findAllPaging.action" method="post" name="form1">
<table cellspacing="0" cellpadding="4" width="100%" class="tableborder" id="table3">
<tr>
<td class="header" colspan="7">
信息查询
</td>
</tr>
<tr align="left">
<td width="52px">影片名称</td>
<td width="60px"><input type="text" name="filmInfo.fname" /></td>
<td width="52px">影厅名称</td>
<td width="60px"><s:action name="Cinemainfo2_findAll" namespace="/" executeResult="true" ignoreContextParams="true" /></td>
<td width="26px">日期</td>
<td width="60px"><INPUT name="rdate" onFocus="this.select();" readonly="readonly" onClick="fPopCalendar(event,this,this);" size="20px"  /></td>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
</form>

<table cellspacing="1" cellpadding="4" width="100%" class="tableborder" id="table3">
<tr>
<td colspan="9" 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>
<td align="center" class="altbg1">
<b>删除</b>
</td>
<td align="center" class="altbg1">
<b>查看电影订票情况</b>
</td>
</tr>
<s:iterator value="#request.list">
<tr>
<td align="center" class="altbg2">
<img src="<s:property value=" mce_src="<s:property value="filmInfo.image" />" width="100px" height="100px" />
</td>
<td class="altbg2" align="center">
<s:property value="filmInfo.fname" />
</td>
<td class="altbg2" align="center">
<s:date name="rdate" format="yyyy-MM-dd" />
</td>
<td class="altbg2" align="center">
<s:date name="rtime" format="HH:mm" />
</td>
<td class="altbg2" align="center">
<s:property value="cinemaInfo.cname" />
</td>
<td class="altbg2" align="center">
<s:property value="filmInfo.price" />元
</td>
<td class="altbg2" align="center">
<a href="javascript:if(confirm('确实要删除吗?'))window.location='Release_delete.action?rid=<s:property value=" mce_href="javascript:if(confirm('确实要删除吗?'))window.location='Release_delete.action?rid=<s:property value="rid" ></a>'">删除</a>
</td>
<td class="altbg2" align="center">
<a href="Release_show.action?rid=<s:property value=" mce_href="Release_show.action?rid=<s:property value="rid" ></a>">修改</a>
</td>
<td class="altbg2" align="center">
<a href="SaleAction_getSaleByCondition.action?cid=<s:property value=" mce_href="SaleAction_getSaleByCondition.action?cid=<s:property value="cinemaInfo.cid"></a>&fid=<s:property value="filmInfo.fid"/>&rdate=<s:date name="rdate" format="yyyy-MM-dd"/>&rtime=<s:property value="rtime"/>">查看</a>
</td>
</tr>
</s:iterator>
<tr style="font-size:12px" mce_style="font-size:12px" align="right">
<th colspan="100" class="pager">
<my:pager uri="Release_findAllPaging.action?temp=1&filmInfo.fname=${fname}&cinemaInfo.cid=${cid}&rdate=${rdate}"
curpage="${ curpage }"
pagesize="${ pagesize}"
pagecount="${ pagecount}"
rowcount="${rowcount }"
/>
</th>
</tr>
</table>
</body>
</html>


这里注意到的一点就是 需要导入自定义标签

还有一点 注意到自定义标签<my> uri的写法 由于我的想法是 将查询条件利用参数传递过去(参数存在request作用域下)

最重要的就是我在路径后面加上了一个temp参数 方便没有带条件的查询[条件的参数为空] (需要对比前面的分页标签类里的uri写法)

上面 Release_findAllPaging.action 在struts.xml里配置 不打算贴出来了 对应的类是ReleaseAction 调用的dao类为ReleaseDao

ReleaseAction类重要方法和属性(get set 方法省略...)

//保存分页的属性

protected Integer curpage = 0;//当前第几页

protected Integer pagesize = 4;//每页条数

protected Integer pagecount;//总页数

protected Integer rowcount; //总行数

//分页查询

public String findAllPaging(){

//地址栏提交中文 tomcat 配置 URIEncoding="GB2312"

HttpServletRequest req = ServletActionContext.getRequest();//拿到请求对象

//表单里的值

String fname = req.getParameter("filmInfo.fname");

String cid = req.getParameter("cinemaInfo.cid");

String rdate = req.getParameter("rdate");

if(fname != null && !fname.equals("")){

req.setAttribute("fname", fname);//不为空的话,放到request作用域下

}

if(cid != null && !cid.equals("")){

req.setAttribute("cid", cid);

}

if(rdate != null && !rdate.equals("")){

req.setAttribute("rdate", rdate);

}

if(fname == null){

fname = (String)req.getAttribute("fname");//等于空 从request作用域下取

}

if(cid == null){

cid = (String)req.getAttribute("cid");

}

if(rdate == null || rdate.equals("")){

rdate = (String)req.getAttribute("rdate");

}

request.put("list",dao.findAllPaging(curpage, pagesize, fname, cid, rdate));//查询当前页的记录 放到list里

//存入当前页

request.put("curpage", curpage);

//存入每页条数

request.put("pagesize", pagesize);

//调用dao获得总行数

Integer rowcount = dao.getRowCount(fname,cid,rdate);

//算出总页数

//101行,每页10条 10.1 使用11页

int pagecount =(int)Math.ceil( (rowcount /( pagesize + 0.0)) );

//总页数

request.put("pagecount", pagecount);

//总条数

request.put("rowcount", rowcount);

return "listAction";

}

dao类重要方法

//根据条件 模糊查询 总记录数

public Integer getRowCount(String fname,String cid,String rdate){

String strSQL = "select count(r) from Release as r where 1 = 1";

if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常

strSQL += " and r.filmInfo.fname like :fname";

}

if(cid != null && !cid.equals("")){

strSQL += " and r.cinemaInfo.cid = :cid";

}

if(rdate != null && !rdate.equals("")){

strSQL += " and r.rdate = :rdate";

}

Query query = HibernateSessionFactory.getSession().createQuery(strSQL);

if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常

query.setString("fname", "%" + fname + "%");

}

if(cid != null && !cid.equals("")){

query.setInteger("cid", new Integer(cid));

}

if(rdate != null && !rdate.equals("")){

query.setString("rdate", rdate);

}

List list = query.list();

return (Integer)list.get(0);

}

//分页模糊查询 档期

public List findAllPaging(int curpage,int pagesize,String fname,String cid,String rdate) {

String strSQL = "select r from Release as r where 1 = 1";

if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常

strSQL += " and r.filmInfo.fname like :fname";

}

if(cid != null && !cid.equals("")){

strSQL += " and r.cinemaInfo.cid = :cid";

}

if(rdate != null && !rdate.equals("")){

strSQL += " and r.rdate = :rdate";

}

Query query = HibernateSessionFactory.getSession().createQuery(strSQL);

if(fname != null && !fname.equals("")){//如果equals在前面的话,容易报 nullpoint 异常

query.setString("fname", "%" + fname + "%");

}

if(cid != null && !cid.equals("")){

query.setInteger("cid", new Integer(cid));

}

if(rdate != null && !rdate.equals("")){

query.setString("rdate", rdate);

}

query.setFirstResult(curpage*pagesize)

.setMaxResults(pagesize);

List list = query.list();

return list;

}

还有要注意点的是 条件是采用get方式传递 中文的问题解决 需在tomcat下配置文件 加上URIEncoding="GB2312"

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jayqean/archive/2010/06/20/5681763.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: