이 기사는 (1) 총 페이지 수를 계산하기 위해 두 가지 방법을 사용합니다. (2) 지정된 페이지 데이터를 쿼리하여 간단한 페이징 효과를 달성합니다.
아이디어 : 먼저 DAO 객체에 Pagination Query 메소드를 제공해야합니다. 지정된 페이지의 데이터를 찾으려면 제어 계층 에서이 메소드를 호출하고 프레젠테이션 레이어에서 EL 표현식 및 JSTL을 통해 페이지의 데이터를 표시하십시오.
먼저 렌더링을 보여 드리겠습니다.
오프 주제 : 이 페이지 디스플레이는 "프리젠 테이션 레이어 제어 레이어 DAO 레이어-다베이스"의 설계 아이디어를 사용하여 구현됩니다. 개선 사항이 있으면 제안하고 함께 배우고 함께 진행하십시오. 더 이상 고민하지 않고 주제를 입력하기 시작하십시오. 자세한 단계는 다음과 같습니다.
1. DAO 층-다타베이스
JDBcutils 클래스는 데이터베이스를 열고 닫는 데 사용되며 핵심 코드는 다음과 같습니다.
Java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; public class jdbcutils {private connection conn = null; private prepartement pstmt = null; /*** 데이터베이스에 연결* @return*/public connection connect () {String user = "root"; String Password = "1234"; String DriverClass = "com.mysql.jdbc.driver"; String JDBCURL = "JDBC : MySQL : // localHost : 3306/book"; class.forn (drivemas); DriverManager.getConnection (JDBCURL, 사용자, 암호);} CATCH (예외 E) {// TODO 자동 생성 CATCKK.PRINTSTACKTRACE ();} return Conn;}/*** 데이터베이스를 닫습니다* @Param Conn* @param pstmt* @param resu*/public void close (연결, 결과) null) {try {conn.close ();} catch (sqlexception e) {// todo 자동 생성 캐치 블록}} if (pstmt! = null) {try {pstmt.close ();} catch (sqlexception e) {// todo auto-Generated blocke.printstacktrace (); {result.close ();} catch (sqlexception e) {// todo auto-auto-auto-receated catch blocke.printstacktrace ();}}}}userdao 클래스의 메소드 getPage () 및 메소드 ListUser ()는 각각 총 페이지 수를 계산하고 지정된 페이지의 데이터를 각각 쿼리하는 데 사용됩니다. 핵심 코드는 다음과 같습니다.
import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import com.db.jdbcutils; public class userdao; @return*/public int getPage () {int recordCount = 0, t1 = 0, t2 = 0; proadStatement pstmt = null; resultSet result = null; jdbcutils jdbc = new jdbcutils (); connection conn = jdbc.connect (); string sql = "select"; {pstmt = conn.preparestatement (sql); result = pstmt.executeQuery (); result.next (); recordCount = result.getInt (1); t1 = recordCount%5; t2 = recordCount/5;} catch (예외 e) {// auto-Generated Blocke.printStacktrace (); 마지막으로 {jdbc.close (conn, pstmt, result);} if (t1! = 0) {t2 = t2+1;} return t2;}/*** 지정된 페이지의 데이터를 쿼리*/public list*/public list <user> listuser (int pageno) {repordstmt = null = null = null = null gromate; null rist <user <user> listuser (int pageno); ArrayList <user> (); int pagesize = 5; int page = (pageno-1) * 5; jdbcutils jdbc = new jdbcutils (); connection conn = jdbc.connect (); String sql = "id 한계에 의한 책에서 선택 * 선택 * {pstmt = conn.setint (sql); page); pstmt.setint (2, pagesize); result = pstmt.executeQuery (); while (result.next ()) {user user = new user (); user.setId (result.getInt (1)); user.setName (restString (2)); user.setNumber (3); hist.}}}}}}}}} todo 자동 생성 캐치 블록 .printstacktrace ();} 마침내 {jdbc.close (Conn, PSTMT, Result);} 리턴리스트;}}사용자 클래스는 쿼리 데이터를 저장하는 데 사용되며 핵심 코드는 다음과 같습니다.
공개 클래스 사용자 {private int id; 개인 문자열 이름; 개인 문자열 번호; public int getId () {return id;} public void setId (int id) {this.id = id;} public string getName () {return name;} public void setName (String Name) {this.name = name;} public string getNumber (thist.) {thisnumber (this. 숫자;}} 2. 제어 레이어
ListUser 클래스는 userDAO 객체를 호출하여 데이터를 쿼리하고 데이터를 표시하도록 페이지를 할당합니다. 핵심 코드는 다음과 같습니다.
import java.io.ioexception; import java.io.printwriter; import java.util.arraylist; import java.util.list; import javax.servlet.servletexception; import javax.servlet.http.httpervlet; import javax.servlet.httplet javax.servlet.http.http.httpervletresponse; import com.dao.user; import com.dao.userdao; public class listuser extends httpservlet {public listuser ();} public void destroy () {super.destroy (); // 로그에 "파괴"문자열을 로그에 넣습니다. // 코드를 여기에 넣습니다. {response.setcharacterencoding ( "utf-8"); int pageno = 1; userdao userdao = new userDao (); list <user> lists = new arraylist <user> (); String pageno = request.getParameter ( "pageno! = if (pageno! = null) {pageno = integer.parseint (pageno);} lists = userdao.listuser (pageno); int recordCount = userDao.getPage (); request.SetAttribute ( "RecordCount", request.get.get.get.get.get.setattribute ( "lists"; rests); pageno); request.getRequestDispatcher ( "userList.jsp"). Forward (요청, 응답);} public void init ()가 servletexception {// 코드를 여기에 넣습니다}} 3. 발현층
출력 페이지 userList.jsp는 EL 및 JSTL을 사용하여 쿼리 결과를 출력하는 데 사용됩니다. 핵심 코드는 다음과 같습니다.
<%@ page language = "java"import = "java.util.*"pageencoding = "utf-8"%> <%@ taglib prefix = "c"uri = "http://java.sun.com/jsp/jstl/core"%> <%@ taglib uri = "http://java.sun.sun.sun.sun.sun.sun.sun.sun.sun.sun.sun.sun.suncore. Prefix = "fmt"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/functions"prefix = "fn"%> <%string path = request.getContextPath (); String BasePath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/";%> <! doctype html public "-// w3c // dtd html 4.01 transitional // en"> <html> <head href = <%= bysepate> "> 'userList.jsp'시작 페이지 </title> <meta http-equiv = "pragma"content = "no-cache"> <meta http-equiv = "cache-control"content = "no-cache"> <meta http-equiv = "content ="0 "> <meta http-equiv ="키워드 " content = "keyword1, keyword2, keyword3"> <meta http-equiv = "description"content = "이것은 내 페이지입니다"> <!-<link rel = "stylesheet"type = "text/css"href = "styles.css">-> <style type = "text/css"> th, td {텍스트 그레이 : 150px Solid Gray; 중심;} body {text-align : center;} a {text-decoration : none;} table {border-collapse : collapse;} </style> </head> <hod> <h2 align = "center"> book information </h2> <table align = "center"> <t> <td> book number </td> <td> book title </td> <td> 인벤토리 </td> </tr> </tab> </table> <table align = "center"> <c : foreach items = "$ {lists}"var = "person"> <td> <td> $ {person.id} </td> <td> $ {person.name} </td> $ {perient..number }</td></tr></c:forEach></table><br><c:if test="${pageNos>1 }"><a href="ListUser?pageNos=1" >Home</a><a href="ListUser?pageNos=${pageNos-1 }">Previous page</a></c:if><c:if test="${pageNos <recordCount} "> <a href ="href = "listUser? pagenos = $ {pagenos+1}"> 다음 페이지 </a> </a> </a> </a> </c : if> <form action = "listuser"> <h4 align = "center"> total $ {recordCount} page <input type = "text"value = "$ {pagenos}"pagenos "pagenos" "pagenos" 타입 = "제출"값 = "도착"> </h4> </form> </body> </html>위는 편집자가 소개 한 Java 웹의 간단한 페이지 매김 표시 예제 코드입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!