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" οnclick="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" οnclick="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 οnclick="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" οnclick="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>
实现简单的任务接收处理分页查询