利用js日期控件重构WEB功能

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

开发需求:网页中的日期部门(注册页面和查询条件)都用js日期控件重写

页面一:更新员工页面

empUpdate.jsp中增加 onfocus 事件

入职日期:<inputid="hiredate"type="text"name="hiredateTxt"value="${requestScope.empBean.hiredate}"onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)"readonly="readonly"><

br>

<%@ page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core"PRefix="c"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>员工更新</title><linkhref="/web01//CSS/main.css"rel="stylesheet"type="text/css"/><scripttype="text/Javascript"src="/web01/js/DatePicker.js"></script></head><body><%@ include file="top.jsp"%><formaction="/web01/empController"method="get">员工编号:<inputtype="text"disabled="disabled"value="${requestScope.empBean.empno}"><

br>员工姓名:<inputtype="text"name="enameTxt"value="${requestScope.empBean.ename}"><

br>职位:<inputtype="text"name="jobTxt"value="${requestScope.empBean.job}"><

br>领导:<inputtype="text"name="mgrTxt"value="${requestScope.empBean.mgr}"><

br>入职日期:<inputid="hiredate"type="text"name="hiredateTxt"value="${requestScope.empBean.hiredate}"onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)"readonly="readonly"><

br>工资:<inputtype="text"name="salTxt"value="${requestScope.empBean.sal}"><

br>奖金:<inputtype="text"name="commTxt"value="${requestScope.empBean.comm}"><

br>部门:<inputtype="text"name="deptnoTxt"value="${requestScope.empBean.deptno}"><

br><inputtype="submit"value="Save"><inputtype="hidden"name="callTp"value="empSave"><inputtype="hidden"name="empno"value="${requestScope.empBean.empno}"><

br/></form><%@ include file="bottom.jsp"%></body></html>

前台传到后台的日期格式是 yyyy-mm-dd,在java端进行格式化去掉“-”后变成 yyyymmdd格式的字符串,然后保存到数据库。

所以增加了一个处理String的类StringUtil.java。

packagecom.test.common.util;publicclassStringUtil {publicstaticString formatString(String dateStringWithLine){

String dateString=null;if(dateStringWithLine !=null) {

dateString= dateStringWithLine.replace("-", "");

}returndateString;

}

}

在service层调用SQL之前处理日期字符串StringUtil.formatString(empBean.getHiredate())

//更新emp信息publicintempSave(EmpBean empBean) {intupdateResulInt = 0;

BaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("UPDATE EMP SET ENAME = ? \n");

sqlBf.append(" , JOB = ? \n");

sqlBf.append(" , MGR = ? \n");

sqlBf.append(" , HIREDATE = TO_DATE(?, 'YYYYMMDD') \n");

sqlBf.append(" , SAL = ? \n");

sqlBf.append(" , COMM = ? \n");

sqlBf.append(" , DEPTNO = ? \n");

sqlBf.append("WHERE EMPNO = ? \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

idx= 1;

pstmt.setString(idx++, empBean.getEname());

pstmt.setString(idx++, empBean.getJob());

pstmt.setInt(idx++, empBean.getMgr());

pstmt.setString(idx++, StringUtil.formatString(empBean.getHiredate()));

pstmt.setDouble(idx++, empBean.getSal());

pstmt.setDouble(idx++, empBean.getComm());

pstmt.setInt(idx++, empBean.getDeptno());

pstmt.setInt(idx++, empBean.getEmpno());

updateResulInt=pstmt.executeUpdate();

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(null, pstmt, conn);

}returnupdateResulInt;

}

页面效果是

页面二:添加员工

empAdd.jsp 中也跟上面相同方式处理

入职日期:<inputid="hiredate"type="text"name="hiredateTxt"value=""onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)"readonly="readonly"><

br>

<%@ page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>添加员工</title><linkhref="/web01/css/main.css"rel="stylesheet"type="text/css"/><scripttype="text/javascript"src="/web01/js/DatePicker.js"></script></head><body><%@ include file="top.jsp"%><formaction="/web01/empController"method="get">员工姓名:<inputtype="text"name="enameTxt"value=""maxlength="10"><

