Java,jsp,javascriptで書かれたページングおよびドロップダウンリストのページング機能


ページdubietyPosition.jsp:
<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ page import="...weihu.vo.DubietyPosition" %>
<%@ page import="...weihu.dao.DubietyPositionDao" %>
<%@ page import="...weihu.util.Page" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="....util.Log" %>

<%
// ...weihu.DubietyPositionDao          
DubietyPositionDao dpDao=null;
//     
List listDP=null;
//     
Page page1=null;
[b]try{
   page1=new Page();
   dpDao=page1.getDao();
   String currentPageNo=request.getParameter("currentPageNo");
   if(currentPageNo!=null){
       page1.refresh(Integer.parseInt(currentPageNo));
   }   
   String pageMethod=request.getParameter("pageMethod");
   if(pageMethod!=null){
       if(pageMethod.equals("first")){
          page1.first();
       }else if(pageMethod.equals("last")){
          page1.last();
       }
   }
   listDP=page1.getCurrentPage(page1.getCurrentPageNo(),page1.getPageSize());

}catch(Exception e){
   Log.error(this, "dubietyPosition.jsp", e.getMessage());
}[/b]
//    
int i=(page1.getCurrentPageNo()-1)*page1.getPageSize()+1;

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<style>

</style>
<link href="/include/WeiHuCSS.css" rel="stylesheet" type="text/css">

<jsp:include page="/public/_nocache.jsp" flush="true" />
<jsp:include page="/public/_head.jsp" flush="true" />
<title>        </title>
...

<script type="text/javascript">
[b]<!--          -->	
function vaildate()   {   
      //var getselect=document.thisForm2.currentPageNo.value;   
     // document.thisForm2.val.value=getselect;   
     // document.thisForm1.submit();  //      
     //      
     window.location.href = "/weihu/Server/dubietyPosition.jsp?currentPageNo=" + document.thisForm1.currentPageNo.value; 
} 
</script>[/b]
</head>
  
 <body bgcolor="#E6E6E6" leftmargin="0" topmargin="0">
     <table width="759" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
       <tr>
	<td>
	   <%@include file="/weihu/inc/serve.jsp"%>
	      <table width="759" border="0" align="center" cellpadding="0" cellspacing="0">
		<tr>
		  <td height="1" bgcolor="#F4F4F4"></td>
		</tr>
		<tr>
		  <td height="23" bgcolor="#E5EAEF" class="td2">
		  <font color="#7B7B7B"><strong>
	           <p style="margin-top:3px">       :      >         </p></strong></font>
		  </td>
		</tr>
	      </table>
	  <form name="thisForm1" method="post" action="dubietyPosition.jsp?currentPageNo=<%=page1.getCurrentPageNo() %>" >
	 <table border="0" cellpadding="0" cellspacing="2" align="center"width="100%">
	<tr>
	  <td colspan="7" class="left">          <input type="checkbox" onclick="toggleAll(this);" checked="checked"/></td>
	</tr>
	<tr>
	         <th width="5%">  </th>
	         <th width="20%">    </th>
		<th width="35%">    </th>
		<th width="12%">    </th>
		<th width="10%">    </th>
		<th width="10%">    </th>
		<th width="8%">  </th>
	</tr>
	<%if(listDP!=null){ %>				
	<%Iterator it=listDP.iterator();
	    while(it.hasNext()){
	            DubietyPosition dp=(DubietyPosition)it1.next();
	%>
	<tr class="tr<%=(i+1)%2%>">
	   <td><b><%=i++ %></b> </td>
	   <td class="left"><%=dp.getPosname() %></td>
	   <td class="left"><%=dp.getComname() %> </td>
	   <td><%=dp.getAddress() %></td>
	   <td><%=dp.getReqDegreeName() %></td>
	  <td><%if(dp.getReqWorkYear()==-100) {%>  
	<%}else{ %>
	<%=dp.getReqWorkYear() %> 
	<%} %>
                </td>
		<td>
		<%if(dpDao.getMapPosState().get(new Integer(dp.getPosId())).equals(new Integer(1))){ %>
               <input type="checkbox" value="<%=dp.getPosId() %>" name="posIds" disabled="disabled"/>
                 <%}else{ %>
   <input type="checkbox" value="<%=dp.getPosId() %>" name="posIds"/>
                          <%} %>
	        </td>
         </tr>
	<tr class="tr<%=i%2%>" id="desc" name="desc">
	<td colspan="7" class="left"><div style="padding:0 15px;">     
		<%=dpDao.getMap().get(new Integer(dp.getPosId())) %></div></td>
		</tr>	
	  <% }%>
	<tr class="right">
	<td colspan="7">
       	<table>
	  <tr>
             <td>
	      <br/> 
		      <%=page1.getCurrentPageNo() %>  ,    <%=page1.getTotalPages() %>
<%if(page1.getCurrentPageNo()!=1){%>				     <a href="dubietyPosition.jsp?pageMethod=first"><u>   </u></a> 
<%} if(page1.getCurrentPageNo()>1){%>			
<a href="dubietyPosition.jsp?currentPageNo=<%=page1.getCurrentPageNo()-1 %>"><u>   </u></a>
<%} if(page1.getCurrentPageNo()<page1.getTotalPages()){%>
<a href="dubietyPosition.jsp?currentPageNo=<%=page1.getCurrentPageNo()+1 %>"><u>   </u></a> 
<%} if(page1.getCurrentPageNo()!=page1.getTotalPages()){%>
<a href="dubietyPosition.jsp?pageMethod=last"><u>    </u></a>
<%} %>

[b]<!--        -->
     
      <select name="currentPageNo"  class="inputAndSelect"  onchange="vaildate()">   
               <%for(int k=1;k<=page1.getTotalPages();k++){ 
                    if(k==page1.getCurrentPageNo()){
               %>
         <option value="<%=k %>" selected="selected"><%=k %></option>
                    <%} else{%>
              <option value="<%=k %>"><%=k %></option>
                  <%}} %>
      </select>[/b]

	</td>
         </tr>
	</table><br/>
	    <input type="checkbox" onclick="selectAll(this)"/><input type="submit" value="       ">			
	</td>
</tr>
		<%}else{ %>
	  <tr><td colspan="7" class="center">       </td> </tr>
<%} %>
</table>
	</form>
	<jsp:include page="../inc/footer.jsp" flush="true" />
