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

Jsp+servlet做一个分页显示怎么做跪求代码要全面的... Jsp + servlet 做一个分页显示 怎么做 跪求代码 要全面的 展开
 我来答
匿名用户
2013-08-19
展开全部
我这个是基于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!,现在我们的分页就行了,赶快试试吧!
匿名用户
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>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-19
展开全部
我有一个jsp+javaBean的 简单改一下应该没什么问题的 要的话加我634154355
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式