br>职位:<inputtype="text"name="jobTxt"value=""maxlength="9"><

br>领导号:<inputtype="text"name="mgrTxt"value=""maxlength="4"><

br>入职日期:<inputid="hiredate"type="text"name="hiredateTxt"value=""onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)"readonly="readonly"><

br>工资:<inputtype="text"name="salTxt"value=""maxlength="7"><

br>奖金:<inputtype="text"name="commTxt"value=""maxlength="7"><

br>部门编号:<inputtype="text"name="deptnoTxt"value=""maxlength="2"><

br><inputtype="submit"value="Add"><inputtype="hidden"name="callTp"value="empAdd"><

br/></form><%@ include file="bottom.jsp"%></body></html>

添加员工的service层调用SQL之前处理字符串StringUtil.formatString(empBean.getHiredate())

//添加新的员工publicintempAdd(EmpBean emp) {intinsertInt = 0;

BaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}intnextEmpno =this.getNextEmpno();

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("INSERT INTO EMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) \n");

sqlBf.append(" VALUES(? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , TO_DATE(?, 'YYYYMMDD') \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ?) \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

idx= 1;

pstmt.setInt(idx++, nextEmpno);

pstmt.setString(idx++, emp.getEname());

pstmt.setString(idx++, emp.getJob());

pstmt.setInt(idx++, emp.getMgr());

pstmt.setString(idx++, StringUtil.formatString(emp.getHiredate()));

pstmt.setDouble(idx++, emp.getSal());

pstmt.setDouble(idx++, emp.getComm());

pstmt.setInt(idx++, emp.getDeptno());

insertInt=pstmt.executeUpdate();

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(rs, pstmt, conn);

}returninsertInt;

}

员工的service的全部代码如下:

packagecom.test.biz.service;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importcom.test.biz.bean.EmpBean;importcom.test.common.dao.BaseDao;importcom.test.common.util.StringUtil;publicclassEmpService {privateintidx = 1;

Connection conn=null;

PreparedStatement pstmt=null;

ResultSet rs=null;publicArrayList<EmpBean>getEmpList(EmpBean eb){

ArrayList<EmpBean> empList =newArrayList<EmpBean>();

BaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}//3. 执行SQL语句StringBuffer sqlBf =newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("SELECT EMPNO \n");

sqlBf.append(" , ENAME \n");

sqlBf.append(" , JOB \n");

sqlBf.append(" , MGR \n");

sqlBf.append(" , TO_CHAR(HIREDATE, 'YYYYMMDD') HIREDATE \n");

sqlBf.append(" , SAL \n");

sqlBf.append(" , COMM \n");

sqlBf.append(" , DEPTNO \n");

sqlBf.append("FROM EMP \n");

sqlBf.append("WHERE ENAME LIKE UPPER(?) || '%' \n");

sqlBf.append("ORDER BY EMPNO \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

idx= 1;

pstmt.setString(idx++, eb.getEname());//4. 获取结果集rs =pstmt.executeQuery();while(rs.next()) {

EmpBean emp=newEmpBean();

emp.setEmpno(rs.getInt("empno"));

emp.setEname(rs.getString("ename"));

emp.setJob(rs.getString("job"));

emp.setMgr(rs.getInt("mgr"));

emp.setHiredate(rs.getString("hiredate"));

emp.setSal(rs.getDouble("sal"));

emp.setComm(rs.getDouble("comm"));

emp.setDeptno(rs.getInt("deptno"));

empList.add(emp);

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(rs, pstmt, conn);

}returnempList;

}//利用empno查询单条员工信息publicEmpBean empById(intempno) {

EmpBean emp=newEmpBean();

BaseDao baseBao=newBaseDao();try{

conn=baseBao.dbConnection();

}catch(SQLException e) {

e.printStackTrace();

}

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("SELECT EMPNO \n");

sqlBf.append(" , ENAME \n");

sqlBf.append(" , JOB \n");

sqlBf.append(" , MGR \n");

sqlBf.append(" , TO_CHAR(HIREDATE, 'YYYY-MM-DD') HIREDATE \n");

sqlBf.append(" , SAL \n");

sqlBf.append(" , COMM \n");

