找一个最好的JSP分页Tag。连接数据库使用的是hibernate
解决方案 »
- UTF-8 大小写问题
- 为什么ArrayList的值会改变?
- 重新安装了一遍tomcat myeclipse中启动 出现严重警告
- 求页面提交进入action,action用request对象转发页面,之后新页面刷新会继续执行action的问题解决方案???
- eclipse中 给xml 添加 dtd 的步骤!
- SVF帐票工具的几个问题,期待高人解答。
- struts中传值给form时得到乱码
- js如何获取table中 tr的行号
- tomcat 4.1 配置虚拟目录,百思不解...帮帮忙
- 关于建立session然后在JSP和SERVLET中传递的小问题.
- 如何让我的站点被搜索引擎收录?
- struts2 action到页面传值问题
http://hi.baidu.com/%CC%EC%CF%C2%CE%DE%C0%B6/blog/item/e6492cfd13e44142d7887d13.html
String zttype = request.getParameter("zt_type");
if(zttype==null){zttype="all";}
String strpageNo = request.getParameter("pageNo");
int pageNo = 1;
if(strpageNo==null||strpageNo.equals("")){
pageNo =1;
}else{
try{
pageNo = Integer.parseInt(strpageNo);
}catch(NumberFormatException e){
pageNo=1;
}
if(pageNo<=0){
pageNo=1;
}
}
int totalPages = 0;//总共多少页
int pageSize = 3;// 每页显示几条
boolean flag = false;//声明一个boolean变量,用于判断是否有内容,根据flag来做出提示
ResultSet rs = null;
int pagePos = 0; //显示的起始位置
int lastPage = 0;
try{
rs = newutil.findCount(zttype);
rs.next();
//首先得到记录的总条数
int totalRecords = rs.getInt(1);
//看一共要分几页,是你的pagesize决定的(每页显示几条)
totalPages = totalRecords % pageSize == 0 ? totalRecords/ pageSize : totalRecords / pageSize + 1;
lastPage = totalPages;
//比如共能分2页,你输入显示第三页,那就不ok了,大于了,就显示最后一页
if (pageNo > totalPages){
pageNo = totalPages;
}
pagePos =(pageNo - 1) * pageSize;}
catch(Exception e){}
rs = newutil.select(pagePos,pageSize,zttype);
while(rs.next()){
long id = rs.getLong(1);
String title = rs.getString(2);
Date time = rs.getDate(4);
String type = rs.getString(6);
%>
<tr><td align="left"><img src="images/dot_main01.gif"> <a href="/toba/showNew.jsp?newId=<%=id%>" target="show1"><%=title%></a></td><td><%=type %></td><td><%=time %></td></tr>
<%
}
%>
<tr><td align="center">共<%=totalPages%>页 第<%=pageNo%>页 <a href="news.jsp?pageNo=<%=pageNo - 1 %>&zt_type=<%=zttype%>" onclick="return linkClick(this)">上一页</a> <a href="news.jsp?pageNo=<%=pageNo + 1 %>&zt_type=<%=zttype%>" onclick="return linkClick(this)">下一页</a> <a href="news.jsp?pageNo=<%=lastPage %>&zt_type=<%=zttype%>" onclick="return linkClick(this)">尾页</a></td></tr>
</table><center><%for(int i=1;i<=totalPages;i++){
%><a href="news.jsp?pageNo=<%=i %>&zt_type=<%=zttype%>" onclick="return linkClick(this)"><%=i %></a>
<%} %></center>
</td>
</tr>
</table>
Session session = HbnUtil.getSession();
Connection connection = session.connection();
Statement sts = connection.createStatement();
String sql = null;
if (type==null||type.equals("all")) {
sql = "select count(*) from new_t";
} else {
System.out.println((String)type);
sql = "select count(*) from new_t where zt_type=" + "'"
+ (String)type + "'";
}
return sts.executeQuery(sql);
} public ResultSet select(int pagePos, int pageSize, String type)
throws Exception {
Session session = HbnUtil.getSession();
Connection connection = session.connection();
String sql = null;
PreparedStatement pstmt = null;
if (type==null||type.equals("all")) {
sql = "select * from new_t order by zt_id desc limit " + pagePos + "," + pageSize;
} else {
sql = "select * from new_t " + "where zt_type=" + "'"
+ type + "'" + " order by zt_id desc limit " + pagePos + ","
+ pageSize;
}
pstmt = connection.prepareStatement(sql);
return pstmt.executeQuery();
}