Jsp + servlet 做一个分页显示 怎么做 跪求代码 要全面的

Jsp + servlet 做一个分页显示 怎么做 跪求代码 要全面的

我这个是基于ssh分页的,和selevt差不多,你可以参考一下,最重要的是自个会了理解了才行,别人的总是别人的。。。。。。好了 往下看。开始了。。。。。。。。。。。。。。。。 分页应该是每个程序员都要掌握的技术,也是用处最多的一种,本人不才,写了一个分页(基于数据库),请大家指点,首先得来个实体类(存储分页的信息,我起名称叫Page)
import java.util.ArrayList;
import java.util.List;public class Page {
private int totalPage = 1;/**
* 前一页
*/
private int prePage = 1;/**
* 下一页
*/
private int nextPage = 1;/**
* 总记录数
*/
private int totalRec = 0;/**
* 默认每页记录数
*/
private final int defaultPageSize = 10;/**
* 每页记录数
*/
private int pageSize = defaultPageSize;/**
* 当前页码
*/
private int pageIndex = 1;/**
* 全部页码,从1开始
*/
private int[] pageNumbers;public int getPageIndex() {
return pageIndex;
}public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex > 0 ? pageIndex : 1;
}public int getNextPage() {
return nextPage;
}public void setNextPage(int nextPage) {
this.nextPage = nextPage > this.totalPage ? this.totalPage : nextPage;
}public int getPageSize() {
return pageSize;
}public void setPageSize(int pageSize) {
this.pageSize = pageSize > 0 ? pageSize : 10;
}public int getPrePage() {
return prePage;
}public void setPrePage(int prePage) {
this.prePage = prePage < 1 ? 1 : prePage;
}public int getTotalPage() {
return totalPage;
}public void setTotalPage(int totalPage) {
this.totalPage = totalPage > 0 ? totalPage : 1;
}public int getTotalRec() {
return totalRec;
}public void setTotalRec(int totalRec) {
this.totalRec = totalRec > -1 ? totalRec : 0;
}public int[] getPageNumbers() {
return pageNumbers;
}public void setPageNumbers(int[] pageNumbers) {
this.pageNumbers = pageNumbers;
}
}
==============实体类有了,我们得在写一个公共的DAO方法(也就是每一个用到分页的地方都会调用这个)================public List findPageByQuery(final String queryString,
final Object[] parameters, final Page pageInfo) {
return (List) getHibernateTemplate().execute(new HibernateCallback()//这里使用了匿名内部类
{
public Object doInHibernate(Session session)//Spring进行事务维护 省去每次创建session和关闭session
throws HibernateException
{
Query query = session.createQuery(queryString);
if (parameters != null)
{
for (int i = 0; i < parameters.length; i++)
{
query.setParameter(i, parameters[i]);
}
}
ScrollableResults sr = query.scroll();
sr.last();
int totalCount = sr.getRowNumber();
int startIndex = (pageInfo.getPageIndex() - 1)
* pageInfo.getPageSize();
query.setMaxResults(pageInfo.getPageSize());
query.setFirstResult(startIndex);
int totalRec = totalCount + 1;
pageInfo.setTotalRec(totalRec);
int totalPage = (totalRec % pageInfo.getPageSize() == 0) ? (totalRec / pageInfo
.getPageSize())
: (totalRec / pageInfo.getPageSize()) + 1;
int[] pageNumbers = new int[totalPage];
for (int i = 0; i < totalPage; i++)
{
pageNumbers[i] = (i + 1);
}
pageInfo.setPageNumbers(pageNumbers);
pageInfo.setTotalPage(totalPage);
pageInfo.setPageSize(pageInfo.getPageSize());
pageInfo.setPageIndex(pageInfo.getPageIndex());
pageInfo.setPrePage(pageInfo.getPageIndex() - 1);
pageInfo.setNextPage(pageInfo.getPageIndex() + 1);
return query.list();
}
}, true);
}
=================至此我们的工作就完成了一半了 ,接下了就改写具体的方法了,来调用我们的公工分页方法public List getUU(int lexample,Page pageInfo)
{
/*ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
dao = (DaoImpl) context.getBean("Dao");
System.out.print(dao);*/ //自己测试时候可以使用
List list = null;
if (pageInfo == null)
pageInfo = new Page();
try
{
hql="SELECT exa FROM Example exa WHERE 1=1 AND exa.LExample="+lexample;
list = this.getCommonDAO().findPageByQuery(hql, null, pageInfo);
} catch (Exception ex)
{
System.out.println(ex);
}
return list;
}=============接下来就是action中调用了===============public ActionForward getMore(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ExampleForm exampleForm = (ExampleForm) form;// TODO Auto-generated method stub
List list = new ArrayList();
try
{
//pageInfo = (PageInfo) request.getAttribute("pageInfo");
String type=request.getParameter("type");
//类别编号
int typeId=0;
//页码编号
String page=request.getParameter("page");
if(page==null)
{page="1";<br> }
if(type==null) {
//默认显示女的
type="2";
}

System.out.println("action...........................");
pageInfo.setPageIndex(Integer.parseInt(page));
pageInfo.setPageSize(20);
typeId=Integer.parseInt(type);
// pageInfo.setPageIndex(Integer.parseInt(request.getParameter("Index")));

list = this.exampleBiz.getUU(typeId,pageInfo);
if(list.size()!=0)
{
request.setAttribute("list", list);
request.setAttribute("pageInfo", pageInfo);
}
}catch
(Exception ex)
{
ex.printStackTrace();
}
return mapping.findForward("more");
}======下面是页面,我们的工作马上就完了。<%String nowPage ;
String type;
int total;