sqlBf.append(" , DEPTNO \n");

sqlBf.append("FROM EMP \n");

sqlBf.append("WHERE EMPNO = ? \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

idx= 1;

pstmt.setInt(idx++, empno);

rs=pstmt.executeQuery();if(rs.next()) {

emp.setEmpno(rs.getInt("EMPNO"));

emp.setEname(rs.getString("ENAME"));

emp.setJob(rs.getString("JOB"));

emp.setMgr(rs.getInt("MGR"));

emp.setHiredate(rs.getString("HIREDATE"));

emp.setSal(rs.getDouble("SAL"));

emp.setComm(rs.getDouble("COMM"));

emp.setDeptno(rs.getInt("DEPTNO"));

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseBao.dbDisconnection(rs, pstmt, conn);

}returnemp;

}//更新emp信息publicintempSave(EmpBean empBean) {intupdateResulInt = 0;

BaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("UPDATE EMP SET ENAME = ? \n");

sqlBf.append(" , JOB = ? \n");

sqlBf.append(" , MGR = ? \n");

sqlBf.append(" , HIREDATE = TO_DATE(?, 'YYYYMMDD') \n");

sqlBf.append(" , SAL = ? \n");

sqlBf.append(" , COMM = ? \n");

sqlBf.append(" , DEPTNO = ? \n");

sqlBf.append("WHERE EMPNO = ? \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

idx= 1;

pstmt.setString(idx++, empBean.getEname());

pstmt.setString(idx++, empBean.getJob());

pstmt.setInt(idx++, empBean.getMgr());

pstmt.setString(idx++, StringUtil.formatString(empBean.getHiredate()));

pstmt.setDouble(idx++, empBean.getSal());

pstmt.setDouble(idx++, empBean.getComm());

pstmt.setInt(idx++, empBean.getDeptno());

pstmt.setInt(idx++, empBean.getEmpno());

updateResulInt=pstmt.executeUpdate();

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(null, pstmt, conn);

}returnupdateResulInt;

}//获取下一个员工号publicintgetNextEmpno() {intnextEmpno = 0;

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("SELECT MAX(EMPNO) + 1 AS EMPNO \n");

sqlBf.append("FROM EMP \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

rs=pstmt.executeQuery();if(rs.next()) {

nextEmpno= rs.getInt("EMPNO");

}

}catch(SQLException e) {

e.printStackTrace();

}returnnextEmpno;

}//添加新的员工publicintempAdd(EmpBean emp) {intinsertInt = 0;

BaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}intnextEmpno =this.getNextEmpno();

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("INSERT INTO EMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) \n");

sqlBf.append(" VALUES(? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , TO_DATE(?, 'YYYYMMDD') \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ?) \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

idx= 1;

pstmt.setInt(idx++, nextEmpno);

pstmt.setString(idx++, emp.getEname());

pstmt.setString(idx++, emp.getJob());

pstmt.setInt(idx++, emp.getMgr());

pstmt.setString(idx++, StringUtil.formatString(emp.getHiredate()));

pstmt.setDouble(idx++, emp.getSal());

pstmt.setDouble(idx++, emp.getComm());

pstmt.setInt(idx++, emp.getDeptno());

insertInt=pstmt.executeUpdate();

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(rs, pstmt, conn);

}returninsertInt;

}//删除一名员工publicintempDelete(intempno) {intdeleteResulInt = 0;

BaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("DELETE FROM EMP \n");

sqlBf.append("WHERE EMPNO = ? \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

idx= 1;

pstmt.setInt(idx++, empno);

deleteResulInt=pstmt.executeUpdate();

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(null, pstmt, conn);

}returndeleteResulInt;

}

}

页面三:访问日志查询页面

requestLogList.jsp

代码说明:

页面加载时设置初始日期值,默认值是当天日期。

<bodyonload="setInitDate();">

functionsetInitDate(){varmyDate =newDate();varmytime = myDate.getFullYear() + '-' + (myDate.getMonth() < 10 ?'0' + (myDate.getMonth() + 1) : myDate.getMonth() + 1) + '-' +myDate.getDate();if(document.getElementById('starttime').value == ""){

document.getElementById('starttime').value =mytime;

}

}

