测试

测试测试测试

Entity

public class Entry {

    private int entryID;

    private boolean asDraft;

    private String title;

    private String content;

    

    

    /**

     * For further extension

     */

    private boolean setTop;

    

    /**

     * For further extension, make it easy to be searched by google

     */

    private String staticLinkURL;



    public int getEntryID() {

        return entryID;

    }



    public void setEntryID(int entryID) {

        this.entryID = entryID;

    }



    public boolean isAsDraft() {

        return asDraft;

    }



    public void setAsDraft(boolean asDraft) {

        this.asDraft = asDraft;

    }



    public String getTitle() {

        return title;

    }



    public void setTitle(String title) {

        this.title = title;

    }



    public String getContent() {

        return content;

    }



    public void setContent(String content) {

        this.content = content;

    }



    

    public boolean isSetTop() {

        return setTop;

    }



    public void setSetTop(boolean setTop) {

        this.setTop = setTop;

    }



    public String getStaticLinkURL() {

        return staticLinkURL;

    }



    public void setStaticLinkURL(String staticLinkURL) {

        this.staticLinkURL = staticLinkURL;

    }

    

    

}

Servlet

import java.io.IOException;

import java.util.List;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



/**

 * Servlet implementation class PostsServlet

 */

public class PostsServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public PostsServlet() {

        super();

        // TODO Auto-generated constructor stub

    }



    /**

     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

     */

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String pageNumberStr = request.getParameter("pageNumber");

        int pageNumber = 1;

        if(pageNumberStr!=null && !pageNumberStr.isEmpty())

        {

            pageNumber = Integer.parseInt(pageNumberStr);

        }

        

        int pageSize = 10; //分页大小

        int totalPosts = PagingDAO.entryList.size(); //总文章数

        int totalPages = totalPosts/pageSize + ((totalPosts%pageSize)>0?1:0); //计算得出的总页数

        

        request.setAttribute("pageSize", pageSize);

        request.setAttribute("totalPosts", totalPosts);

        request.setAttribute("pageNumber", pageNumber);

        request.setAttribute("totalPages", totalPages);

        

        List<Entry> entryList = PagingDAO.getEntryList(pageNumber, pageSize);

        System.out.println("entryList:"+ entryList.size());

        request.setAttribute("entryList", entryList);

        

        request.getRequestDispatcher("index.jsp").forward(request, response);        

    }



}

dao

import java.util.ArrayList;

import java.util.List;



public class PagingDAO {



    public static List<Entry> entryList = new ArrayList<Entry>();



    static {

        for (int i = 0; i < 116; i++) {

            Entry e = new Entry();

            e.setEntryID(i);

            e.setTitle("The title for the article " + i + " ");

            entryList.add(e);

        }

    }



    public static List<Entry> getEntryList(int pageNum, int pageSize) {

        System.out.println(pageNum+";"+pageSize);

        List<Entry> ret = new ArrayList<Entry>();

        int start = (pageNum - 1) * pageSize;

        int end = start + pageSize - 1;

        System.out.println(start+"-"+end);

        if (start >= entryList.size())

            return ret;

        

        for (int i = start; i <= end; i++) {

            if (i < entryList.size())

                ret.add(entryList.get(i));

        }

        

        return ret;

    }

}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



page Size : ${pageSize}

<br />

Total Posts: ${totalPosts}

<br />

Total Pages: ${totalPages}

<br />

Current Page: ${pageNumber}

<hr />



<table>

    <thead>

        <tr align="center">

            <td width="10%">Article ID</td>

            <td width="70%">Article Title</td>

            <td colspan="3">Actions</td>

        </tr>

    </thead>

    <tbody>

        <c:forEach items="${entryList}" var="entry">

            <tr align="center">

                <td>${entry.entryID}</td>

                <td>${entry.title}</td>

                <td><a href="viewEntry?entryID=${entry.entryID}">View</a></td>

                <td><a href="editEntry?entryID=${entry.entryID}">Edit</a></td>

                <td><a href="deleteEntry?entryID=${entry.entryID}">Delete</a></td>

            </tr>

        </c:forEach>

    </tbody>

    <tfoot>

        <tr align="center">

            <td colspan="5">

                <jsp:include page="paging_footer.jsp"></jsp:include>

            </td>

        </tr>

    </tfoot>

</table>



<hr/>

paging_footer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



<script type="text/javascript">

function gotoSelectedPage()

{

    var x = document.getElementById("navigatorForm");

    //alert("Original action: " + x.action)

    x.submit();

}

</script>

<form action="Posts" method="get" id="navigatorForm">

    <a href="Posts?pageNumber=1">首页</a> 

    <c:if test="${pageNumber>1}">

        <a href="Posts?pageNumber=${pageNumber-1}">上一页</a>

    </c:if> 

    跳转到第 <select name="pageNumber" onchange="gotoSelectedPage();">

    <c:forEach begin="1" end="${totalPages}" step="1" var="pageIndex">

        <c:choose>

            <c:when test="${pageIndex eq pageNumber}">

                <option value="${pageIndex}" selected="selected">${pageIndex}</option>

            </c:when>

            <c:otherwise>

                <option value="${pageIndex}">${pageIndex}</option>

            </c:otherwise>

        </c:choose>

    </c:forEach>

    </select><c:if test="${pageNumber<totalPages}">

        <a href="Posts?pageNumber=${pageNumber+1}">下一页</a>

    </c:if> 

    <a href="Posts?pageNumber=${totalPages}">末页</a>

</form>

 

你可能感兴趣的:(测试)