List. jsp:
<Div class = "right_content">
<H2 class = "bulletin_h2"> system announcement <Div class = "bulletin_content">
<Ul>
<C: forEach items = "$ {notices}" var = "notice">
<Li> <em >>></em> <a href = "getnecontent? Id =$ {notice. id} ">$ {notice. title} </a>
<Span> Delete </span>
<Span> edit </span>
<Span >$ {notice. pubtime} </span>
</Li>
</C: forEach>
</Ul>
<C: if test = "$ {currentPage! = 1} ">
<A href = "noticelist? Page =$ {currentPage-1} "> Previous </a>
</C: if>
<C: forEach begin = "1" end = "$ {noOfPages}" var = "I">
<C: choose>
<C: when test = "$ {currentPage eq I}">
<Td >$ {I} </td>
</C: when>
<C: otherwise>
<Td> <a href = "noticelist? Page =$ {I} ">$ {I} </a>
</Td>
</C: otherwise>
</C: choose>
</C: forEach>
<C: if test = "$ {currentPage lt noOfPages}">
<Td> <a href = "noticelist? Page =$ {currentPage + 1} "> Next </a>
</Td>
</C: if>
</Div>
</Div>
List. java:
Public ArrayList <Notice> getNotices (int offset, int recordsPerPage ){
ResultSet rs = null;
PreparedStatement ps = null;
ArrayList <Notice> notices = new ArrayList <Notice> ();
Connection conn = JdbcUtils. getConnection ();
String SQL = "select SQL _CALC_FOUND_ROWS id, title, content, person, filepath, pubtime from notice order by id desc limit ?,? ";
Try {
Ps = conn. prepareStatement (SQL );
Ps. setInt (1, offset );
Ps. setInt (2, recordsPerPage );
Rs = ps.exe cuteQuery ();
While (rs. next ()){
Notice notice = new Notice ();
Notice. setId (rs. getInt ("id "));
Notice. setTitle (rs. getString ("title "));
Notice. setContent (rs. getString ("content "));
Notice. setPerson (rs. getString ("person "));
Notice. setFilepath (rs. getString ("filepath "));
Notice. setPubtime (rs. getString ("pubtime "));
Notices. add (notice );
}
Rs. close ();
Rs = ps.exe cuteQuery ("SELECT FOUND_ROWS ()");
If (rs. next ())
This. noOfRecords = rs. getInt (1 );
} Catch (Exception e ){
E. printStackTrace ();
}
Return notices;
}
Servlet:
// Handle garbled characters
Request. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = gbk ");
Int page = 1;
Int recordsPerPage = 10;
If (request. getParameter ("page ")! = Null)
Page = Integer. parseInt (request. getParameter ("page "));
NoticeDAO nd = new NoticeDAOImpl ();
ArrayList <Notice> notices = nd. getNotices (page-1) * recordsPerPage, recordsPerPage );
Int noOfRecords = nd. getNoOfRecords ();
Int noOfPages = (int) Math. ceil (noOfRecords * 1.0/recordsPerPage );
Request. setAttribute ("notices", notices );
Request. setAttribute ("noOfPages", noOfPages );
Request. setAttribute ("currentPage", page );
Request. getRequestDispatcher ("noticelist. jsp"). forward (request, response );
References: http://www.cnblogs.com/super119/archive/2011/01/13/1934997.html QQ group: 127402101. Modify the remarks when you enter the group. |