添加查询条件

访问日期:<inputid="starttime"type="text"name="starttime"value="${requestScope.starttime }"onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)"readonly="readonly"><

br>

点击查询时进行判断已经选择了日期

<inputtype="submit"value="Search"onclick="verDate()">

functionverDate(){

dateStr= document.getElementById('starttime').value;if(dateStr.length == 0){

alert("请选择日期!");returnfalse;

}

}

每个超链接中添加日期 starttime=${requestScope.starttime }

<ahref="/web01/requestInfoController?callTp=requestInfoPageList&starttime=${requestScope.starttime }&now_page_num=1">首页 |</a>

完整代码:

<%@ page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>访问日志查看</title><linkhref="/web01//css/main.css"rel="stylesheet"type="text/css"/><scripttype="text/javascript"src="/web01/js/DatePicker.js"></script><scripttype="text/javascript">functionsetInitDate(){varmyDate=newDate();varmytime=myDate.getFullYear()+'-'+(myDate.getMonth()<10?'0'+(myDate.getMonth()+1) : myDate.getMonth()+1)+'-'+myDate.getDate();if(document.getElementById('starttime').value==""){

document.getElementById('starttime').value=mytime;

}

}functionverDate(){

dateStr=document.getElementById('starttime').value;if(dateStr.length==0){

alert("请选择日期!");returnfalse;

}

}</script></head><bodyonload="setInitDate();"><%@ include file="top.jsp"%><h2>访问日志查询</h2><formaction="/web01/requestInfoController"method="get">访问日期:<inputid="starttime"type="text"name="starttime"value="${requestScope.starttime }"onfocus="setday(this,'yyyy-MM-dd','2010-01-01','2020-12-30',1)"readonly="readonly"><

br><inputtype="submit"value="Search"onclick="verDate()"><inputtype="hidden"name="callTp"value="requestInfoList"><

br/><table><tr><th>NO</th><th>contextPath</th><th>localAddr</th><th>localName</th><th>localPort</th><th>method</th><th>remoteAddr</th><th>remoteHost</th><th>remotePort</th><th>requestURI</th><th>requestURL</th><th>requestedsessionId</th><th>locale</th><th>regiDt</th></tr><c:forEachitems="${requestScope.requestInfoList}"var="requestInfo"><tr><td><c:outvalue="${requestInfo.rowSeq }"default=" "/></td><td><c:outvalue="${requestInfo.contextPath }"default=" "/></td><td><c:outvalue="${requestInfo.localAddr }"default=" "/></td><td><c:outvalue="${requestInfo.localName }"default=" "/></td><td><c:outvalue="${requestInfo.localPort }"default=" "/></td><td><c:outvalue="${requestInfo.method }"default=" "/></td><td><c:outvalue="${requestInfo.remoteAddr }"default=" "/></td><td><c:outvalue="${requestInfo.remoteHost }"default=" "/></td><td><c:outvalue="${requestInfo.remotePort }"default=" "/></td><td><c:outvalue="${requestInfo.requestURI }"default=" "/></td><td><c:outvalue="${requestInfo.requestURL }"default=" "/></td><td><c:outvalue="${requestInfo.requestedSessionId }"default=" "/></td><td><c:outvalue="${requestInfo.locale }"default=" "/></td><td><c:outvalue="${requestInfo.regiDt }"default=" "/></td></tr></c:forEach></table></form>总个数:<b>${sessionScope.ttlCnt}</b>页<

br>总页数:<b>${sessionScope.ttlPage}</b>页<

br><ahref="/web01/requestInfoController?callTp=requestInfoPageList&starttime=${requestScope.starttime }&now_page_num=1">首页 |</a><c:choose><c:whentest="${sessionScope.now_page_num==1}">上一页</c:when><c:otherwise><ahref="/web01/requestInfoController?callTp=requestInfoPageList&starttime=${requestScope.starttime }&now_page_num=${sessionScope.now_page_num - 1}">上一页 |</a></c:otherwise></c:choose><c:choose><c:whentest="${sessionScope.now_page_num==sessionScope.ttlPage}">下一页</c:when><c:otherwise><ahref="/web01/requestInfoController?callTp=requestInfoPageList&starttime=${requestScope.starttime }&now_page_num=${sessionScope.now_page_num + 1}">下一页 |</a></c:otherwise></c:choose><ahref="/web01/requestInfoController?callTp=requestInfoPageList&starttime=${requestScope.starttime }&now_page_num=${sessionScope.ttlPage}">尾页</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<scripttype="text/javascript">functionpageNum_Change(){varnow_page_num=document.getElementById("now_page_num").value;

window.open("/web01/requestInfoController?callTp=requestInfoPageList&starttime=${requestScope.starttime }&now_page_num="+now_page_num,"_self");

}</script>直接访问:<inputid="now_page_num"value="${sessionScope.now_page_num}"><inputtype="button"value="go"onclick="pageNum_Change()"><%@ include file="bottom.jsp"%></body></html>

控制器代码的处理,需要把request中的日期(查询日期)重新赋值到request中。

request.setAttribute("starttime", request.getParameter("starttime"));

控制器完成代码:

packagecom.test.system.controller;importjava.io.IOException;importjava.util.ArrayList;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.test.system.bean.RequestInfoBean;importcom.test.system.service.RequestInfoService;/*** Servlet implementation class RequestInfoController*/@WebServlet("/RequestInfoController")publicclassRequestInfoControllerextendsHttpServlet {privatestaticfinallongserialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/publicRequestInfoController() {super();

}/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

String callTp= request.getParameter("callTp");if(callTp.equals("requestInfoList")) {intnow_page_num = 1;

RequestInfoService ris=newRequestInfoService();

ArrayList<RequestInfoBean> requestInfoList = ris.getRequestInfoList("", now_page_num, request.getParameter("starttime"));

HttpSession session=request.getSession();//当前页面(第一次查询时设置成第一页)session.setAttribute("now_page_num", now_page_num);//总页数intttlPage = ris.getTtlPage(request.getParameter("starttime"));

session.setAttribute("ttlPage", ttlPage);//获取总数intttlCnt = ris.getTtlCount(request.getParameter("starttime"));

session.setAttribute("ttlCnt", ttlCnt);

request.setAttribute("starttime", request.getParameter("starttime"));

request.setAttribute("requestInfoList", requestInfoList);

request.getRequestDispatcher("/view/requestLogList.jsp").forward(request, response);

}elseif(callTp.equals("requestInfoPageList")) {

RequestInfoService ris=newRequestInfoService();

ArrayList<RequestInfoBean> requestInfoList = ris.getRequestInfoList("", Integer.parseInt(request.getParameter("now_page_num")), request.getParameter("starttime"));

HttpSession session=request.getSession();

session.setAttribute("now_page_num", request.getParameter("now_page_num"));//总页数intttlPage = ris.getTtlPage(request.getParameter("starttime"));

session.setAttribute("ttlPage", ttlPage);//获取总数intttlCnt = ris.getTtlCount(request.getParameter("starttime"));

session.setAttribute("ttlCnt", ttlCnt);

request.setAttribute("starttime", request.getParameter("starttime"));

request.setAttribute("requestInfoList", requestInfoList);

request.getRequestDispatcher("/view/requestLogList.jsp").forward(request, response);

}

}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {this.doGet(request, response);

}

}

日期处理的service层代码。在原有代码的基础上添加REGI_DT范围处理。

sqlBf.append("WHERE REGI_DT >= TO_DATE(?, 'YYYYMMDD') \n");

sqlBf.append("AND REGI_DT< (TO_DATE(?, 'YYYYMMDD') + 1) \n");

完成代码:

