程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> javabean servlet jsp完成分頁功效代碼解析

javabean servlet jsp完成分頁功效代碼解析

編輯:關於JAVA

javabean servlet jsp完成分頁功效代碼解析。本站提示廣大學習愛好者:(javabean servlet jsp完成分頁功效代碼解析)文章只能為提供參考,不一定能成為您想要的結果。以下是javabean servlet jsp完成分頁功效代碼解析正文


前端完成用ligerUI完成分頁,感到用框架確切簡略,閒著無聊,模仿著liger的分頁界面完成了一遍(只需是功效,款式甚麼疏忽) 

這裡用基本的三層架構+servlet+jsp完成,思緒很簡略,把一切分頁相干信息寫入到一個pagebean類外面,service前往這個bean類,每次分頁查詢時都從該bean裡查找信息。只是個中的細節成績比擬繁瑣,如界限處置(前端和後台界限都要處置),下拉框跳轉後要顯示以後頁等等 

這是ligerUI完成的分頁款式(完成進程我的上一篇博客有寫:http://www.jb51.net/article/92850.htm) 

模仿完成進程: 

目次構造

 

數據庫(mysql)

 

model層,一個數據庫對應的model(Blog),還有一個pageBean(BlogPage) 

import java.sql.Date;

public class Blog {
 private int id;
 private int category_id;
 private String title;
 private String content;
 private Date created_time;
  //getter和setter辦法
 @Override
 public String toString() {
  return "Blog [id=" + id + ", category_id=" + category_id + ", title=" + title + ", content=" + content
    + ", created_time=" + created_time + "]";
 } 
}

public class BlogPage {
 private List<Blog> pagerecord;//每頁記載
 private int pageno;//以後頁
 private int pagenostart;//每頁開端索引
 private int pagesize=5;//每頁若干數據
 private int totalrecord;//總記載數
 private int totalpage;//總頁數
 
 public BlogPage(int pageno,int totalrecord){
  //pageno totalrecord都可以當作已有信息
  this.totalrecord=totalrecord;
  //盤算總頁數
  totalpage=(totalrecord%pagesize==0)?totalrecord/pagesize:totalrecord/pagesize+1;
  //pageno的界限處置
  if(pageno<=1)
   this.pageno=1;
  else if(pageno>=totalpage)
   this.pageno=totalpage;
  else
   this.pageno=pageno;
  //盤算每頁開端索引,即每頁第一個數據的索引,用於分頁查詢
  pagenostart=(this.pageno-1)*pagesize;
 }
 public int getPagenostart() {
  return pagenostart;
 }
 public void setPagenostart(int pagenostart) {
  this.pagenostart = pagenostart;
 }
 public List<Blog> getPagerecord() {
  return pagerecord;
 }
 public void setPagerecord(List<Blog> pagerecord) {
  this.pagerecord = pagerecord;
 }
 public int getPageno() {
  return pageno;
 }
 public void setPageno(int pageno) {
  this.pageno = pageno;
 }
 public int getPagesize() {
  return pagesize;
 }
 public void setPagesize(int pagesize) {
  this.pagesize = pagesize;
 }
 public int getTotalrecord() {
  return totalrecord;
 }
 public void setTotalrecord(int totalrecord) {
  this.totalrecord = totalrecord;
 }
 public int getTotalpage() {
  return totalpage;
 }
 public void setTotalpage(int totalpage) {
  this.totalpage = totalpage;
 }
}

 

dao層

JDBCUtil封裝了jdbc的銜接和釋放操作

public class JDBCUtil {
 private static String url = "jdbc:mysql://localhost:3306/blogs_stu";
 private static String username = "root";
 private static String password = "";
 static {
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public static Connection getConnection(){
  Connection conn;
  try {
   conn= DriverManager.getConnection(url, username, password);
   return conn;
  } catch (SQLException e) {
   e.printStackTrace();

  }
  return null;
 }
 public static void release(ResultSet rs,PreparedStatement ps,Connection conn){
  if(rs!=null){
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  if(ps!=null){
   try {
    ps.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  if(conn!=null){
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
}

public class BlogDao {
 //每頁的記載,傳入每頁開端索引和每頁年夜小用於分頁,即limit的兩個參數(mysql分頁用limit)
 public List<Blog> getPageRecord(int pagenostart, int pagesize) {
  Connection conn = JDBCUtil.getConnection();
  PreparedStatement ps = null;
  ResultSet rs = null;
  String sql = "select * from blog limit ?,?";
  List<Blog> list = new ArrayList<Blog>();
  try {
   ps = conn.prepareStatement(sql);
   ps.setInt(1, pagenostart);
   ps.setInt(2, pagesize);
   rs = ps.executeQuery();
   while (rs.next()) {
    Blog blog = new Blog();
    blog.setId(rs.getInt("id"));
    blog.setCategory_id(rs.getInt("category_id"));
    blog.setTitle(rs.getString("title"));
    blog.setContent(rs.getString("content"));
    blog.setCreated_time(rs.getDate("created_time"));
    list.add(blog);
   }
   return list;
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   JDBCUtil.release(rs, ps, conn);
  }
  return null;
 }
 //總記載數
 public int getTotal() {
  Connection conn = JDBCUtil.getConnection();
  PreparedStatement ps = null;
  ResultSet rs = null;
  try {
   ps = conn.prepareStatement("select count(*) from blog");
   rs = ps.executeQuery();
   if (rs.next()) {
    return rs.getInt(1);
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   JDBCUtil.release(rs, ps, conn);
  }
  return 0;
 }
}

service層 

public class BlogService {
 BlogDao blogDao = new BlogDao();
 //前往pagebean,一切分頁須要的信息都去pagebean裡查找
 public BlogPage findPageRecord(int pageno) {
  int totalrecord = blogDao.getTotal();
  BlogPage blogpage = new BlogPage(pageno, totalrecord);
  List<Blog> list = blogDao.getPageRecord(blogpage.getPagenostart(),blogpage.getPagesize());
  blogpage.setPagerecord(list);
  return blogpage;
 }
}

servlet類 

@WebServlet("/BlogSplitServlet")
public class BlogSplitServlet extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("UTF-8");
  response.setContentType("text/html; charset=utf-8");
  String pagenostr=request.getParameter("pageno");
  //初次拜訪servletpagenostr為null,給一個初始值,即默許拜訪第一頁
  int pageno=1;
  if(pagenostr!=null)
   pageno=Integer.parseInt(pagenostr);
  BlogService service=new BlogService();
  BlogPage blogPage=service.findPageRecord(pageno);
  request.setAttribute("blogPage", blogPage);
  request.getRequestDispatcher("/blogPage.jsp").forward(request, response);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doGet(request, response);
 }
}

如許一切的分頁信息就封裝到pagebean裡了 

jsp完成只須要將pagebean裡的信息掏出來就好了 

上面給出我的jsp完成(模仿ligerUI) 

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@page import="java.util.*,model.Blog,model.BlogPage"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript">
 window.onload = function() {
  //包管select的option與以後頁顯示分歧
  select = document.getElementById("select");
  pageno = '${blogPage.pageno}';
  select.options[pageno - 1].selected = 'selected';
 }
 //select下拉列表跳轉
 function selectjump() {
  var pageno = select.selectedIndex + 1;
  window.location.href = "http://localhost/jspPageSplit/BlogSplitServlet?pageno="
    + pageno;
 }
 //text跳轉,onblur事宜,輸出框掉去核心是產生
 function textjump() {
  var pageno = document.getElementById("text").value;
  window.location.href = "http://localhost/jspPageSplit/BlogSplitServlet?pageno="
    + pageno;
 }
</script>
</head>
<body>
 <%
  BlogPage blogPage = (BlogPage) request.getAttribute("blogPage");
  List<Blog> list = blogPage.getPagerecord();
  // 尾頁填充空白行,若不填充,尾頁表格tr行數與後面紛歧致很好看
  if (list.size() < blogPage.getPagesize()) {
   for (int i = list.size(); i < blogPage.getPagesize(); i++)
    list.add(null);
  }
 %>
 <div >
  <table border="1" cellspacing="0" width="100%" bgcolor="#CEF0C5">
   <tr height="40px">
    <td>id</td><td>題目</td><td>內容</td><td>創立時光</td>
   </tr>
   <%
    for (Blog blog : list) {
     if (blog != null) {
   %>
   <tr height="50px">
    <td width="10%"><%=blog.getId()%></td>
    <td width="20%"><%=blog.getTitle()%></td>
    <td width="40%"><%=blog.getContent()%></td>
    <td width="30%"><%=blog.getCreated_time()%></td>
   </tr>
   <!-- 尾頁空白行填充 -->
   <%} else {%>
   <tr height="50px">
    <td width="10%"></td>
    <td width="20%"></td>
    <td width="40%"></td>
    <td width="30%"></td>
   </tr>
   <%}}%>
  </table>
  <div >
    
  <!-- select下拉框 -->
  <select id="select">
   <%for (int i = 1; i <= blogPage.getTotalpage(); i++) {%>
   <option onclick="selectjump()"><%=i%></option>
   <%}%>
  </select> 
  <a href="${pageContext.request.contextPath}/BlogSplitServlet?pageno=1">首頁</a>
  <a href="${pageContext.request.contextPath}/BlogSplitServlet?pageno=<%=blogPage.getPageno()-1<1?blogPage.getPageno():blogPage.getPageno()-1%>">上一頁</a>  
  <input type="text" id="text" size="1px" value="${blogPage.pageno}" onblur="textjump()">/${blogPage.totalpage} 
  <a href="${pageContext.request.contextPath}/BlogSplitServlet?pageno=<%=blogPage.getPageno()+1>blogPage.getTotalpage()?blogPage.getPageno():blogPage.getPageno()+1%>">下一頁</a>
  <a href="${pageContext.request.contextPath}/BlogSplitServlet?pageno=<%=blogPage.getTotalpage()%>">尾頁</a>
  <div >
  顯示從${blogPage.pagenostart+1}到${blogPage.pageno==blogPage.totalpage?blogPage.totalrecord:blogPage.pagesize},
  共${blogPage.totalrecord}條. 每頁顯示${blogPage.pagesize}條
  </div>
  </div>
 </div>
</body>
</html>

這是最初的模樣,款式粗略的調了下,功效跟ligerUI默許的分頁如出一轍 

將JSP中代碼改成標簽(JSTL,需引入響應的jar包)並將JSP中的尾頁補白放在servlet中後 

servlet中參加 

// 尾頁填充空白行,若不填充,尾頁表格tr行數與後面紛歧致很好看
  List<Blog> list = blogPage.getPagerecord();
  if (list.size() < blogPage.getPagesize()) {
   for (int i = list.size(); i < blogPage.getPagesize(); i++)
    list.add(null);
  }
  blogPage.setPagerecord(list);

jsp頁面 

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@page import="java.util.*,model.Blog,model.BlogPage"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
<script type="text/javascript">
 //select下拉列表跳轉
 function selectjump() {
  var select = document.getElementById("select");
  var pageno = select.selectedIndex + 1;
  window.location.href = "http://localhost/jspPageSplit/BlogSplitServlet?pageno="
    + pageno;
 }
 //text跳轉,onblur事宜,輸出框掉去核心時產生
 function textjump() {
  var pageno = document.getElementById("text").value;
  window.location.href = "http://localhost/jspPageSplit/BlogSplitServlet?pageno="
    + pageno;
 }
</script>
</head>
<body>
 <div >
  <table border="1" cellspacing="0" width="100%" bgcolor="#CEF0C5">
   <tr height="40px">
    <td>id</td><td>題目</td><td>內容</td><td>創立時光</td>
   </tr>
   <c:forEach items="${blogPage.pagerecord}" var="c" varStatus="vs">
   <c:if test="${c!=null}">
      <tr height="50px">
    <td width="10%">${c.id}</td>
    <td width="20%">${c.title}</td>
    <td width="40%">${c.content}</td>
    <td width="30%">${c.created_time}</td>
   </tr>
   </c:if>
   <!-- 尾頁空白行填充 -->
   <c:if test="${c==null}">
   <tr height="50px">
    <td width="10%"></td>
    <td width="20%"></td>
    <td width="40%"></td>
    <td width="30%"></td>
   </tr>
   </c:if>
   </c:forEach>
  </table>
  <div >
    
  <!-- select下拉框 -->
  <select id="select">
  <c:forEach begin="1" end="${blogPage.totalpage}" var="i">
  <option value="${i}" onclick="selectjump()" ${blogPage.pageno==i?'selected="selected"':''}>${i}</option>
  </c:forEach>
  </select> 
  <a href="${pageContext.request.contextPath}/BlogSplitServlet?pageno=1">首頁</a>
  <a href="${pageContext.request.contextPath}/BlogSplitServlet?pageno=${blogPage.pageno-1<1?blogPage.pageno:blogPage.pageno-1}">上一頁</a>  
  <input type="text" id="text" size="1px" value="${blogPage.pageno}" onblur="textjump()">/${blogPage.totalpage} 
  <a href="${pageContext.request.contextPath}/BlogSplitServlet?pageno=${blogPage.pageno+1>blogPage.totalpage?blogPage.pageno:blogPage.pageno+1}">下一頁</a>
  <a href="${pageContext.request.contextPath}/BlogSplitServlet?pageno=${blogPage.totalpage}">尾頁</a>
  <div >
  顯示從${blogPage.pagenostart+1}到${blogPage.pageno==blogPage.totalpage?blogPage.totalrecord:blogPage.pagesize},
  共${blogPage.totalrecord}條. 每頁顯示${blogPage.pagesize}條
  </div>
  </div>
 </div>
</body>
</html>

 現實應用中可以依據需求編寫jsp頁面,然則後台代碼根本是通用的 


以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved