Jsp Pager Tag 適用帶有頁(yè)碼的翻頁(yè)功能(<<1 2 3 4 >>),形式風(fēng)格多種多樣,變換起來(lái)十分簡(jiǎn)單。
-------------
環(huán)境配置:
-------------
1,如果使用Struts并且使用DispatchAction,那么需要修改一下Jsp Pager Tag的代碼,如何修改可以參考:
http://blog.csdn.net/srx/archive/2005/10/06/495827.aspx
最新版本請(qǐng)到http://jsptags.com下載,之后把重新編譯的jar放到工程的WEB-INFO/lib/下。
并把pager-taglib.tld放到WEB-INFO/下。
web.xml:
<taglib-uri>
http://jsptags.com/tags/navigation/pager
</taglib-uri>
<taglib-location>
/WEB-INF/pager-taglib.tld
</taglib-location>
</taglib>
2,Strus (略)
<action path="/u2bmanpointlist"
name="U2BmanPointForm"
parameter="method"
type="cn.sintal.action.front.U2BmanPointListAction">
<forward name="FJ0" path="/WEB-INF/jsp/front/FJ0.jsp"/>
<forward name="FJ1" path="/WEB-INF/jsp/front/FJ1.jsp"/>
</action>
3,Hibernate(略)
--------------
具體程序:
---------------
1,Action
Action:
==========================================================================
public ActionForward displayList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
int MAX_PAGE_ITEMS = 2;
U2CheckSessionBo.front_Neednt_Login(request, mapping);
// 獲得城市id
String cityid = request.getParameter("usercityid");
if (cityid == null || cityid.equals("")) {
cityid = "1";
}
request.setAttribute("usercityid", cityid);
// 查詢工具類
DBUtil obj = new DBUtil();
// 列表hql
StringBuffer sb = new StringBuffer();
sb.append("select from BBusinessman bman where ");
sb.append(" bman.flag=? and bman.c.id=? ");
sb.append(" order by bman.point desc");
String hql_list = sb.toString();
// 得到總記錄數(shù)的hql
String hql_count = " select count(*) from BBusinessman bman where bman.flag=? and bman.c.id=? ";
// 查詢條件
Object[] data = new Object[] { "0", new Integer(cityid) };
// 查詢對(duì)象
BBusinessmanDAO dao = new BBusinessmanDAO();
// 從頁(yè)面獲得偏移量
String offset = request.getParameter("pager.offset");
if (offset == null) {
// 第一次查詢,需要查出記錄總數(shù)
int count = 0;
count = obj.getAllNumber(hql_count, data, dao);
// 只需要下傳頁(yè)面總記錄數(shù),每頁(yè)記錄數(shù)即可。
request.getSession().setAttribute("resultSize", "" + count);
}
log.info("the varname=offset ,value=" + offset);
if (offset == null || offset.equals("")) {
offset = "0";
}
int intoffset = 0;
if (offset != null && !offset.equals("")) {
intoffset = Integer.parseInt(offset);
}
// 查詢處索要的list
List tmp = obj.doSearch(hql_list, data, dao, intoffset,
MAX_PAGE_ITEMS);
request.setAttribute("u2bmanpointlist", tmp);
} catch (Exception e) {
log.error(e.getMessage());
return mapping.findForward("error");
}
return mapping.findForward("FJ0");
}
2,Action里使用到的工具類:
DBUtil.java
/**
* 得到總數(shù)
*
* @param hql
* @param data
* @param dao
* @return
* @throws Exception
*/
public int getAllNumber( String hql , Object[] data ,_RootDAO dao )
throws Exception{
int count = 0 ;
Session session = null;
try {
_RootDAO.initialize();
session = dao.getSession();
if (session == null || session.isOpen() == false) {
session = dao.createNewSession();
}
Query query = dao.getQuery(hql, session);
if (query == null) {
throw new Exception("query is null ");
}
if (data != null)
log.info("data.length=" + data.length);
for (int i = 0; data != null && i < data.length; i++) {
System.out.print("the aql is =" + hql);
System.out.println("data is " + data[i]);
query.setParameter(i , data[i]);
}
Integer retInt = (Integer)(query.uniqueResult());
if( retInt!=null )
count = retInt.intValue();
return count;
} catch (SQLException e) {
log.error(e.getMessage());
throw e;
} catch (HibernateException e1) {
log.error(e1.getMessage());
throw e1;
} finally {
if (session != null && session.isOpen())
session.close();
}
}
/**
* 得到某頁(yè)的數(shù)據(jù)
* @param hql
* @param data
* @param dao
* @param offset
* @param pagenum
* @return
* @throws Exception
*/
public List doSearch(String hql, Object[] data, _RootDAO dao, int offset,
int pagenum) throws Exception {
Session session = null;
try {
_RootDAO.initialize();
session = dao.getSession();
if (session == null || session.isOpen() == false) {
session = dao.createNewSession();
}
Query query = dao.getQuery(hql, session);
if (query == null) {
throw new Exception("query is null ");
}
if (data != null)
log.info("data.length=" + data.length);
for (int i = 0; data != null && i < data.length; i++) {
System.out.print("the aql is =" + hql);
System.out.println("data is " + data[i]);
query.setParameter(i , data[i]);
}
query.setFirstResult(offset);
query.setMaxResults(pagenum);
List retlist = query.list();
return retlist;
} catch (SQLException e) {
log.error(e.getMessage());
throw e;
} catch (HibernateException e1) {
log.error(e1.getMessage());
throw e1;
} finally {
if (session != null && session.isOpen())
session.close();
}
}
3,Jsp
===============================================
<%@ taglib uri="/WEB-INF/pager-taglib.tld" prefix="pg" %>
<TABLE width=760 border=0 align=center cellPadding=0 cellSpacing=0 bgcolor="#F2F5FA">
<tr>
<td height="200" colspan="7" bgcolor="#F2F5FA"> </td>
<td width="704" height="300" align="right" bgcolor="#F2F5FA"><TABLE cellSpacing=1 cellPadding=5 width="95%" align=center bgColor=#325597 border=0>
<TR bgcolor="2C6DB1" background="/front/img/sintal_register_107.gif">
<TD width="50" align=center background="/front/img/sintal_register_107.gif" bgcolor="2C6DB1" class=text4>排名</TD>
<TD width="100" align=center background="/front/img/sintal_register_107.gif" bgcolor="2C6DB1" class=text4>商戶名稱</TD>
<TD width="200" align=center background="/front/img/sintal_register_107.gif" bgcolor="2C6DB1" class=text4>地址</TD>
<TD width="101" align=center background="/front/img/sintal_register_107.gif" bgcolor="2C6DB1" class=text4>電話</TD>
<TD width="101" align=center background="/front/img/sintal_register_107.gif" bgcolor="2C6DB1" class=text4>積分</TD>
<TD width="50" align=center background="/front/img/sintal_register_107.gif" bgcolor="2C6DB1" class=text4> 分類</TD>
</TR>
<bean:define id="listsize" name="resultSize" type="String"/>
<pg:pager items="<%=(Integer.parseInt(listsize))%>"
maxPageItems="<%=2 %>"
maxIndexPages="<%=5%>"
isOffset="<%= true %>"
url="/u2bmanpointlist.do?method=displayList"
export="offset,currentPageNumber=pageNumber"
scope="request"
>
<!-- 這樣在uri中增加一個(gè)parameter.可以增加多個(gè)-->
<bean:define id="cid" name="usercityid" type="String" scope="request"/>
<pg:param name="usercityid" value="<%=cid%>"/>
<!-- 從page對(duì)象中取得屬性值firstItem , lastIterm -->
<pg:page export="firstItem, lastItem">
<!-- 顯示結(jié)果集的摘要信息 -->
<div class="resultInfo">
當(dāng)前頁(yè)數(shù):<strong><bean:write name="currentPageNumber"/></strong> ,本頁(yè)從第 <strong><%= firstItem %></strong> 條到第 <strong> <%= lastItem %></strong>條 <strong>/</strong> 總記錄數(shù) <strong><bean:write name="resultSize"/></strong>條
</div>
</pg:page>
<!-- begin iterator -->
<logic:present name="u2bmanpointlist">
<logic:notEmpty name="u2bmanpointlist">
<logic:iterate id="u2bmanobj" name="u2bmanpointlist" indexId="ind">
<bean:define id="var_bman_indx" name="ind" type="Integer"/>
<% if( var_bman_indx.intValue()%2==0){%>
<TR bgcolor="#E3F1FE">
<%}else{%>
<TR bgcolor="#C1DAF7">
<%}%>
<TD align="center" class=text1>
<%=(var_bman_indx.intValue()+offset.intValue()+1)%></TD>
<TD align="center" class=text1>
<a target="_blank" class="black" href="/u2bmanpointlist.do?method=detail&bid=<bean:write name="u2bmanobj" property="id"/>">
<bean:write name="u2bmanobj" property="companyName"/></a>
</TD>
<TD align="center" class=text1><bean:write name="u2bmanobj" property="companyAddress"/></TD>
<TD align="left" class=text1><bean:write name="u2bmanobj" property="telephone"/></TD>
<TD align="center" class=text1><bean:write name="u2bmanobj" property="point" format="###,###"/></TD>
<TD align="center" class=text1><bean:write name="u2bmanobj" property="ty.tyName"/></TD>
</TR>
</logic:iterate>
</logic:notEmpty>
</logic:present>
<!-- end iterator-->
</TABLE>
<jsp:include page="/alltheweb.jsp" flush="true"/><br>
<%
/**這些是各種不同的風(fēng)格。
%>
<jsp:include page="/google.jsp" flush="true"/><br>
<jsp:include page="/yahoo.jsp" flush="true"/><br>
<jsp:include page="/altavista.jsp" flush="true"/><br>
<jsp:include page="/lycos.jsp" flush="true"/><br>
<jsp:include page="/texticon.jsp" flush="true"/><br>
<jsp:include page="/simple.jsp" flush="true"/><br>
<%
**/
%>
</div></td>
</pg:pager>
</table>
<div class="rnav">
<span class="rnavLabel">Results:</span>
<pg:prev export="pageUrl">
<a href="<%= pageUrl %>" class="rnavLink">« Prev</a>
</pg:prev>
<pg:pages export="pageUrl,pageNumber,firstItem,lastItem">
<% if (pageNumber == currentPageNumber) { %>
<span class="rnavCurr"><%= firstItem %>-<%= lastItem %></span>
<% } else { %>
<a href="<%= pageUrl %>" class="rnavLink"><%= firstItem %>-<%= lastItem %></a>
<% } %>
</pg:pages>
<pg:next export="pageUrl">
<a href="<%= pageUrl %>" class="rnavLink">Next »</a>
</pg:next>
</div>
</pg:index>
總結(jié):
使用它的好處是:
1,Action編碼比較少,只需要把總記錄數(shù),每頁(yè)記錄數(shù)兩個(gè)變量。
2, 風(fēng)格變化十分方便,它提供了幾種外觀,定制起來(lái)也十分方便.
4, 不用時(shí)用隱藏變量了,不需要額外的form.
5, 向url寫(xiě)參數(shù)十分方便.例如<pg:param name="" value=""/>就可以傳遞參數(shù).
缺點(diǎn):
1,頁(yè)面上仍不可避免地使用了少量的腳本(“<%=%>”這樣的語(yǔ)句)。