packagecom.test.system.service;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjavax.servlet.http.HttpServletRequest;importcom.test.common.Constant;importcom.test.common.dao.BaseDao;importcom.test.common.util.StringUtil;importcom.test.system.bean.RequestInfoBean;publicclassRequestInfoService {privateintidx = 1;privateConnection conn =null;privatePreparedStatement pstmt =null;privateResultSet rs =null;//保存request信息publicvoidsaveRequestInfo(HttpServletRequest request){

BaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("INSERT INTO REQUEST_INFO (REQUEST_INFO_SEQ \n");

sqlBf.append(" , CHARACTER_ENCODING \n");

sqlBf.append(" , CONTENT_TYPE \n");

sqlBf.append(" , CONTEXT_PATH \n");

sqlBf.append(" , LOCAL_ADDR \n");

sqlBf.append(" , LOCAL_NAME \n");

sqlBf.append(" , LOCAL_PORT \n");

sqlBf.append(" , METHOD \n");

sqlBf.append(" , REMOTE_ADDR \n");

sqlBf.append(" , REMOTE_HOST \n");

sqlBf.append(" , REMOTE_PORT \n");

sqlBf.append(" , REMOTE_USER \n");

sqlBf.append(" , REQUEST_URI \n");

sqlBf.append(" , REQUEST_URL \n");

sqlBf.append(" , REQUESTED_SESSION_ID \n");

sqlBf.append(" , LOCALE \n");

sqlBf.append(" , REGI_DT) \n");

sqlBf.append("VALUES(SEQ_REQUEST_INFO.NEXTVAL \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , ? \n");

sqlBf.append(" , SYSDATE) \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

idx= 1;

pstmt.setString(idx++, request.getCharacterEncoding());

pstmt.setString(idx++, request.getContentType());

pstmt.setString(idx++, request.getContextPath());

pstmt.setString(idx++, request.getLocalAddr());

pstmt.setString(idx++, request.getLocalName());

pstmt.setInt(idx++, request.getLocalPort());

pstmt.setString(idx++, request.getMethod());

pstmt.setString(idx++, request.getRemoteAddr());

pstmt.setString(idx++, request.getRemoteHost());

pstmt.setInt(idx++, request.getRemotePort());

pstmt.setString(idx++, request.getRemoteUser());

pstmt.setString(idx++, request.getRequestURI());

pstmt.setString(idx++, request.getRequestURL().toString() + "?callTp=" + request.getParameter("callTp"));

pstmt.setString(idx++, request.getRequestedSessionId());

pstmt.setString(idx++, request.getLocale().toString());inti =pstmt.executeUpdate();if(i == 1) {

System.out.println("##### save request success \n");

}else{

System.out.println("##### save request fail \n");

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(null, pstmt, conn);

}

}//查询ListpublicArrayList<RequestInfoBean> getRequestInfoList(String str,intnow_page_num, String startTime){

ArrayList<RequestInfoBean> requestInfoList =newArrayList<RequestInfoBean>();

BaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("SELECT A.* \n");

sqlBf.append("FROM (SELECT T1.* \n");

sqlBf.append(" , ROWNUM AS ROW_SEQ \n");

sqlBf.append(" FROM (SELECT CHARACTER_ENCODING \n");

sqlBf.append(" , CONTENT_TYPE \n");

sqlBf.append(" , CONTEXT_PATH \n");

sqlBf.append(" , LOCAL_ADDR \n");

sqlBf.append(" , LOCAL_NAME \n");

sqlBf.append(" , LOCAL_PORT \n");

sqlBf.append(" , METHOD \n");

sqlBf.append(" , REMOTE_ADDR \n");

sqlBf.append(" , REMOTE_HOST \n");

sqlBf.append(" , REMOTE_PORT \n");

sqlBf.append(" , REMOTE_USER \n");

sqlBf.append(" , REQUEST_URI \n");

sqlBf.append(" , REQUEST_URL \n");

sqlBf.append(" , REQUESTED_SESSION_ID \n");

sqlBf.append(" , LOCALE \n");

sqlBf.append(" , TO_CHAR(REGI_DT, 'YYYY/MM/DD HH24:MI:SS') REGI_DT \n");

sqlBf.append(" FROM REQUEST_INFO \n");

sqlBf.append(" WHERE REGI_DT >= TO_DATE(?, 'YYYYMMDD') \n");

sqlBf.append(" AND REGI_DT< (TO_DATE(?, 'YYYYMMDD') + 1) \n");

sqlBf.append(" ORDER BY REQUEST_INFO_SEQ DESC \n");

sqlBf.append(" ) T1 \n");

sqlBf.append(" WHERE ROWNUM < (? * ?) + 1 \n");

sqlBf.append(" ) A \n");

sqlBf.append("WHERE A.ROW_SEQ > (? * (? - 1)) \n");

sqlBf.append("ORDER BY A.ROW_SEQ \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

pstmt.setString(1, StringUtil.formatString(startTime));

pstmt.setString(2, StringUtil.formatString(startTime));

pstmt.setInt(3, Constant.UNIT_CNT);

pstmt.setInt(4, now_page_num);

pstmt.setInt(5, Constant.UNIT_CNT);

pstmt.setInt(6, now_page_num);

rs=pstmt.executeQuery();while(rs.next()) {

RequestInfoBean rib=newRequestInfoBean();

rib.setCharacterEncoding(rs.getString("CHARACTER_ENCODING"));

rib.setContentType(rs.getString("CONTENT_TYPE"));

rib.setContextPath(rs.getString("CONTEXT_PATH"));

rib.setLocalAddr(rs.getString("LOCAL_ADDR"));

rib.setLocalName(rs.getString("LOCAL_NAME"));

rib.setLocalPort(rs.getInt("LOCAL_PORT"));

rib.setMethod(rs.getString("METHOD"));

rib.setRemoteAddr(rs.getString("REMOTE_ADDR"));

rib.setRemoteHost(rs.getString("REMOTE_HOST"));

rib.setRemotePort(rs.getInt("REMOTE_PORT"));

rib.setRemoteUser(rs.getString("REMOTE_USER"));

rib.setRequestURI(rs.getString("REQUEST_URI"));

rib.setRequestURL(rs.getString("REQUEST_URL"));

rib.setRequestedSessionId(rs.getString("REQUESTED_SESSION_ID"));

rib.setLocale(rs.getString("LOCALE"));

rib.setRegiDt(rs.getString("REGI_DT"));

rib.setRowSeq(rs.getInt("ROW_SEQ"));

requestInfoList.add(rib);

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(rs, pstmt, conn);

}returnrequestInfoList;

}//获取记录总数publicintgetTtlCount(String startTime){intttlCnt = 0;//Total CountBaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("SELECT COUNT(1) TTL_CNT \n");

sqlBf.append("FROM REQUEST_INFO \n");

sqlBf.append("WHERE REGI_DT >= TO_DATE(?, 'YYYYMMDD') \n");

sqlBf.append("AND REGI_DT< (TO_DATE(?, 'YYYYMMDD') + 1) \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

pstmt.setString(1, StringUtil.formatString(startTime));

pstmt.setString(2, StringUtil.formatString(startTime));

rs=pstmt.executeQuery();if(rs.next()) {

ttlCnt= rs.getInt("TTL_CNT");

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(rs, pstmt, conn);

}returnttlCnt;

}//获取页数publicintgetTtlPage(String startTime){intttlPage = 0;//Total CountBaseDao baseDao=newBaseDao();try{

conn=baseDao.dbConnection();

}catch(SQLException e1) {

e1.printStackTrace();

}

StringBuffer sqlBf=newStringBuffer();

sqlBf.setLength(0);

sqlBf.append("SELECT COUNT(1) TTL_CNT \n");

sqlBf.append("FROM REQUEST_INFO \n");

sqlBf.append("WHERE REGI_DT >= TO_DATE(?, 'YYYYMMDD') \n");

sqlBf.append("AND REGI_DT< (TO_DATE(?, 'YYYYMMDD') + 1) \n");try{

pstmt=conn.prepareStatement(sqlBf.toString());

pstmt.setString(1, StringUtil.formatString(startTime));

pstmt.setString(2, StringUtil.formatString(startTime));

rs=pstmt.executeQuery();if(rs.next()) {

ttlPage= rs.getInt("TTL_CNT") % Constant.UNIT_CNT == 0 ? rs.getInt("TTL_CNT") / Constant.UNIT_CNT : rs.getInt("TTL_CNT") / Constant.UNIT_CNT + 1;

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

baseDao.dbDisconnection(rs, pstmt, conn);

}returnttlPage;

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航