天天看點

使用JSP自定義标簽實作簡易分頁功能

今天學習了JSP的自定義标簽的3個建立方式特此開始記錄下來

DB類

package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class dbUtil {

	Connection con=null;
	ResultSet rs=null;
	PreparedStatement pst=null;
	
	public void getConnection(){
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studb","root","123456");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	/**
	 * 添加、修改、查詢
	 * @param sql
	 * @param param
	 * @return
	 */
	public int execUpdate(String sql,Object[] param){
		int row = -1;
		getConnection();
		try {
			pst = con.prepareStatement(sql);
			
			if(param!=null){
				for(int i=0;i<param.length;i++){
					pst.setObject(i+1,param[i]);
				}
			}
			row = pst.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return row;
	}
	public ResultSet execQuery(String sql,Object[] param){
		getConnection();
		try {
			pst = con.prepareStatement(sql);
			if(param!=null){
				for(int i=0;i<param.length;i++){
					pst.setObject(i+1,param[i]);
				}
			}
			
			rs = pst.executeQuery();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return rs;
	}
	/**
	 * 關閉
	 * @param rs
	 */
	public void getClose(ResultSet rs){
		
		try {
			if(rs!=null){
				rs.close();
			}
			if(con!=null){
				con.close();
			}
			if(pst!=null){
				pst.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
           

DAO類

package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.pojo.Page;
import com.pojo.Student;

public class studentDAO {

	dbUtil db = new dbUtil();
	/**
	 * 分頁
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page<Student> Query(int pageNo,int pageSize,String tab){
		
		ArrayList<Student> list = new ArrayList<>();
		
		Page<Student> pa = new Page<Student>();
		
		int page =(pageNo-1)*pageSize;
		
		ResultSet rs = db.execQuery("SELECT * FROM "+tab+" LIMIT "+page+","+pageSize+";", null);
		try {
			while(rs.next()){
				Student stu = new Student();
				stu.setId(rs.getInt(1));
				stu.setName(rs.getString(2));
				stu.setSex(rs.getString(3));
				stu.setAge(rs.getInt(4));
				stu.setAddress(rs.getString(5));
				list.add(stu);
			}
			pa.setCount(getCount(tab));
			pa.setPageNo(pageNo);
			pa.setPageSize(pageSize);
			pa.setData(list);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return pa;
		
	}
    //傳回的是資料庫的總數量
	public int getCount(String tab){
		
		ResultSet rs = db.execQuery("select count(*) from "+tab+"", null);
		
		int row=0;
		try {
			while(rs.next()){
				row=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row;
	}
}
           

pojo類

package com.pojo;

import java.util.ArrayList;

public class Page<T> {

	// 每頁存放5條資料
	private int pageSize = 5;
	// 目前頁數
	private int pageNo = 1;
	// 總的數量
	private int count = 0;
	// 資料存放
	private ArrayList<T> data = null;

	public int getPageSize() {
		return pageSize;
	}

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

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public ArrayList<T> getData() {
		return data;
	}

	public void setData(ArrayList<T> data) {
		this.data = data;
	}

	// 首頁
	public int getIndexPage() {

		return 1;
	}

	// 上一頁
	public int getUpPage() {

		int page = pageNo - 1;
		if (page <= 1) {
			page = 1;
		}
		return page;
	}

	// 下一頁
	public int getDownPage() {
		int page = pageNo + 1;
		if (page >= getSumPage()) {

			page = getSumPage();
		}
		return page;
	}

	// 尾頁 總頁數
	public int getSumPage() {

		if (count % pageSize == 0) {

			return count / pageSize;

		} else {
			return count / pageSize + 1;
		}
	}

	// 尾頁
	public int getLastPage() {

		if (count % pageSize == 0) {

			return count / pageSize;

		} else {
			return count / pageSize + 1;
		}
	}
}
           

标簽實作類

package com.zuxia;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.dao.studentDAO;
import com.pojo.Page;
import com.pojo.Student;

public class pageTag extends TagSupport{
	
	private Integer pageIndex;
	private Integer pageSize;
	private String tableName;
	
	public Integer getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(Integer pageIndex) {
		this.pageIndex = pageIndex;
	}

	public Integer getPageSize() {
		return pageSize;
	}

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

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		JspWriter out = pageContext.getOut();
		Page<Student> page = new studentDAO().Query(pageIndex, pageSize, tableName);
		ArrayList<Student> list = page.getData();
		try {
			out.print("<table  cellspacing='0' cellpadding='5' width='50%' align='center'>");
				out.print("<tr>");
				out.print("<td><h3>");out.print("序号");out.print("</h3></td>");
				out.print("<td><h3>");out.print("姓名");out.print("</h3></td>");
				out.print("<td><h3>");out.print("性别");out.print("</h3></td>");
				out.print("<td><h3>");out.print("年齡");out.print("</h3></td>");
				out.print("<td><h3>");out.print("位址");out.print("</h3></td>");
				out.print("</tr>");
			for (int i=0;i<list.size();i++) {
				Student stu = list.get(i);
				out.print("<tr>");
				out.println("<td>"+stu.getId()+"</td>");
				out.println("<td>"+stu.getName()+"</td>");
				out.println("<td>"+stu.getSex()+"</td>");
				out.println("<td>"+stu.getAge()+"</td>");
				out.println("<td>"+stu.getAddress()+"</td>");
				out.print("</tr>");
			}
			
			out.print("<tr>");
			out.print("<td colspan='5' style='text-align: center;'>");
			out.print("<a href='index.jsp?pageIndex=1'>首頁</a>&nbsp;&nbsp;<a href='index.jsp?pageIndex="+page.getUpPage()+"'>上一頁</a>&nbsp;&nbsp;<a href='index.jsp?pageIndex="+page.getDownPage()+"'>下一頁</a>&nbsp;&nbsp;<a href='index.jsp?pageIndex="+page.getSumPage()+"'>尾頁</a>");
			out.print("</td>");
			out.print("</tr>");
			out.print("</table>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	@Override
	public int doAfterBody() throws JspException {
		// TODO Auto-generated method stub
		return super.doAfterBody();
	}
	
	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return super.doEndTag();
	}
	
}
           

pageTag.tld配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
        
<taglib>
	<!-- taglib的版本 -->
	<tlib-version>1.0</tlib-version>
	<!-- jsp的版本 -->
	<jsp-version>1.2</jsp-version>
	<!-- 标簽的短名 -->
	<short-name>myTag</short-name>
	<!-- 提供給jsp頁面引用的路徑 -->
	<uri>page.com</uri>
	<tag>
		<!-- 标簽名 -->
		<name>page</name>
		<!-- 路徑 -->
		<tag-class>com.zuxia.pageTag</tag-class>
		<!-- 标簽類型(empty/jsp) -->
		<body-content>empty</body-content>
		<attribute>
			<!-- 變量名稱 -->
			<name>pageIndex</name>
			<!-- 是否必填 -->
			<required>true</required>
			<!-- 是否可以輸入表達式-->
			<rtexprvalue>true</rtexprvalue>
			<!-- 提示資訊 -->
			<description>請輸入起始坐标</description>
		</attribute>
		<attribute>	
			<!-- 變量名稱 -->
			<name>pageSize</name>
			<!-- 是否必填 -->
			<required>true</required>
			<!-- 是否可以輸入表達式-->
			<rtexprvalue>true</rtexprvalue>
			<!-- 提示資訊 -->
			<description>請輸入起始坐标</description>
		</attribute>	
		<attribute>
			<!-- 變量名稱 -->
			<name>tableName</name>
			<!-- 是否必填 -->
			<required>true</required>
			<!-- 是否可以輸入表達式-->
			<rtexprvalue>true</rtexprvalue>
			<!-- 提示資訊 -->
			<description>請輸入起始坐标</description>
		</attribute>
	</tag>
</taglib>
           

頁面引用

<%@ taglib uri="page.com" prefix="myTag" %>

<%
		request.setCharacterEncoding("UTF-8");
	%>
	<myTag:page pageIndex="${param.pageIndex == null ? 1 : param.pageIndex}" pageSize="5" tableName="stuinfo"/>
           

好了