</td>
	  </tr>
    </table>		
  </body>
</html>


javascript:
 <script type="text/javascript">  
 <b><!--          -->      
 function vaildate()   {     
       //var getselect=document.thisForm2.currentPageNo.value;     
     // document.thisForm2.val.value=getselect;     
      // document.thisForm1.submit();  //        
      //        
      window.location.href = "/weihu/Server/dubietyPosition.jsp?currentPageNo=" + document.thisForm1.currentPageNo.value;   
}   
</script>

ページ内のformに対応するコード
<select name="currentPageNo"  class="inputAndSelect"  onchange="vaildate()">     
               <%for(int k=1;k<=page1.getTotalPages();k++){   
                     if(k==page1.getCurrentPageNo()){  
                %>  
          <option value="<%=k %>" selected="selected"><%=k %></option>  
                    <%} else{%>  
              <option value="<%=k %>"><%=k %></option>  
                   <%}} %>  
       </select></b> 

------------------------------------
ツールクラスPage.java
package com.huanglq.weihu.util;

import java.util.List;

/**
 *      
 * @author huanglq
 *
 */
public class Page {

	//   
	private int totalRows;
	
	//        ,        20 
	private int pageSize=20;
	
	//   
	private int currentPageNo;
	
	//   
	private int totalPages;

	//DubietyPositionDao
	DubietyPositionDao dao=new DubietyPositionDao();
	//    
	public Page(){
		totalRows=dao.getTotalRows();
		if(totalRows%pageSize==0){
			totalPages=totalRows/pageSize;
		}else{
			totalPages=totalRows/pageSize+1;
		}
		//   currentPageNo          currentPageNo=1
		currentPageNo=1;
	}
	//       
	public List getCurrentPage(int _currentPageNo,int _pageSize){
		return dao.currentPage(_currentPageNo, _pageSize);
	}
	
	//   
	public void first(){
		currentPageNo=1;
	}
	
