JSP条件查询翻页问题
在JSP的页面中包含输入条件查询,如果没有输入条件则显示全部内容,输入条件后查询第一页正常,但是翻页后条件就无效了,谁能告诉我个解决办法.
在点下一页的时候把条件的参数带上好象不管用,而且麻烦,哪位高手遇到过这样的问题给哦出个主意.
[解决办法]
我也是在翻页时把条件代上,很管用啊
[解决办法]
翻页也要把条件带上
[解决办法]
可以在处理第一页时将条件用bean包装后存在session里,翻页时从session里取出条件即可,大概意思路:
servlet() {
if(有查询条件) {
将条件值存入bean;
将bean存入session;
}
从session中取bean;
将bean与页码一起包装成sql语句;
获取值返回页面;
}
这只是一个思路,不一定很合理,你可以参考一下。
[解决办法]
有2种方法:
1、直接传条件;
2、方到session中来处理。
[解决办法]
<script language= "Javascript ">
function goUrl(url) {
document.formPage.action = url;
document.formPage.target = "_self ";
document.formPage.submit();
}
</script>
<form name= "formPage " " action= " " method= "post " >
<input name= "Str_select " type= "hidden " id= "Str_select " value= " <%=Str_select%> " size= "10 " maxlength= "10 ">
<a href= "javaScript:goUrl( 'marriage_licence_query.jsp?page= <%=pageNow+1%> ') "> <img src= " <%=strLevel%> /skin/image1/common/page_next.gif " alt= "后一页 " border= "0 "> </a>
<a href= "javaScript:goUrl( 'marriage_licence_query.jsp?page= <%=pageTotal%> ') "> <img src= " <%=strLevel%> skin/image1/common/page_last.gif " alt= "尾页 " border= "0 "> </a>
</form>
String Str_select=request.getParameter( "Str_select ");
if(request.getParameter( "Str_select ").equals( " ")){
查询的一些条件.
}
HQuery hquery = new HQuery();
hquery.setQueryString(Str_select);
[解决办法]
我用的是笨办法,但好使,传sql语句!
我曾经发帖问过有没有更好的办法,到现在还没有答案!呵呵!
[解决办法]
传SQL语句,甚至传结果集也可以,
我都试过,都放在session中,在初始化时,即第一次进action的时候,判断是否有页数的那个地方,把session清空一下,就行了
[解决办法]
翻页带条件就好了 跳到下一页
[解决办法]
1、条件放入session
2、form 中增加hidden 属性;
3、传sql文
都可以
但是最好别传结果集,当数据量过大时,画面显示的速度会非常慢!
[解决办法]
String sql_search = "select * from table ";
request.getSession().setAttribute( "sql_search ", sql_search);
List list = db.select(sql_search, 10);
if(!list.isEmpty()){
request.setAttribute( "flag ", "yes ");
}
else{
request.setAttribute( "flag ", "no ");
}
return new ActionForward( "/search.jsp ");
}
-------------------------------
search.jsp:
<logic:notEmpty name= "flag ">
<logic:equal name= "flag " value= "yes ">
<script>
window.opener.location= 'selectAll.do?action=selectAll&name=one ';
window.opener.alert( "查询成功! ");
window.close();
</script>
</logic:equal>
<logic:equal name= "flag " value= "no ">
<script>
window.alert( "对不起,没有查到相关记录! ");
window.history.go(-1);
</script>
</logic:equal>
</logic:notEmpty>
---------------------------------
selectAll.do:
public ActionForward selectAll()
{
String name = request.getParameter( "name ");
if(name.equals( "all "))
{
//查询所有
}
if(name.equals( "one ")) //条件查询
{
String sql = request.getSession().getAttribute( "sql_search ").toString();
List list = new DB(ds).select(sql,10);
//对list进行分页
}
return new ActionForward( "/list.jsp ");
}
以上是我最近一次练习时使用的,传sql语句的,用起来还可以,我在另一个帖子里也发过
[解决办法]
我正在用的:
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.*,com.v246.sql.*,com.v246.utils.*,java.util.*,com.v246.* " errorPage= " " %>
<%
String thisPageName = request.getRequestURI();
thisPageName = thisPageName.substring(thisPageName.lastIndexOf( "/ ")+1,thisPageName.length());
String type = request.getParameter( "type ");
String disPageName=(Aqu.vacancy(type))?thisPageName+ "? ":thisPageName+ "?type= "+type+ "& ";
Session sessions = new Session();
//分页
Pagination p = sessions.createPagination();//创建用于分页的类
String pageTmp = request.getParameter( "page ");
int pages = 1;
if(!Aqu.vacancy(pageTmp))
{
pages = Integer.parseInt(pageTmp);
}
p.setDisCount(15);//设置分页大小
p.setTable(ArticleConst.infoTable);//设置分页的表
p.setPrimaryKey( "id ");
p.setOrder(Const.desc);
if(!Aqu.vacancy(type))
{
p.setTerm( "type= "+type);
}
ResultSet rs = p.gotoPage(pages);
%>
<html>
<script language= "Javascript1.2 ">
<!-- // load htmlarea
_editor_url = "editor/ "; // URL to htmlarea files
var win_ie_ver = parseFloat(navigator.appVersion.split( "MSIE ")[1]);
if (navigator.userAgent.indexOf( 'Mac ') > = 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf( 'Windows CE ') > = 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf( 'Opera ') > = 0) { win_ie_ver = 0; }
if (win_ie_ver > = 5.5) {
document.write( ' <scr ' + 'ipt src= " ' +_editor_url+ 'editor.js " ');
document.write( ' language= "Javascript1.2 "> </scr ' + 'ipt> ');
} else { document.write( ' <scr '+ 'ipt> function editor_generate() { return false; } </scr '+ 'ipt> '); }
// -->
</script>
<head>
<script language 'javascript '>
function go(type)
{
window.location.href= " <%=thisPageName%> ?type= "+type;
//alert( " <%=thisPageName%> ?type= "+type);
}
</script>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 健康生活管理 </title>
<link href= "../css/style.css " rel= "stylesheet " type= "text/css ">
<style type= "text/css ">
<!--
.STYLE1 {
font-size: 9pt;
font-weight: normal;
}
.STYLE6 {font-size: 9pt}
.www {font-family: "宋体 ";
font-size: 12px;
}
.STYLE7 {font-size: 14px;
font-weight: bold;
}
-->
</style>
</head>
<body style= "background-color: #cad7f7; ">
<table width= "80% " border= "0 " align= "center " cellpadding= "3 " cellspacing= "1 " class= "STYLE1 ">
<!--DWLayoutTable-->
<tr align= "center " >
<td height= "12 " colspan= "4 " background= "../images/admin_top_bg.gif "> <span class= "STYLE7 "> 后台管理------> <%=ArticleConst.name%> 文章管理 </span> </td>
</tr>
<tr align= "center " >
<td height= "12 " colspan= "4 " align= "left " background= "../images/admin_top_bg.gif "> <select name= "type " id= "type " onChange= "go(this.options[this.selectedIndex].value) ">
<option value= "-1 "> 请选择要管理的栏目 </option>
<%
Map map = ArticleConst.getInstance().getMap();
Map mapID = ArticleConst.getInstance().getMapID();
Iterator it = map.keySet().iterator();
while(it.hasNext())
{
Object o = it.next();
%>
<option value= " <%=o%> "> <%=map.get(o)%> </option>
<%
}
%>
</select> </td>
</tr>
<tr bgcolor= "#f1f3f5 ">
<td width= "440 " height= "22 " align= "center "> 文章标题 </td>
<td width= "165 " align= "center "> 发布日期 </td>
<td width= "50 " align= "center "> 点击数 </td>
<td width= "112 " rowspan= "2 " align= "center "> 操作 </td>
</tr>
<tr bgcolor= "#f1f3f5 ">
<td> </td>
<td> </td>
<td> </td>
</tr>
<%
while(rs.next())
{
%>
<tr bgcolor= "#f1f3f5 ">
<td height= "25 "> [ <a href= ' <%=thisPageName+ "?type= "+rs.getInt( "type ")%> '> <%=map.get(Integer.valueOf(rs.getInt( "type ")))%> </a> ] <%=rs.getString( "title ")%> </td>
<td> <%=rs.getString( "releaseDate ")%> </td>
<td> <%=rs.getInt( "click ")%> </td>
<td width= "112 " align= "center "> <a href= " <%=ArticleConst.path%> /nr.jsp?id= <%=rs.getInt( "id ")%> "> 查看 </a> | <a href= "modifyInfo.jsp?id= <%=rs.getInt( "id ")%> &action=del "> 修改 </a> | <a href= 'deleteInfo.jsp?id= <%=rs.getInt( "id ")%> '> 删除 </a> </td>
</tr>
<%
}
sessions.close();
%>
<tr bgcolor= "#f1f3f5 ">
<td height= "20 " colspan= "4 " align= "right " valign= "top "> <label> <span class= "www "> 共有 <%=p.getDataCount()%> 项查询结果 目前显示 第 <%=(p.getCurrentPage()==1)?1:(p.getDisCount())*(p.getCurrentPage())-p.getDisCount()%> - <%=(p.getDisCount())*(p.getCurrentPage())%> 项 <a href= ' <%=disPageName%> page=1 '> 首页 </a> <a href= ' <%=disPageName%> page= <%=(p.getCurrentPage())-1%> '> 上一页 </a> <a href= ' <%=disPageName%> page= <%=(p.getCurrentPage())+1%> '> 下一页 </a> <a href= ' <%=disPageName%> page= <%=p.getPageCount()%> '> 末页 </a> </span> </label> </td>
</tr>
</table>
<br>
</body>
</html>
[解决办法]
我现在正在用的,希望能给你点启发
<%@ page contentType= "text/html; charset=gbk " language= "java " import= "java.sql.*,com.v246.sql.*,com.v246.utils.*,java.util.*,com.v246.* " errorPage= " " %>
<%
String thisPageName = request.getRequestURI();
thisPageName = thisPageName.substring(thisPageName.lastIndexOf( "/ ")+1,thisPageName.length());
String type = request.getParameter( "type ");
String disPageName=(Aqu.vacancy(type))?thisPageName+ "? ":thisPageName+ "?type= "+type+ "& ";
Session sessions = new Session();
//分页
Pagination p = sessions.createPagination();//创建用于分页的类
String pageTmp = request.getParameter( "page ");
int pages = 1;
if(!Aqu.vacancy(pageTmp))
{
pages = Integer.parseInt(pageTmp);
}
p.setDisCount(15);//设置分页大小
p.setTable(ArticleConst.infoTable);//设置分页的表
p.setPrimaryKey( "id ");
p.setOrder(Const.desc);
if(!Aqu.vacancy(type))
{
p.setTerm( "type= "+type);
}
ResultSet rs = p.gotoPage(pages);
%>
<html>
<script language= "Javascript1.2 ">
<!-- // load htmlarea
_editor_url = "editor/ "; // URL to htmlarea files
var win_ie_ver = parseFloat(navigator.appVersion.split( "MSIE ")[1]);
if (navigator.userAgent.indexOf( 'Mac ') > = 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf( 'Windows CE ') > = 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf( 'Opera ') > = 0) { win_ie_ver = 0; }
if (win_ie_ver > = 5.5) {
document.write( ' <scr ' + 'ipt src= " ' +_editor_url+ 'editor.js " ');
document.write( ' language= "Javascript1.2 "> </scr ' + 'ipt> ');
} else { document.write( ' <scr '+ 'ipt> function editor_generate() { return false; } </scr '+ 'ipt> '); }
// -->
</script>
<head>
<script language 'javascript '>
function go(type)
{
window.location.href= " <%=thisPageName%> ?type= "+type;
//alert( " <%=thisPageName%> ?type= "+type);
}
</script>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 健康生活管理 </title>
<link href= "../css/style.css " rel= "stylesheet " type= "text/css ">
<style type= "text/css ">
<!--
.STYLE1 {
font-size: 9pt;
font-weight: normal;
}
.STYLE6 {font-size: 9pt}
.www {font-family: "宋体 ";
font-size: 12px;
}
.STYLE7 {font-size: 14px;
font-weight: bold;
}
-->
</style>
</head>
<body style= "background-color: #cad7f7; ">
<table width= "80% " border= "0 " align= "center " cellpadding= "3 " cellspacing= "1 " class= "STYLE1 ">
<!--DWLayoutTable-->
<tr align= "center " >
<td height= "12 " colspan= "4 " background= "../images/admin_top_bg.gif "> <span class= "STYLE7 "> 后台管理------> <%=ArticleConst.name%> 文章管理 </span> </td>
</tr>
<tr align= "center " >
<td height= "12 " colspan= "4 " align= "left " background= "../images/admin_top_bg.gif "> <select name= "type " id= "type " onChange= "go(this.options[this.selectedIndex].value) ">
<option value= "-1 "> 请选择要管理的栏目 </option>
<%
Map map = ArticleConst.getInstance().getMap();
Map mapID = ArticleConst.getInstance().getMapID();
Iterator it = map.keySet().iterator();
while(it.hasNext())
{
Object o = it.next();
%>
<option value= " <%=o%> "> <%=map.get(o)%> </option>
<%
}
%>
</select> </td>
</tr>
<tr bgcolor= "#f1f3f5 ">
<td width= "440 " height= "22 " align= "center "> 文章标题 </td>
<td width= "165 " align= "center "> 发布日期 </td>
<td width= "50 " align= "center "> 点击数 </td>
<td width= "112 " rowspan= "2 " align= "center "> 操作 </td>
</tr>
<tr bgcolor= "#f1f3f5 ">
<td> </td>
<td> </td>
<td> </td>
</tr>
<%
while(rs.next())
{
%>
<tr bgcolor= "#f1f3f5 ">
<td height= "25 "> [ <a href= ' <%=thisPageName+ "?type= "+rs.getInt( "type ")%> '> <%=map.get(Integer.valueOf(rs.getInt( "type ")))%> </a> ] <%=rs.getString( "title ")%> </td>
<td> <%=rs.getString( "releaseDate ")%> </td>
<td> <%=rs.getInt( "click ")%> </td>
<td width= "112 " align= "center "> <a href= " <%=ArticleConst.path%> /nr.jsp?id= <%=rs.getInt( "id ")%> "> 查看 </a> | <a href= "modifyInfo.jsp?id= <%=rs.getInt( "id ")%> &action=del "> 修改 </a> | <a href= 'deleteInfo.jsp?id= <%=rs.getInt( "id ")%> '> 删除 </a> </td>
</tr>
<%
}
sessions.close();
%>
<tr bgcolor= "#f1f3f5 ">
<td height= "20 " colspan= "4 " align= "right " valign= "top "> <label> <span class= "www "> 共有 <%=p.getDataCount()%> 项查询结果 目前显示 第 <%=(p.getCurrentPage()==1)?1:(p.getDisCount())*(p.getCurrentPage())-p.getDisCount()%> - <%=(p.getDisCount())*(p.getCurrentPage())%> 项 <a href= ' <%=disPageName%> page=1 '> 首页 </a> <a href= ' <%=disPageName%> page= <%=(p.getCurrentPage())-1%> '> 上一页 </a> <a href= ' <%=disPageName%> page= <%=(p.getCurrentPage())+1%> '> 下一页 </a> <a href= ' <%=disPageName%> page= <%=p.getPageCount()%> '> 末页 </a> </span> </label> </td>
</tr>
</table>
<br>
</body>
</html>
[解决办法]
就看你的查询分页是怎么做的了。
如果你是把结果全部查询出来后,把结果集放在session中,那么你就可以直接对 这个结果集进行操作,不管你翻多少页,也只需要对这个结果集进行定位就可以了,显示当前的几行。
你完全不需要在显示下一页的时候,再去访问数据库查询,这个效率是比较低的。
用结果集是比较好的方法,除非你的数据特别的庞大,至于这个上限是多少,我没有测试过,不过保守的说,一万行的数据的结果集应该也不会太大。
------解决方案--------------------
如果你用的是struts框架的话,就没有那么麻烦了。所以条件都会保存在actionForm中。
直接点下页就可以了!!!
[解决办法]
如果是学习,手写分页;
如果是开发系统,建议采用第三方页面结构,那样你的LIST页面风格不但统一且CSV,PDF,XML,等导出全部结合,相当happy~