Servlet+Dao+JavaBean實現簡單的分條件查詢並且實現分頁
阿新 • • 發佈:2019-01-02
PageBean類
分頁類實現方法
package com.systop.rwgl.page.model; import java.util.List; public class Page<T> { private int currentPage = 1; // 當前頁, 預設顯示第一頁 private int pageCount = 10; // 每頁顯示的行數(查詢返回的行數), 預設每頁顯示4行 private int totalCount; // 總記錄數 private int totalPage; // 總頁數 = 總記錄數 / 每頁顯示的行數 (+ 1) private List pageData; // 分頁查詢到的資料 public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { if (totalCount % pageCount == 0) { totalPage = totalCount / pageCount; } else { totalPage = totalCount / pageCount + 1; } return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List getPageData() { return pageData; } public void setPageData(List pageData) { this.pageData = pageData; } }
Dao類
package com.systop.rwgl.taskrecept.dao; 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.systop.core.dao.BaseDao; import com.systop.rwgl.page.model.Page; import com.systop.rwgl.taskrecept.model.TaskRecept; /** * * 使用者接受處理Dao類 * */ public class TaskReceptDao extends BaseDao { Connection conn = null; PreparedStatement stmt = null; /** * 分頁查詢所有已經接受且未完成的任務 * 返回集合 * @param Page tr,name,workno, workname,workjjcd,starttime,stoptime * @return * @throws Exception */ public void selectReceptTask(Page<TaskRecept> tr,String name , String workno, String workname,String workjjcd,String starttime,String stoptime) throws Exception { // 分頁查詢資料 把查詢到的資料設定到tr物件中 StringBuffer sql = new StringBuffer("select * "); StringBuffer content = new StringBuffer("FROM task_work where WK_JSZ = '"+name+"' and WK_SFJS = 1 and WK_ZT=2"); try { if(!workno.equals("")) { content.append(" and WK_NO='"+workno+"'"); } if(!workname.equals("")) { content.append(" and WK_MC like '%"+workname+"%'"); } if(!workjjcd.equals("")) { content.append(" and WK_YXJ='"+workjjcd+"'"); } if(!starttime.equals("") && !stoptime.equals("")) { content.append(" and WK_JZRQ between '"+starttime+"' and '"+stoptime+"'"); } sql.append(content); sql.append(" limit ?,?"); StringBuffer sql2 = new StringBuffer("select content(*) "); sql2.append(content); String sql3 = sql2.toString(); //存入總記錄數 int totalCount = this.getTotalCount(name); tr.setTotalCount(totalCount); // 1. 獲取當前頁: 計算查詢的起始行、返回的行數 if (tr.getCurrentPage() <= 0) { tr.setCurrentPage(1); // 把當前頁設定為1 } else if (tr.getCurrentPage() > tr.getTotalPage()) { tr.setCurrentPage(tr.getTotalPage()); // 把當前頁設定為最大頁數 } int currentPage = tr.getCurrentPage(); int index = (currentPage - 1) * tr.getPageCount(); // 查詢的起始行 int count = tr.getPageCount(); // 查詢返回的行數 String sql1 = sql.toString(); conn = getConn(); stmt = conn.prepareStatement(sql1); stmt.setInt(1, index); stmt.setInt(2, count); ResultSet rs = stmt.executeQuery(); List<TaskRecept> tasklist = new ArrayList<TaskRecept>(); while (rs.next()) { TaskRecept ta = new TaskRecept(); ta.setId(rs.getInt(1)); ta.setNo(rs.getString(3)); ta.setMc(rs.getString(4)); ta.setLb(rs.getString(5)); ta.setZxz(rs.getString(7)); ta.setFbz(rs.getString(8)); ta.setNr(rs.getString(10)); ta.setYxj(rs.getString(11)); tasklist.add(ta); List<TaskRecept> pageData = tasklist; tr.setPageData(pageData); } } catch (SQLException e) { e.printStackTrace(); }finally { if (conn != null) { conn.close(); } if (stmt != null) { stmt.close(); } } }
Servlet
package com.systop.rwgl.taskrecept.controller;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.systop.rwgl.employee.model.Employee;
import com.systop.rwgl.page.model.Page;
import com.systop.rwgl.selectoption.dao.SelectOptionDao;
import com.systop.rwgl.selectoption.model.Select;
import com.systop.rwgl.taskrecept.dao.TaskReceptDao;
import com.systop.rwgl.taskrecept.model.TaskRecept;
import com.systop.rwgl.user.model.User;
/**
* 前端控制器任務接收處理類
*/
@WebServlet("/TaskReceptServlet")
public class TaskReceptServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TaskReceptServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//查詢所有已經接收和未完成的任務
String action = request.getParameter("action");
if(action != null && action.equals("select")){
//接收表單資料
String workno = "";
String workname = "";
String workjjcd = "";
String starttime = "";
String stoptime = "";
workno=request.getParameter("workno")==null?"":request.getParameter("workno");
workname=request.getParameter("workname")==null?"":request.getParameter("workname");
workjjcd=request.getParameter("workjjcd")==null?"":request.getParameter("workjjcd");
starttime=request.getParameter("starttime")==null?"":request.getParameter("starttime");
stoptime=request.getParameter("stoptime")==null?"":request.getParameter("stoptime");
try {
TaskReceptDao TaskReceptDao = new TaskReceptDao();
String currPage = request.getParameter("currentPage");
// 判斷
if (currPage == null || "".equals(currPage.trim())){
currPage = "1"; // 第一次訪問,設定當前頁為1;
}
// 轉換
int currentPage = Integer.parseInt(currPage);
//2. 建立PageBean物件,設定當前頁引數; 傳入page方法引數
Page<TaskRecept> page = new Page<TaskRecept>();
page.setCurrentPage(currentPage);
//獲取session中的姓名
HttpSession session = request.getSession();
Employee employee = (Employee) session.getAttribute("employee");
String name = employee.getName();
try {
TaskReceptDao.selectReceptTask(page, name, workno, workname, workjjcd, starttime, stoptime);
} catch (Exception e) {
e.printStackTrace();
}
String context = request.getContextPath();
// int n = currentPage /5;
// String[] style = {"","","","","",""};
//
// for(int i = 0; i < 5; i++)
// if(5*n +i+1 == currentPage) {
// style[i]="am-active";
// }
String pageContent = "";
if(currentPage == 1) {
pageContent =
"<UL class=\"am-pagination\">" +
" <LI><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage-1)+"\">«</A></LI>\r\n" +
" <LI class=\"am-active\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage)+"\">"+currentPage+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+1)+"\">"+(currentPage+1)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+2)+"\">"+(currentPage+2)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+3)+"\">"+(currentPage+3)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+4)+"\">"+(currentPage+4)+"</A></LI>\r\n" +
" <LI><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+1)+"\">»</A></LI>\r\n" +
"</UL>";
}else {
pageContent =
"<UL class=\"am-pagination\">" +
" <LI><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage-1)+"\">«</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage-1)+"\">"+(currentPage-1)+"</A></LI>\r\n" +
" <LI class=\"am-active\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage)+"\">"+(currentPage)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+1)+"\">"+(currentPage+1)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+2)+"\">"+(currentPage+2)+"</A></LI>\r\n" +
" <LI class=\"\"><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+3)+"\">"+(currentPage+3)+"</A></LI>\r\n" +
" <LI><A\r\n" + "href=\""+context+"/taskrecept?action=pagelist¤tPage="+(currentPage+1)+"\">»</A></LI>\r\n" +
"</UL>";
}request.setAttribute("pageContent", pageContent );
request.setAttribute("page", page);
request.getRequestDispatcher("taskrecept/taskrecept.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
任務接收處理頁面。taskrecept.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/common/taglib.jsp"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<LINK href="css/admin.css" rel="stylesheet">
<LINK href="css/amazeui.min.css" rel="stylesheet">
<link href="css/form.css" rel="stylesheet">
<script src="js/laydate/laydate.js"></script>
</head>
<BODY>
<DIV class="admin-content">
<HEADER class="am-topbar admin-header">
<DIV class="am-cf am-padding">
<DIV class="am-fl am-cf am-topbar-brand">
<STRONG class="am-text-primary am-text-lg">任務接收處理</STRONG> / <SMALL>這裡有你接收且未完成的任務呦</SMALL>
</DIV>
</DIV>
</HEADER>
<div class="opensearch" id="opensearch" onclick="opensearch();">
<a>開啟關閉查詢資訊</a>
</div>
<div>
<form
action="${pageContext.request.contextPath}/taskrecept?action=select"
method="post" id="form1" style="display: none">
<div class="search">
<span class="worksearch"> 任務編號:<input
type="text" name="workno" value="${workno}"></span> <span class="worksearch">任務名稱:<input
type="text" name="workname" value="${workname}"></span> <span class="worksearch">
緊急程度:<select name="workjjcd" value="${workjjcd}">
<option value="">請選擇緊急程度</option>
<c:forEach items="${jjcdlist}" var="arr">
<option>${arr.name}</option>
</c:forEach>
</select>
</span>
</div>
<div class="search">
<span class="worksearch">
完成時間從:<input type="taxt" id="start" name="starttime" value="${starttime}">
到 <input type="taxt" id="stop" name="stoptime" value="${stoptime}">
</span>
<span class="worksearch">
<input type="submit" value="查詢" class="am-btn am-btn-primary">
</span>
<span class="worksearch"> <input type="reset"
class="am-btn am-btn-primary" value="清空">
</span>
</div>
</form>
</div>
<DIV class="am-g">
<DIV class="am-u-sm-12">
<FORM class="am-form">
<TABLE class="am-table am-table-striped am-table-hover table-main">
<THEAD>
<TR>
<TH class="table-id">序號</TH>
<TH class="table-title">任務編號</TH>
<TH class="table-title">任務名稱</TH>
<TH class="table-title">任務類別</TH>
<TH class="table-type">釋出人</TH>
<TH class="table-author">緊急程度</TH>
<TH class="table-date">任務內容</TH>
<TH class="table-set">操作</TH>
</TR>
</THEAD>
<TBODY>
<c:forEach items="${page.pageData}" var="arr" varStatus="status">
<tr>
<td>${status.index+1}</td>
<td>${arr.no}</td>
<td>${arr.mc}</td>
<td>${arr.lb}</td>
<td>${arr.fbz}</td>
<td>${arr.yxj}</td>
<td>${arr.nr}</td>
<TD>
<DIV class="am-btn-group am-btn-group-xs"
style="position: relative;">
<a href="#" class="am-btn am-btn-default am-btn-xs am-text-secondary" onclick="openwritefk(${arr.id});"> 填寫反饋 </a>
<a href="${pageContext.request.contextPath}/taskDelay?taskNo=${arr.no}&action=taskno" class="am-btn am-btn-default am-btn-xs"> 申請延期 </a>
<a onclick="return complete(${arr.id});" class="am-text-secondary am-btn am-btn-default am-btn-xs am-text-danger">
完成任務 </a>
</DIV>
</TD>
</tr>
</c:forEach>
</TBODY>
</TABLE>
<DIV class="am-cf">
共 ${counts}條記錄
<DIV class="am-fr">${pageContent}</DIV>
</DIV>
<HR>
</FORM>
</DIV>
</DIV>
</DIV>
<c:forEach items="${page.pageData}" var="arr" varStatus="status">
<div class="content2" id="fk${arr.id}" style="display: none;">
<div class="title">請填寫以下內容</div>
<div class="main">
<form
action="${pageContext.request.contextPath}/taskrecept?action=addfk"
method="post">
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tbody>
<tr valign="top">
<td valign="middle" align="right" class="field">內容:</td>
<td><textarea name="fknr" id="message" cols="60" rows="7"></textarea>
<font color="red">*</font></td>
</tr>
<tr valign="top">
<td class="field"></td>
<td><input name="id" id="${arr.id}" type="hidden"
value="${arr.id}" /> <input type="submit" value="提 交"
class="am-btn am-btn-default am-btn-xs" />
<input type="button" value="關閉"
class="am-btn am-btn-default am-btn-xs" onclick="closewritefk()" /></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</c:forEach>
</BODY>
<script type="text/javascript">
function opensearch(){
if(document.getElementById("form1").style.display=="none"){
document.getElementById("form1").style.display="";
}else{
document.getElementById("form1").style.display="none";
}
}
function openwritefk(){
if(document.getElementById("writefk").style.display=="none"){
document.getElementById("writefk").style.display="";
}else{
document.getElementById("writefk").style.display="none";
}
}
function complete(id){
if(confirm("確定任務已經完成了麼?")){
window.location.href = "${pageContext.request.contextPath}/taskrecept?action=complete&id="+id;
}else{
return;
}
}
function openwritefk(id){
if(document.getElementById("fk"+id).style.display=="none"){
document.getElementById("fk"+id).style.display="";
}else{
document.getElementById("fk"+id).style.display="none";
}
}
function closewritefk(){
for(var i = 0; i<${fn:length(page.pageData)} ; i++){
document.getElementsByClassName("content2")[i].style.display="none";
}
}
//執行一個laydate例項
laydate.render({
elem: '#start'
,type: 'date'
});
laydate.render({
elem: '#stop'
,type: 'date'
});
</script>
</html>
實現簡單的任務接收處理分頁查詢