	//    
	public void last(){
		currentPageNo=totalPages;
	}
	//          
	public void refresh(int _currentPageNo){
		currentPageNo=_currentPageNo;
		if(currentPageNo>totalPages)
			last();
	}
	
	
	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public DubietyPositionDao getDao() {
		return dao;
	}
	public void setDao(DubietyPositionDao dao) {
		this.dao = dao;
	}
	
}
----------------------
daoのDubietyPositionDaoクラスのページング内容の書き方1:resultsetでページング
//pageSize    ,totalRows   
	public List getDPCurrentPage(int currentPageNo, int pageSize){
		List listDP=null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		//       start
		int total = currentPageNo * pageSize;
		//       
		int start = total - pageSize + 1;
		
		String sql="select Top "+total+" posid,comname,posname,address_pc,reqDegreeName,reqworkyear "
		+"from huanglq_query..compos_query where contains(comname,'"+COMNAMES+"') "
		+"and contains(posname,'"+POSNAMES+"')";
		
		
		try {
			conn = DBUtil.getHuanglqQueryConn();
			pstmt = conn.prepareStatement(sql,
					ResultSet.TYPE_SCROLL_INSENSITIVE,//                      ResultSet      。
					ResultSet.CONCUR_READ_ONLY);//            ResultSet        
			
			rs = pstmt.executeQuery();

			Log.debug(this, "getDPCurrentPage(int currentPageNo, int pageSize)", sql.toString());

			if (rs.absolute(start)) {//rs         
				listDP = new ArrayList();
				while (rs.getRow() <= total) {
					DubietyPosition dp = new DubietyPosition();
					dp.setPosId(rs.getInt("posid"));
					dp.setComname(rs.getString("comname"));
					dp.setPosname(rs.getString("posname"));
					dp.setAddress(rs.getString("address_pc"));
					dp.setReqWorkYear(rs.getInt("reqworkyear"));
					dp.setReqDegreeName(rs.getString("reqDegreeName"));
					//                
					listDP.add(dp);
					
					/*
					 * rs.nect()     rs.getRow()    1,                   ,  null  
					 */
					if (!rs.next()) {
						break;
					}
				}
			}
		} catch (Exception e) {
			Log.error(this, "getDPCurrentPage(int currentPageNo, int pageSize)", e.getMessage());
		} finally {
			DBUtil.clean(this, rs);
			DBUtil.clean(this, pstmt);
			DBUtil.clean(this, conn);
		}
		return listDP;
	}

daoのDubietyPositionDaoクラスのページング内容の書き方2:sql文でページングを参照
public List currentPage(int currentPageNo, int pageSize) {
	List listDP = new ArrayList();//       	
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	int pageNo = (currentPageNo - 1) * pageSize;
	int pSize = pageSize;
	//       
	String sql = "select Top "+ pSize+ " posid,comname,posname,address_pc,reqDegreeName,reqworkyear from huagnlq_query..compos_query where posid not in " + "( select Top "+ pageNo+ " posid from huanglq_query..compos_query order by inserttime desc)  order by inserttime desc";

	try {
	    conn = DBUtil.getHuanglqQueryConn();
	    pstmt = conn.prepareStatement(sql);
	    rs = pstmt.executeQuery();
	    while (rs.next()) {
	         DubietyPosition dp = new DubietyPosition();
		dp.setPosId(rs.getInt("posid"));
		dp.setComname(rs.getString("comname"));
		dp.setPosname(rs.getString("posname"));
		dp.setAddress(rs.getString("address_pc"));
		dp.setReqWorkYear(rs.getInt("reqworkyear"));
	        dp.setReqDegreeName(rs.getString("reqDegreeName"));
		//                
		listDP.add(dp);
	    }
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		DBUtil.clean(listDP, rs);
		DBUtil.clean(listDP, pstmt);
		DBUtil.clean(listDP, conn);
	}
	return listDP;
	}

DubietyPositionDao.java
package com.huanglq.weihu.dao;

