Jspにおけるページング機能の実現
ページングクエリ機能はウェブプログラミングでよく用いられる技術であるが、再利用可能で簡単なページング技術をどのように実現するか、以下のコードはいくつかの参考を提供し、ユーザーリストのページング表示を実現することができ、他のデータがページング表示を必要とする場合、その中のページングオブジェクト(SplitPage.java)を多重化し、daoインタフェースを実現するクラスを提供することができる.この例のインタフェースクラスとページファイルのリストを先にリストします.
1.データベース接続オブジェクト:DBConnection、データ接続オブジェクトgetConnection()を取得する.
2.ページング(クラス)オブジェクト:SplitPage.JAva、ページパラメータ(トップページ/末尾ページ/現在nページ目など)を提供する
3.データベースアクセスインタフェース:dao、業務ロジックにおけるデータベース操作インタフェースを提供し、ページング検索のためのインタフェース方法は:
findAll(SplitPage sp)/メソッドの入力パラメータはページングオブジェクト
getRows()/合計レコード数の提供
UserDao.JAvaはこのインタフェースを実現するために用いる.
4.ユーザーJavaBean:User.JAvaは、ユーザリストをページング表示する際のユーザオブジェクトである.
5.userList.jsp:ユーザリストjspページをページング表示する.
============================================================
詳細は次のとおりです.
------------------------------------------------------------
1.データベース接続オブジェクトについて、次の方法でdaoにConnectionオブジェクトを提供する
//データベース接続の取得
------------------------------------------------------------
2.ページングオブジェクトの実装:
------------------------------------------------------------
3.UserDao.JAvaでのインタフェース実装方法findAll(SplitPage sp)
データセットのページングリストを提供する.この方法が実現すれば,いつも正しく提供できる.
ページごとにデータを表示する.
//ページング
------------------------------------------------------------
4.残りの仕事はjspにユーザーリストをページングすることであり、ページングブラウズが必要である
表示するデータはすべてdaoとSplitPageオブジェクトで実現できる.
1.データベース接続オブジェクト:DBConnection、データ接続オブジェクトgetConnection()を取得する.
2.ページング(クラス)オブジェクト:SplitPage.JAva、ページパラメータ(トップページ/末尾ページ/現在nページ目など)を提供する
3.データベースアクセスインタフェース:dao、業務ロジックにおけるデータベース操作インタフェースを提供し、ページング検索のためのインタフェース方法は:
findAll(SplitPage sp)/メソッドの入力パラメータはページングオブジェクト
getRows()/合計レコード数の提供
UserDao.JAvaはこのインタフェースを実現するために用いる.
4.ユーザーJavaBean:User.JAvaは、ユーザリストをページング表示する際のユーザオブジェクトである.
5.userList.jsp:ユーザリストjspページをページング表示する.
============================================================
詳細は次のとおりです.
------------------------------------------------------------
1.データベース接続オブジェクトについて、次の方法でdaoにConnectionオブジェクトを提供する
//データベース接続の取得
public class DBUtil {
protected static Connection cnt = null;
protected static Statement stmt;
protected static ResultSet rs;
protected static String dbClassName = "com.mysql.jdbc.Driver";
/*
*
*/
protected DBUtil() {
if (cnt == null) {
try {
Class.forName(dbClassName);
cnt = DriverManager
.getConnection("jdbc:mysql://localhost/youdbname?"
+ "user=root&password=key");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException s) {
System.out.print(" ");
s.printStackTrace();
}
} else
return;
}
/*
*
*/
public static ResultSet executeQuery(String sql) {
try {
if (cnt == null)
new DBUtil();
return (cnt.createStatement().executeQuery(sql));
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
}
}
/*
* ( )
*/
public static int executeUpdate(String sql) {
try {
if (cnt == null)
new DBUtil();
return cnt.createStatement().executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
return -1;
} finally {
}
}
/*
* ,
*/
public static void CloseAll() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (cnt != null) {
cnt.close();
cnt = null;
}
} catch (SQLException ac) {
ac.printStackTrace();
}
}
}
------------------------------------------------------------
2.ページングオブジェクトの実装:
public class SplitPage {
// ,
final public static String FIRSTPAGE="first";//
final public static String PREVIOUSEPAGE="previous";//
final public static String NEXTPAGE="next";//
final public static String LASTPAGE="last";//
private int pageRows=3;// , 3 ,
private int totalRows=0;// , dao
private int currentPage=1;// ,
private int firstPage=1;// ,
private int totalPages=1;// ,
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getPageRows() {
return pageRows;
}
public void setPageRows(int pageRows) {
if(pageRows==0)throw new ArithmeticException();
this.pageRows = pageRows;// pageRows , .
// , ,
this.totalPages=(this.totalRows%this.pageRows==0)?this.totalRows/this.pageRows:this.totalRows/this.pageRows+1;
}
public int getTotalRows() {
return totalRows;
}
// , ,
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
// ( ), , , , 1.
this.totalPages=(this.totalRows%this.pageRows==0)?this.totalRows/this.pageRows:this.totalRows/this.pageRows+1;
}
// ,
// .
public int getTotalPages() {
return totalPages;
}
// ,
// , .
public int confirmPage(String flag){
int newPage=this.currentPage;
if(flag!=null){//flag
if(flag.equals(SplitPage.FIRSTPAGE)){
newPage=1;
}else if(flag.equals(SplitPage.LASTPAGE)){
newPage=this.totalPages;
}else if(flag.equals(SplitPage.NEXTPAGE)){
// , ,
newPage=(this.totalPages==this.currentPage)?this.currentPage:this.currentPage+1;
}else if(flag.equals(SplitPage.PREVIOUSEPAGE)){
// , ,
newPage=(this.firstPage==this.currentPage)?this.currentPage:this.currentPage-1;
}else{//
int tpage=Integer.parseInt(flag.trim());
newPage=tpage;
}
}else{// ,
newPage=this.currentPage;
}
//
this.setCurrentPage(newPage);
return newPage;
}
}
------------------------------------------------------------
3.UserDao.JAvaでのインタフェース実装方法findAll(SplitPage sp)
データセットのページングリストを提供する.この方法が実現すれば,いつも正しく提供できる.
ページごとにデータを表示する.
//ページング
public class UserDao{
public int getRows(){
int num = 0;
String sql = "Select count(*) From t_user";
ResultSet rs = DBUtil.executeQuery(sql);
try {
while(rs.next()){
num += rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(num);
return num;
}
public List<User> findAll(SplitPage sp) {
List<User> list = new ArrayList<User>();
//
//Connection conn = factory.getConnection();
//Statement st = null;
// ResultSet rs = null;
// sqlserver sql , SplitPage
// MySQL "select * from user limit " + sp.getPageRows()*
//(sp.getCurrentPage()-1) +"," + sp.getPageRows();
// mysql, mysql
String sql = "select * from t_user limit "+ sp.getPageRows()*(sp.getCurrentPage()-1) +"," + sp.getPageRows() ;
//String sql = "select top "+sp.getPageRows()+" * from t_user where id " +
// " not in (select top ("+sp.getPageRows()*(sp.getCurrentPage()-1)+") id " +" from t_user order by id) order by id";
try {
// st = conn.createStatement();
ResultSet rs = DBUtil.executeQuery(sql);
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTitle(rs.getString("title"));
user.setAddress(rs.getString("email"));
user.setEmail(rs.getString("address"));
user.setPhoto(rs.getString("photo"));
user.setMypage(rs.getString("mypage"));
user.setContent(rs.getString("content"));
user.setPdate(rs.getString("pdate"));
user.setAsk(rs.getString("ask"));
user.setQQ(rs.getString("QQ"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.CloseAll();
}
return list;
}
}
------------------------------------------------------------
4.残りの仕事はjspにユーザーリストをページングすることであり、ページングブラウズが必要である
表示するデータはすべてdaoとSplitPageオブジェクトで実現できる.
<html>
<head>
<title>My JSP 'userList.jsp' starting page</title>
//
//
<script type="text/javascript">
function go(){
var goPage=document.all.selectpage.value;
alert(" :userList.jsp?flag="+goPage);
document.open("userList.jsp?flag="+goPage,"_self","");
}
</script>
</head>
<%--
spage , , session
.
--%>
<jsp:useBean id="spage" class="com.accp.zl.util.SplitPage" scope="session"></jsp:useBean>
<jsp:useBean id="dao" class="com.accp.zl.dao.UserDao" scope="session"></jsp:useBean>
<%
// , SplitPage
String flag=request.getParameter("flag");
// ,
//
int totalRows=dao.getRows();//
spage.setTotalRows(totalRows);
// , ,
//
int currentPage=spage.confirmPage(flag);
spage.setPageRows(3); , 3
spage.setTotalRows(totalRows);
%>
<body>
<center>
<p><h2> , </h2>
<br>
<table border="1">
<thead>
<th> </th><th> </th><th> </th><th> </th><th> </th>
</thead>
<%
// , session
List<User> list=dao.findAll(spage);
for(User u:list){
%>
<tr>
<td><%=u.getId() %></td>
<td><%=u.getUserName() %></td>
<td><%=u.getBirthday() %></td>
<td><%=u.getInterest() %></td>
<td><a href='modify.jsp?userid=<%=u.getId() %>'> </a><a href='delete.jsp?userid=<%=u.getId() %>'> </a></td>
</tr>
<%} %>
<tr>
<td colspan="5" align="right">
<%-- ,
.
--%>
【
<a href="userList.jsp?flag=<%=SplitPage.FIRSTPAGE%>"> </a>
<a href="userList.jsp?flag=<%=SplitPage.PREVIOUSEPAGE %>"> </a>
<a href="userList.jsp?flag=<%=SplitPage.NEXTPAGE %>"> </a>
<a href="userList.jsp?flag=<%=SplitPage.LASTPAGE %>"> </a>
<select id="selectpage" name="goPage" onchange="javascript:go();">
<%
for(int i=1;i<=spage.getTotalPages();i++){
%>
<option value="<%=i%>" <%=(spage.getCurrentPage()==+i)?"selected='selected'":"" %>><%=i%>/<%=spage.getTotalPages()%>
<%}%>
</select>
】
</td>
</tr>
</table>
</center>
</body>
</html>