这两天的工作是项目中"综合模块"的系统日志部分,内容包括查询和显示,其实都不算太难,无非就是查询+分页。但偏偏 就遇到了一个问题 ,令我困扰不已。现将问题叙述如下:
页面分为两部分,上部分为查询输入,下部分为数据显示。
在未输入查询条件前,页面下部分显示的是所有数据(当然也是分页)。
当查得所需数据时,由于数据量过大,也需分页显示。
比如查询结果分为两页显示,在点击显示下一页内容后,本应该是跳转至查询所得结果的第二页,但是问题却出现了,系统是跳转至了未查询数据的第二页!!!!
代码如下:
<
form action
=
"
xtrz.jsp
"
method
=
"
post
"
>
< table width = " 500 " align = " center " cellpadding = " 0 " cellspacing = " 1 " class = " table " >
< tr class = " upTd " >
< td height = " 25 " colspan = " 4 " align = " center " > 系统日志 </ td >
</ tr >
< tr >
< td align = " center " > 开始时间 </ td >
< td align = " center " >< input type = " text " name = " starttime " value = ' <c:out value="<%=request.getParameter("starttime")%>"/> ' /></ td >
< td align = " center " > 结束时间 </ td >
< td align = " center " >< input type = " text " name = " stoptime " value = ' <c:out value="<%=request.getParameter("stoptime")%>"/> ' /></ td >
</ tr >
< tr >
< td height = " 25 " colspan = " 4 " align = " center " >
< input type = " submit " name = " query " value = " 查询 " >& nbsp; & nbsp; & nbsp; & nbsp;
< input type = " reset " name = " reset " value = " 重置 " ></ td >
</ tr >
</ table >
</ form >
< br />
< table width = " 500 " align = " center " cellpadding = " 0 " cellspacing = " 1 " class = " table_B " >
< tr class = " upTd " >
< td height = " 22 " align = " center " > 级别 </ td >
< td align = " center " > 时间 </ td >
< td align = " center " > 模块 </ td >
< td align = " center " > 操作 </ td >
</ tr >
<%
Connection conn = Database.getConnection();
int perpg = 15 ; // 每页显示条数
int pg = 1 ; // 当前页数记录
int count = 0 ; // 取得数据数目
int count2 = 0 ; // 记录条数
int pagenum; // 总页数
String pgsql = "" ;
String sql = "" ;
String status = "" ; // 查询条件
if (request.getParameter( " starttime " ) != null && ! request.getParameter( " starttime " ).equals( "" )) {
status = status + " and logdate > cast(' " + request.getParameter( " starttime " ) + " ' as datetime) " ;
}
if (request.getParameter( " stoptime " ) != null && ! request.getParameter( " stoptime " ).equals( "" )) {
status = status + " and logdate < cast(' " + request.getParameter( " stoptime " ) + " ' as datetime) " ;
}
pgsql = " select count(*) as num from TB_LOG4J where priority = 'info' " + status;
// System.out.println(pgsql);
ResultSet res = Database.SQLQuery(conn,pgsql);
if (res.next()) { // 查得数据条数
count = res.getInt( " num " );
count2 = count;
}
res.close();
for (pagenum = 0 ;count > 0 ;pagenum ++ ) { // 分页
count = count - perpg;
}
if (request.getParameter( " page " ) != null && ! request.getParameter( " page " ).equals( "" )) {
pg = Integer.parseInt(request.getParameter( " page " ));
if (pg > pagenum) {
pg = pagenum;
}
else if (pg <= 1 ) {
pg = 1 ;
}
}
sql = " select top 15 * from TB_LOG4J where logdate not in (select top " + (pg - 1 ) * perpg + " logdate from TB_LOG4J where priority = 'info' " + status + " order by logdate desc) and priority='info' " + status + " order by logdate desc " ; // sql server
// System.out.println(sql);
res = Database.SQLQuery(conn,sql);
while (res.next()) { %>
< tr >
< td height = " 22 " align = " center " ><%= res.getString( " priority " ) %></ td >
< td align = " center " ><%= res.getString( " logdate " ) %></ td >
< td align = " center " ><%= res.getString( " class " ) %></ td >
< td align = " center " ><%= res.getString( " msg " ) %></ td >
</ tr >
<% }
res.close();
conn.close();
%>
</ table >
< table width = " 500 " border = " 0 " align = " center " cellpadding = " 0 " cellspacing = " 1 " >
< tr >
< td >
<% if (pagenum > 1 && pg != 1 ) { %>< a href = " xtrz.jsp?page=1 &&status=<%=("status")%> " > 首页 </ a ><% } %>
<% if (pagenum > 1 && pg > 1 ) { %>< a href = " xtrz.jsp?page=<%=pg-1%>&&status=<%=("status")%> " > 前一页 </ a > <% } %>
<% if (pagenum > 1 && pg < pagenum) { %>< a href = " xtrz.jsp?page=<%=pg+1%>&&status=<%=("status")%> " > 下一页 </ a > <% } %>
<% if (pagenum > 1 && pg != pagenum) { %>< a href = " xtrz.jsp?page=<%=pagenum%>&&status=<%=("status")%> " > 尾页 </ a ><% } %>
共 <%= count2 %> 条记录 & nbsp; /& nbsp; <%= pagenum %> 页,当前是 <%= pg %> 页 </ td >
</ tr >
</ table >
< table width = " 500 " align = " center " cellpadding = " 0 " cellspacing = " 1 " class = " table " >
< tr class = " upTd " >
< td height = " 25 " colspan = " 4 " align = " center " > 系统日志 </ td >
</ tr >
< tr >
< td align = " center " > 开始时间 </ td >
< td align = " center " >< input type = " text " name = " starttime " value = ' <c:out value="<%=request.getParameter("starttime")%>"/> ' /></ td >
< td align = " center " > 结束时间 </ td >
< td align = " center " >< input type = " text " name = " stoptime " value = ' <c:out value="<%=request.getParameter("stoptime")%>"/> ' /></ td >
</ tr >
< tr >
< td height = " 25 " colspan = " 4 " align = " center " >
< input type = " submit " name = " query " value = " 查询 " >& nbsp; & nbsp; & nbsp; & nbsp;
< input type = " reset " name = " reset " value = " 重置 " ></ td >
</ tr >
</ table >
</ form >
< br />
< table width = " 500 " align = " center " cellpadding = " 0 " cellspacing = " 1 " class = " table_B " >
< tr class = " upTd " >
< td height = " 22 " align = " center " > 级别 </ td >
< td align = " center " > 时间 </ td >
< td align = " center " > 模块 </ td >
< td align = " center " > 操作 </ td >
</ tr >
<%
Connection conn = Database.getConnection();
int perpg = 15 ; // 每页显示条数
int pg = 1 ; // 当前页数记录
int count = 0 ; // 取得数据数目
int count2 = 0 ; // 记录条数
int pagenum; // 总页数
String pgsql = "" ;
String sql = "" ;
String status = "" ; // 查询条件
if (request.getParameter( " starttime " ) != null && ! request.getParameter( " starttime " ).equals( "" )) {
status = status + " and logdate > cast(' " + request.getParameter( " starttime " ) + " ' as datetime) " ;
}
if (request.getParameter( " stoptime " ) != null && ! request.getParameter( " stoptime " ).equals( "" )) {
status = status + " and logdate < cast(' " + request.getParameter( " stoptime " ) + " ' as datetime) " ;
}
pgsql = " select count(*) as num from TB_LOG4J where priority = 'info' " + status;
// System.out.println(pgsql);
ResultSet res = Database.SQLQuery(conn,pgsql);
if (res.next()) { // 查得数据条数
count = res.getInt( " num " );
count2 = count;
}
res.close();
for (pagenum = 0 ;count > 0 ;pagenum ++ ) { // 分页
count = count - perpg;
}
if (request.getParameter( " page " ) != null && ! request.getParameter( " page " ).equals( "" )) {
pg = Integer.parseInt(request.getParameter( " page " ));
if (pg > pagenum) {
pg = pagenum;
}
else if (pg <= 1 ) {
pg = 1 ;
}
}
sql = " select top 15 * from TB_LOG4J where logdate not in (select top " + (pg - 1 ) * perpg + " logdate from TB_LOG4J where priority = 'info' " + status + " order by logdate desc) and priority='info' " + status + " order by logdate desc " ; // sql server
// System.out.println(sql);
res = Database.SQLQuery(conn,sql);
while (res.next()) { %>
< tr >
< td height = " 22 " align = " center " ><%= res.getString( " priority " ) %></ td >
< td align = " center " ><%= res.getString( " logdate " ) %></ td >
< td align = " center " ><%= res.getString( " class " ) %></ td >
< td align = " center " ><%= res.getString( " msg " ) %></ td >
</ tr >
<% }
res.close();
conn.close();
%>
</ table >
< table width = " 500 " border = " 0 " align = " center " cellpadding = " 0 " cellspacing = " 1 " >
< tr >
< td >
<% if (pagenum > 1 && pg != 1 ) { %>< a href = " xtrz.jsp?page=1 &&status=<%=("status")%> " > 首页 </ a ><% } %>
<% if (pagenum > 1 && pg > 1 ) { %>< a href = " xtrz.jsp?page=<%=pg-1%>&&status=<%=("status")%> " > 前一页 </ a > <% } %>
<% if (pagenum > 1 && pg < pagenum) { %>< a href = " xtrz.jsp?page=<%=pg+1%>&&status=<%=("status")%> " > 下一页 </ a > <% } %>
<% if (pagenum > 1 && pg != pagenum) { %>< a href = " xtrz.jsp?page=<%=pagenum%>&&status=<%=("status")%> " > 尾页 </ a ><% } %>
共 <%= count2 %> 条记录 & nbsp; /& nbsp; <%= pagenum %> 页,当前是 <%= pg %> 页 </ td >
</ tr >
</ table >
说明:数据库系SQL server,语言为Jsp。
总结原因:1、页面传参方式错误,理由是status并未传至下一页
2、定义变量错误,原因不明
进度:尚未解决!!!