/**
 *              Dao
 *           huanglw_query..compos_query [huanglq].[dbo].[com_Position]
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

import com.huanglq.message.Message;
import com.huanglq.util.DBUtil;
import com.huanglq.util.DateTime;
import com.huanglq.util.Log;

public class DubietyPositionDao {
	
	//        
	public static final String[] COMNAME_CONTAINS = {"  ","   ","  ","  ","  ","   ","  ","  "};
	
	//       
	public static final String[] POSNAME_CONTAINS = {"dj","   ","  ","   ","  ","  "," ","   ",
		"  ","  ","  ","   ","  ","  ","   ","  "};
	//              
	public static String COMNAMES="";
	//             
	public static String POSNAMES="";
	
	//           COMNAMES POSNAMES
	static {
		for(int i=0;i<COMNAME_CONTAINS.length;i++){
			COMNAMES=COMNAMES+COMNAME_CONTAINS[i]+" or ";
		}
		COMNAMES = COMNAMES.substring(0, COMNAMES.length() - 4);
		
		for(int i=0;i<POSNAME_CONTAINS.length;i++){
			POSNAMES=POSNAMES+POSNAME_CONTAINS[i]+" or ";
		}
		POSNAMES = POSNAMES.substring(0, POSNAMES.length() - 4);
	}
	
	//               sql  
	public static final String TOTALSIZE_SQL="select count(*) from huauglq_query..compos_query where contains " +
			"(comname,'"+COMNAMES+"') and  contains(posname,'"+POSNAMES+"')";
	
	//     posid  posDescription,map(posid,posDescription)
	private Map map = new HashMap();
	//     posid  posState,map(posid,posState)
	private Map mapPosState = new HashMap();

	//     huanglq_query..compos_query
	//           
	public int getTotalRows() {
		int totalRows = 0;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			conn = DBUtil.getHuanglqQueryConn();
			pstmt = conn.prepareStatement(TOTALSIZE_SQL);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				totalRows = rs.getInt(1);
			}
			Log.debug(this, "DubietyPositionDao.getTotalRows()", TOTALSIZE_SQL);
		} catch (Exception e) {
			Log.error(this, "DubietyPositionDao.getTotalRows()", e.getMessage());
		} finally {
			DBUtil.clean(this, rs);
			DBUtil.clean(this, pstmt);
			DBUtil.clean(this, conn);
		}
		return totalRows;
	}

	//     huanglq_query..compos_query
	//         ,     
	public List currentPage(int currentPageNo, int pageSize) {
		List listDP = new ArrayList();//       
		
		long startTime=new Date().getTime();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		int pageNo = (currentPageNo - 1) * pageSize;
		int pSize = pageSize;
		//       
		String sql = "select Top "
				+ pSize
				+ " posid,comname,posname,address_pc,reqDegreeName,reqworkyear from huanglq_query..compos_query where posid not in "
				+ "( select Top "
				+ pageNo
				+ " posid from huanglq_query..compos_query "
				+ "where contains(comname,'"+COMNAMES+"') "
				+ "and contains(posname,'"+POSNAMES+"') order by inserttime desc) "
				+ "and contains(comname,'"+COMNAMES+"')"
				+ " and contains(posname,'"+POSNAMES+"') order by inserttime desc";

		try {
			conn = DBUtil.getHuanglqQueryConn();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				DubietyPosition dp = new DubietyPosition();
				dp.setPosId(rs.getInt("posid"));
				dp.setComname(rs.getString("comname"));
				dp.setPosname(rs.getString("posname"));
				dp.setAddress(rs.getString("address_pc"));
				dp.setReqWorkYear(rs.getInt("reqworkyear"));
				dp.setReqDegreeName(rs.getString("reqDegreeName"));
				//                
				listDP.add(dp);
			}
			Log.debug(this, "DubietyPositionDao.currentPage(int currentPageNo, int pageSize)", sql);
		} catch (Exception e) {
			Log.error(this, "DubietyPositionDao.currentPage(int currentPageNo, int pageSize)", e.getMessage());
		} finally {
			DBUtil.clean(listDP, rs);
			DBUtil.clean(listDP, pstmt);
			DBUtil.clean(listDP, conn);
		}
		long endTime=new Date().getTime();
		System.out.println(endTime-startTime+" ***************************");
		return listDP;
	}

	//     [huanglq].[dbo].[com_Position]
	//   posid       ,            ,
	// --PosState 0=  ,1=  ,2=  
	public void modifyPosState(int posid) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		//    posState  1
		String sql = "UPDATE [huanglq].[dbo].[com_Position] SET PosState =1,UpdateDate=getDate() WHERE posId=?";
		try {
			conn = DBUtil.getHuanglq90Conn();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, posid);
			pstmt.execute();
			Log.debug(this, "DubietyPositionDao.modifyPosState(int posid)", sql);
		} catch (Exception e) {
			Log.error(this, "DubietyPositionDao.modifyPosState(int posid)", e.getMessage());
		} finally {
			DBUtil.clean(this, pstmt);
			DBUtil.clean(this, conn);
		}
	}

	//     [huanglq].[dbo].[com_Position]
	//   posid            , map   
	//                   ,       
	// --PosState 0=  ,1=  ,2=  
	//   posStatus=1  posid checkbox disabled="disabled"  
	// mapPosState  mapPosState(posid,posStatus),map  map(posid,posDescription)
	//posids          posid  
	public void getPosDescAndState(String posids) {
		Connection conn = null;
		Statement pstmt = null;
		ResultSet rs = null;
		String sql = "select posid,posDescription,posState from [huanglq].[dbo].[com_Position] WHERE posID in(";
		try {
			conn = DBUtil.getHuanglqConn();
			pstmt = conn.createStatement();
			rs = pstmt.executeQuery(sql + posids + ")");
			while (rs.next()) {
				Integer posId = new Integer(rs.getInt("posid"));
				String posDescription = "";
				try {
					posDescription = rs.getString("posDescription");
				} catch (Exception e) {
					posDescription = "         ";
				}
				map.put(posId, posDescription);
				mapPosState.put(posId, new Integer(rs.getInt("posState")));
			}
			Log.debug(this, "DubietyPositionDao.getPosDescAndState(String posids)", sql);
		} catch (Exception e) {
			Log.error(this, "DubietyPositionDao.getPosDescAndState(String posids)", e.getMessage());
		} finally {
			DBUtil.clean(this, rs);
			DBUtil.clean(this, pstmt);
			DBUtil.clean(this, conn);
		}
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Map getMapPosState() {
		return mapPosState;
	}

	public void setMapPosState(Map mapPosState) {
		this.mapPosState = mapPosState;
	}
	
	//    2,     
	//pageSize    ,totalRows   
	public List getDPCurrentPage(int currentPageNo, int pageSize){
		List listDP=null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		//       start
		int total = currentPageNo * pageSize;
		//       
		int start = total - pageSize + 1;
		
		String sql="select Top "+total+" posid,comname,posname,address_pc,reqDegreeName,reqworkyear "
		+"from huanglq_query..compos_query where contains(comname,'"+COMNAMES+"') "
		+"and contains(posname,'"+POSNAMES+"')";
		
		
		try {
			conn = DBUtil.getHuanglqQueryConn();
			pstmt = conn.prepareStatement(sql,
					ResultSet.TYPE_SCROLL_INSENSITIVE,//                      ResultSet      。
					ResultSet.CONCUR_READ_ONLY);//            ResultSet        
			
			rs = pstmt.executeQuery();

			Log.debug(this, "getDPCurrentPage(int currentPageNo, int pageSize)", sql.toString());

			if (rs.absolute(start)) {// rs         
				listDP = new ArrayList();
				while (rs.getRow() <= total) {
					DubietyPosition dp = new DubietyPosition();
					dp.setPosId(rs.getInt("posid"));
					dp.setComname(rs.getString("comname"));
					dp.setPosname(rs.getString("posname"));
					dp.setAddress(rs.getString("address_pc"));
					dp.setReqWorkYear(rs.getInt("reqworkyear"));
					dp.setReqDegreeName(rs.getString("reqDegreeName"));
					//                
					listDP.add(dp);
					
					/*
					 * rs.next()     rs.getRow()    1,                   ,  null  
					 */
					if (!rs.next()) {
						break;
					}
				}
			}
		} catch (Exception e) {
			Log.error(this, "getDPCurrentPage(int currentPageNo, int pageSize)", e.getMessage());
		} finally {
			DBUtil.clean(this, rs);
			DBUtil.clean(this, pstmt);
			DBUtil.clean(this, conn);
		}
		return listDP;
	}
	
}


/**
* DubietyPosition.java
*疑わしいポジションをマウントするクラスです
*不審なポジション情報のフィルタ
*/

package com.jobcn.weihu.vo;

public class DubietyPosition {

	//   id
	private int posId;

	//      
	private String posname;

	//    
	private String comname;

	//   
	private String address;

	//     
	private String reqDegreeName;

	//     
	private int reqWorkYear;

	public int getPosId() {
		return posId;
	}

	public void setPosId(int posId) {
		this.posId = posId;
	}

	public String getPosname() {
		return posname;
	}

	public void setPosname(String posname) {
		this.posname = posname;
	}

	public String getComname() {
		return comname;
	}

	public void setComname(String comname) {
		this.comname = comname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getReqDegreeName() {
		return reqDegreeName;
	}

	public void setReqDegreeName(String reqDegreeName) {
		this.reqDegreeName = reqDegreeName;
	}

	public int getReqWorkYear() {
		return reqWorkYear;
	}

	public void setReqWorkYear(int reqWorkYear) {
		this.reqWorkYear = reqWorkYear;
	}
}