nowPage=request.getParameter("page");
if(nowPage==null)
{nowPage="1";<br>}
type=request.getParameter("type"); //这个是我项目中的类别,没必要的
if(type==null) {
type="2";
}
int curPage=Integer.parseInt(nowPage);
int typeId=Integer.parseInt(type);
System.out.println(typeId+"------------------------------------");
if((Page)request.getAttribute("pageInfo") == null){
total = 1;
} else {
total = ((Page)request.getAttribute("pageInfo")).getTotalPage();
if(curPage>((Page)request.getAttribute("pageInfo")).getTotalPage()) {
curPage=total;
}
}

%><div style="width:870px; margin-bottom:20px;">
<logic:notEmpty name="list">
<logic:iterate id="example" name="list">
<div class="s_ren">
<div><img src="example.account.headIcn" /></div>
<div class="style_ju"><strong>${example.account.name }</strong></div>
<div>明星指数:<span class="style_ju">254179</span></div>
<div style="float:left; background-color:#FFC189; text-align:center; height:22px; width:55px; line-height:22px;">送鲜花</div>
<div style="float:right; background-color:#9DCBE7; text-align:center;height:22px; width:55px; line-height:22px;">扔鸡蛋</div>
</div>
</logic:iterate>
</logic:notEmpty>
</div>
<div style=" border-top:1px solid #ccc; height:1px;"></div>
<div style="text-align:center">
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=1&type=<%=typeId %>">首页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=curPage-1<0?1:curPage-1%>&type=<%=typeId %>"">上一页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=(curPage+1)>total?total:curPage+1%>&type=<%=typeId %>"">下一页</a> |
<a href="${pageContext.request.contextPath }/example.do?method=getMore&page=<%=total %>">末页</a>
当前第<%=curPage %>页 转到第 <select id="page" onchange="window.location.href='${pageContext.request.contextPath }/example.do?method=getMore&page='+document.getElementById('page').value;&type=<%=typeId %>"> <%
for(int toPage = 1; toPage<=total; toPage++) {
%>
<option value = <%=toPage%>
<%if(toPage==curPage){ %>
selected
<%} %>
>
<%=toPage %>
</option>
<%
}
%>
</select> 页
共<%=total%>页 ${pageInfo.pageIndex}
</div>OK!,现在我们的分页就行了,赶快试试吧!
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-08-19
分页 select
package com.xaccp.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.xaccp.daoImpl.BookDaoImpl;
import com.xaccp.entity.Users;public class SelectServlet extends HttpServlet
{ public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{ this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{ // 实例化对象
HttpSession session = request.getSession();
Users LoginUser = (Users) session.getAttribute("LoginUser");
if (LoginUser != null)
{
BookDaoImpl bookDao = new BookDaoImpl();
String relation = request.getParameter("relation");
if(relation==null)
{
relation="0";
}
List allList = null;
// 总条数
int c = 0;
// 总页数:
int s = 0;
// 每页显示数量:
int num = 9;
// 要访问的页数
int pg = 1;
// 获取请求中的页数
String p1 = request.getParameter("p");
if (p1 == null)
{
p1 = "1";
}
try
{
pg = Integer.parseInt(p1);
}
catch (Exception ex)
{
pg = 1;
}

if ("0".equals(relation) || relation == null)
{
c = bookDao.Count(LoginUser.getUserID());

s = (c + num - 1) / num;

if (pg > s)
{
pg = s;
}
if (pg <= 0)
{
pg = 1;
}
allList = bookDao.allList(LoginUser.getUserID(), pg, num);
}
else
{
c = bookDao.Count(LoginUser.getUserID(), relation);

s = (c + num - 1) / num;
if (pg > s)
{
pg = s;
}

if (pg <= 0)
{
pg = 1;
}
allList = bookDao.findRelationList(LoginUser.getUserID(),
relation, pg, num);
}
//将要转发的值存入request
request.setAttribute("allList", allList);
request.setAttribute("pg", pg);
request.setAttribute("s", s);
request.setAttribute("relation", relation);
//转发
request.getRequestDispatcher("/list.jsp")
.forward(request, response);
}
else
{
response.sendRedirect("/addressBook/index.jsp");
}
}}
分页查询方法 public List allList(String userId,int pg,int num)
{
String sql="select top "+num+" *from book where userId=? and id not in (select top "+(pg-1)*num+" id from book where userId=? order by id desc) order by id desc";
String [] param={userId,userId};
List allList=bookList(sql, param);
return allList;
} 页面显示<form name="f1" method="post">
<table width="77%" height="51" border="1" cellpadding="0" cellspacing="0" bordercolor="green">
<tr>
<td height="24"><div align="center">全选
<input type="checkbox" name="all" onClick="selectall()">
</div></td>
<td><div align="center">姓名</div></td>
<td><div align="center">电话</div></td>
<td><div align="center">地址</div></td>
</tr>

<c:forEach var="b" items="${requestScope.allList}">
<tr align="center">
<td><div align="center"><input type="checkbox" name="sele" value="${b.id }" onClick="selects1()"></div></td>
<td><div align="center"><a href="findBookById?Id=${b.id }">${b.name }</a></div></td>
<td><div align="center">${b.phone }</div></td>
<td><div align="center">${b.address }</div></td>
</tr>
</c:forEach> <tr>
<td colspan="4">
<div align="center">
共${s }页

当前${pg}页

跳转<input style="width=30" name="page" > <input type=button value="GO" onClick="goto(${pg},${relation })">

<a href="SelectServlet?p=${pg-1}&relation=${relation }">上一页</a>

<a href="SelectServlet?p=${pg+1}&relation=${relation }">下一页</a>

<input type="button" onClick="delmsg()" name="Submit3" value="删除">

</tr>
</table>
</form> 页面JavaScript验证 <script language="javascript" type="text/javascript">
var all_ = document.getElementById("all");
var s1_ = document.getElementsByName("sele");
function selectall()
{
for (var i = 0 ; i < s1_.length ; i ++ )
{
s1_[i].checked = all_.checked;
}
}
function selects1()
{
var c = getSelectCount();
if(c == s1_.length)
{
all_.checked = true;
}
else
{
all_.checked = false;
}
}
function getSelectCount()
{
var c = 0;
for (var i = 0 ; i < s1_.length ; i ++ )
{
if(s1_[i].checked)
{
c ++ ;
}
}
return c;
}function delmsg()
{
var c = getSelectCount();
if(c == 0)
{
alert("请选择要删除的记录!");
return;
}
if(confirm("确定要删除吗?"))
{
document.f1.action="delServlet";
document.f1.submit();
}
}
function goto(p,r)
{
var p= document.f1.page.value;
if(p.length==0)
{
alert("请输入跳转页数");
document.f1.page.focus();
return;
}
if(isNaN(p))
{
alert("页数必须是数字");
document.f1.page.value="";
document.f1.page.focus();
return;
}
location.href="SelectServlet?p="+p+"&relation="+r;
}
</script>
第2个回答  2013-08-19
我有一个jsp+javaBean的 简单改一下应该没什么问题的 要的话加我634154355

jsp+servlet分页,点下一页,下一页的内容接上一页的内容显示,怎么办?
int lastStartRecord=0;\/\/最后一页开始显示记录的编号 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { \/\/获取需要显示的页数,由用户提交 if(request.getParameter("pageNo")==null){ \/\/如果为空,则表示第1页 if(startRow == 0){...

在JSP中如何实现分页技术啊?
如果对RowSetPage.getHTML()生成的默认分页代码不满意可以编写自己的分页处理代码,RowSetPage提供了很多getter方法用于获取相关信息(如当前页码、总页数、 总记录数和当前记录数等)。 在实际应用中可以将分页查询和显示做成jsp taglib, 进一步简化JSP代码,屏蔽Java Code。 附:分页工具类的源代码, 有注释,应该很容易理...

jsp分页查询中 加入一段代码实现输入页码跳转
private void toList(HttpServletRequest request, HttpServletResponse response) { String key=request.getParameter("ukey");User u=new User();WebPage wp=new WebPage();u.setName(key);request.setAttribute("ukey", key);int currentPage=1;if(request.getParameter("currentPage")!=null){ ...

jsp怎么实现文章内容的分页显示,文章内容包含HTML标签。
1。取出的分页内容中,html标签不完整(比如<a href=,后面就没了。。。),针对这种情况,我们应该实现一个向前搜索和向后搜索内容以保证html标签完整性的功能。2。有可能出现标签不成对的情况,比如有,没有这种情况,那么这个问题也是我们需要去搜索内容来解决的。或者说做的过分点,我们写一个缺少结...

JSP+Servlet+JavaBean写“博奥教育网址之家“前台和后台,后台卡在分页...
给你一个通用的分页代码 但是数据量小的时候可以 大的时候就很慢了 pagesize \/\/每页显示的数据大小 rowcount\/\/行记录 iPage\/\/页数 pagecount\/\/总页数 recordcount \/\/总的记录数 sqlRst.last();recordcount = sqlRst.getRow();sqlRst.beforefirst();\/\/这段代码是取得总的记录数 然后进行分页...

JSP页面分页实现,大家帮忙啊
if (intPage > 1) { ><a href="<%=request.getContextPath()%>\/pages\/wish.jsp?page=<%=intPage - 1%>">上一页 < } > 祝愿人 接受人 祝愿内容 祝愿时间 < if (wishes.size() != 0) { for (Wish wish : wishes) { > <a href="<%=request.getContextPath(...

今天要写一个java分页的页面,jsp页面需要传三个参数beginpage 和 endp...
nowpage 默认是 request 作用范围的 在作为 servlet类里 应该 request.setRequest("nowpage",相应大变量);

java分页显示代码,在页面有每页显示的条数pageSize和当前页pageNow两个...
上一页,此处的a代表超链接,inut代表你的那个输入框,你最好给他们加上ID,然后代替掉他们

jsp怎么打开多个标签页
1、先定义一个实体类,包含一些设置信息和承载要显示的分页数据。2、然后开始编写分页标签。定义一个类,让它继承TagSupport类,然后重写其中的部分方法。3、通过在value属性来从Requst中获取之前从后台填充了数据的PageBean对象。标签类写完,还要编写对应的tlg文件。4、然后在web.xml中进行注册。5、再编写...

jsp页面如何让超链接无效,分页时如果是最后一页那么下一页就不可用
<%if(页码<总页数){ %> <a href="data_list_servlet?pageNo=<%=pageNo+1 %>">下一页 <%} %>意思就是如果当前页页码大于或者等于总页数的话,下一页这几个字就不显示了

相似回答