四:后台部分(java)
我用的是MVC 3层模型
servlet部分:(可以跳过)
1 public voiddoPost(HttpServletRequest request, HttpServletResponse response)2 throwsServletException, IOException {3
4 response.setContentType("text/html;charset=utf-8");5 PrintWriter out =response.getWriter();6 //获取分页参数
7 String p=request.getParameter("page"); //当前第几页(点击获取)
8 int page=Integer.parseInt(p);9
10 String row=request.getParameter("rows"); //每页显示多少条记录
11 int rows=Integer.parseInt(row);12
13 String s=request.getParameter("Cat"); //栏目ID
14 int indexId=Integer.parseInt(s);15 JSONObject object=(newContentService()).getContentPaiXuById(indexId, page, rows);16 out.print(object);17 out.flush();18 out.close();19 }
Service部分:(可以跳过)
public JSONObject getContentPaiXuById(int indexId, int page, introws) {
JSONArray array=newJSONArray();
Listcontentlist1=(newContentDao()).selectIndexById(indexId);
Listcontentlist=paginationContent(contentlist1,page,rows);for(Content content:contentlist){
JSONObject object=newJSONObject();
object.put("contentId", content.getContentId());
object.put("caption", content.getCaption());
object.put("createDate", content.getCreateDate());
object.put("times", String.valueOf(content.getTimes()));
object.put("source", content.getSource());
object.put("text", content.getText());
object.put("pic", content.getPic());
object.put("refPic", content.getRefPic());
object.put("hot", content.getHot());
object.put("userId", content.getAuthorId().getUserId());int id =content.getAuthorId().getUserId();
String ShowName= (new UserService()).selectUserById(id).getString("ShowName");
object.put("showName", ShowName);
array.add(object);
}
JSONObject obj=newJSONObject();
obj.put("total", contentlist1.size());
obj.put("rows", array);returnobj;
}
获取出每页的的起止id(这部分是重点),同样写在Service中,比如说假设一页有6条内容,那么第一页的id是从1到6,第二页的id是从7到12,以此类推
1 //获取出每页的内容 从哪个ID开始到哪个ID结束。
2 private List paginationContent(List list,int page,introws){3 Listsmall=new ArrayList();4 int beginIndex=rows*page; //rows是每页显示的内容数,page就是我前面强调多次的点击的分页的页数的索引值,第一页为0,这样子下面就好理解了!
5 System.out.println(beginIndex);6 intendIndex;7 if(rows*(page+1)>list.size()){
8 endIndex=list.size();9 }10 else{11 endIndex=rows*(page+1);12 }13 for(int i=beginIndex;i
Dao层:(可以跳过)
1 public List selectIndexById(intindexId){2 Listlist=new ArrayList();3 try{4 conn =DBConn.getCon();5 String sql = "select * from T_Content,T_User where T_Content.AuthorId = T_User.UserId and CatlogId=? order by CreateDate desc";6 pstm =conn.prepareStatement(sql);7 pstm.setInt(1, indexId);8 rs =pstm.executeQuery();9 SimpleDateFormat ff=new SimpleDateFormat("yyyy年MM月dd日 hh时mm分");10 while(rs.next()){11 Content content = newContent();12 content.setContentId(rs.getInt("ContentId"));13 content.setCaption(rs.getString("Caption"));14 content.setCreateDate(f.format(rs.getTimestamp("CreateDate")));15 content.setTimes(rs.getInt("Times"));16 content.setSource(rs.getString("Source"));17 content.setText(rs.getString("Text"));18 content.setPic(rs.getString("Pic"));19 content.setRefPic(rs.getString("RefPic"));20 content.setHot(rs.getInt("Hot"));21 User user = newUser();22 user.setUserId(rs.getInt("UserId"));23 content.setAuthorId(user);24 Catlog catlog = new Catlog(); //CntURL待开发
25 catlog.setCatlogId(rs.getInt("CatlogId"));26 content.setCatlog(catlog);27 list.add(content);28 }29 }catch(Exception e){30 e.printStackTrace();31 }finally{32 DBConn.closeDB(conn, pstm, rs);33 }34 returnlist;35 }
以上就是网页所实现的分页代码,easy-ui部分的分页也可以参考以上代码。如果有所收获,支持一下哟,谢谢!