樊占江 发表于 2012-8-6 23:12
在src或者配置里面解决
dao层- /**
- * 根据分页条件得到每页的数据
- * @param beginIndex
- * @return List<Company>
- */
- public List<Company> getResultByLimit(int beginIndex) {
- String sql="SELECT * FROM companys ORDER BY id LIMIT ?,?";
- BeanListHandler<Company> beanListHandler=new BeanListHandler<Company>(Company.class);
- return template.findAllData(sql, beanListHandler, beginIndex,Constants.EVERYPAGE);
- }
-
复制代码 service层
/**
* 得到简单分页的对象
* @param nowPage
* @return
*/
public Page getPage(int nowPage) {
Page page =new Page();
int allCounts=companydao.getCompanysCount();
page.init(nowPage, allCounts);
page.setAllCounts(allCounts);
page.setNowPage(nowPage);
int beginIndex=(nowPage-1)*Constants.EVERYPAGE;//设置查询的开始索引位置
page.setResult(companydao.getResultByLimit(beginIndex));
return page;
}
web层
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BusinessServiceImpl service=new BusinessServiceImpl();
int currentPage=1;
//获取标志位
//获取当前页的页数
String scurrentPage=request.getParameter("currentPage");
if(scurrentPage!=null &&!"".equals(scurrentPage))
currentPage=Integer.parseInt(scurrentPage);
Page page=service.getPage(currentPage);//调用服务层的方法获取Page对象
request.setAttribute("page",page);//将page对象传递到jsp
request.getRequestDispatcher("/company/list.jsp").forward(request, response);//转发
页面
<body>
<form action="" method="post">
<table border="1" align="center">
<input type="hidden" name="flag" id="flag" value="page"/>
<tr>
<td>编号</td><td>客户</td><td>职业编号</td><td>客户来源编号</td><td>客户区域编号</td><td>邮编</td><td>地址</td>
</tr>
<c:forEach items="${page.result }" var="com">
<tr>
<td>${com.id }</td><td>${com.company_name}</td><td>${com.job_name }</td><td>${com.source_name }</td><td>${com.location_name }</td><td>${com.pcode }</td><td>${com.address }</td>
</tr>
</c:forEach>
</table>
<p align="center">共有${page.allCounts}条记录 共${page.allPages }页 当前是第${page.nowPage }页
<a href="javascript:page(1)">首页</a>
<a href="javascript:page(${page.prePage })">上页</a>
<a href="javascript:page(${page.nextPage })">下页</a>
<a href="javascript:page(${page.allPages })">尾页</a>
</p>
</form>
</body>
<script type="text/javascript">
function page(currentPage){
//<input type="hidden" name="flag" id="flag" value="page"/>
//获取标志位的值
var flag=document.getElementById("flag").value;
var path="${pageContext.request.contextPath }/servlet/SimplePagingServlet?currentPage="+currentPage+"&flag="+flag;
window.location=path;
}
</script>
</html>
|