1. 程式人生 > >JSP分頁c:foreach分頁程式碼以及效果新聞釋出系統

JSP分頁c:foreach分頁程式碼以及效果新聞釋出系統

我們實訓周做了一個新聞釋出系統,也就是今天2015年10月10號週六,剛過完國慶,今天加課,我做了新聞類別的增刪改查,耗費了倆個小時做出來了分頁,值得讓我記錄一下,挺費勁的
下面是實現的JSP用到c: foreach的標籤庫結構實現分頁

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="bean.*" %>
<%@ page language="java" import="dal.*" %>
<%@ page language="java"
import="dao.*" %>
<%@ page language="java" import="util.*" %> <%@ page language="java" import="java.io.*" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()+path+"/"; %>
<!-- 測試的網址是: http://localhost:8080/News_release_system/news_type/test.jsp --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>類別新聞 的增刪改查</title> <meta http-equiv="pragma" content="no-cache"
>
<meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css" > body{ text-align: center; } table{ text-align:center; border-collapse:collapse; } .font{ font-family:"華文新魏"; font-size: 18px; } </style> </head> <body class="font"> <% ArrayList<NRC_TYPE> list=new ArrayList<NRC_TYPE>(); News_type_Dao type=new News_type_Dao(); NRC_TYPE typebean=new NRC_TYPE(); list=type.queryall(); pageContext.setAttribute("list", list); int count=0; //總行數 int page_count=1; //開始條數 int page_total=1; //,總頁碼 int page_current= 1; //首頁 int page_size=20;//一頁的行數 %> <br><h1>類別新聞</h1> <% String page_cu = request.getParameter("page_current"); if(page_cu==null){ page_cu="1"; } page_current = Integer.parseInt(page_cu); if(page_current<0){ page_current = 1; } page_count=page_count+ page_current*page_size; %> <table align="center" width="100%" border="3px" bordercolor="#OOO" cellspacing="5px"> <tr> <th >新聞類別序號</th> <th>新聞類別名稱</th> <th>新聞類別詳述</th> <th nowrap="nowrap">新聞類別具體操作</th> </tr> <c:forEach items="${list}" var="typebean" step="1" varStatus="i" begin="<%=page_count%>" end="<%=page_size+page_count-1%>"> <tr> <td>${i.index}</td> <td>${typebean.getT_NAME()}</td> <td>${typebean.getT_MEMO()}</td> <td nowrap="nowrap">&nbsp;&nbsp; <a href="<%=path %>/dao/News_servlet?method=delete&&ID=<%=(list.get(count+1).getT_ID())%>"> 刪除</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="<%=path %>/news_type/add.jsp"> 新增</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="<%=path %>/dao/News_servlet?method=update&&ID=<%=(list.get(count+1).getT_ID())%>"> 修改</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="<%=path %>/dao/News_servlet?method=detail&&ID=<%=(list.get(count+1).getT_ID())%>"> 詳情</a>&nbsp;&nbsp;</td> <% count++; %> </tr> </c:forEach> <%-- <% page_size=page_count+page_size; %> --%> <tr> <td colspan="2" align="right"> 本頁總共<%=count%>條 &nbsp; &nbsp; &nbsp; &nbsp; 總共<%=list.size()%></td> <% page_total = ((list.size())%20==0)?((list.size())/20):((list.size())/20+1); %> <td colspan="2" align="center"> <a href = "page.jsp?page_current=1" >首頁</a> <a href = "page.jsp?page_current=<%=page_current-1%>" >上一頁</a> <a href = "page.jsp?page_current=<%=page_current+1%>" >下一頁</a> <a href = "page.jsp?page_current=<%=page_total%>" >尾頁</a><%=page_current+1%> 頁/共 <%=page_total%></td> </tr> <%-- <% if(count==0){ request.getRequestDispatcher("/news_type/test.jsp").forward(request, response); } %> --%> </table> </body> </html>

效果一

效果二
分頁三