最近碰到了一个问题就是:在同一个JSP页面里面,当用户第一次进来的时候。需要显示的是所有的记录数(分页显示),如果用户选择了条件,后面就应该显示根据用户选择的条件进行分页而在JSP里面固定的上一页 或者 下一页 的链接还是原来显示所有的连接。这个问题该怎么优化(处理)?
部分代码如下:
<c:if test="${showPage>1}">
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=1">首页</a>
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${showPage-1}">上一页</a>
</c:if>
<c:if test="${showPage<maxPage || showPage==maxPage}">
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${showPage+1}">下一页</a>
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${maxPage}">尾页</a>
</c:if>第<font color='#FF0000'>${showPage}</font>页⁄ 共<font color='#FF0000'>${maxPage}</font>页
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=1">返回所有</a>
部分代码如下:
<c:if test="${showPage>1}">
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=1">首页</a>
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${showPage-1}">上一页</a>
</c:if>
<c:if test="${showPage<maxPage || showPage==maxPage}">
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${showPage+1}">下一页</a>
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=${maxPage}">尾页</a>
</c:if>第<font color='#FF0000'>${showPage}</font>页⁄ 共<font color='#FF0000'>${maxPage}</font>页
<a href="MessageInfoServlet?act=selectAllMessageInfoPage&page=1">返回所有</a>
解决方案 »
- 请教下关于JSP中的超链接问题
- 该怎样总结网站的框架
- 如何在JSP里网站首页自动选择语言转跳?
- JSF的初级问题,就是调不通,不知道什么原因。
- struts2 s:iterator 循环速度太慢
- 关于配置tomcat5.0.30在建立自己的bean时出现的问题!紧急~~~
- su.downloadFile("/upload/"+filename+"");我想输出括号里的值要怎么写:System.out.println(在这里);
- jsp中怎样获取客户端浏览器和操作系统的类型?
- 连接数据库,怎样在jsp页面跳转后保持数据库连接?在线等~~
- response.sendRedirect()中文问题
- java.lang.NullPointerException空指针异常
- J2EE平台 web报表
3楼的,用CSS如何实现?发一下代码.
一、第一次进来:
要分页显示全部的。并能实现上一页。下一页
二、当用户选择了条件后。
要根据用户选中的条件来分页显示上一页,下一页。。
这个地方的分页 sql 语句怎么写? 全部的可以。当是在原来的基础上不能加入条件。这个问题 怎么解决呀。还请大家帮帮忙? 谢谢了
如果是hibernate,楼主只需要改变你想看的页数就可以了,不需要条件的。如果是jdbc,因为每一次查询都需要条件,所以每次传入页码的时候,还是要把条件传过去就可以了啊。
想不到有什么问题。
有需要的朋友可以去看看
import="java.sql.*" errorPage=""%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="data.*"%>
<jsp:useBean id="pages" scope="page" class="data.strongSplitPage" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%!//显示每页的纪录数
int pageSize = 5;
String sqlStr = "";
//当前页
int showPage = 1; String cloth = "";
String clothes = "";
String search = "";
String searches = "";%><%
if (request.getParameter("search_info") != null)
cloth = request.getParameter("search_info");
if (request.getParameter("info_Name") != null)
search = request.getParameter("info_Name");
try {
clothes = new String(cloth.getBytes("ISO-8859-1"), "gbk");
searches = new String(search.getBytes("ISO-8859-1"), "gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} //str = "select * from clothes";
if (clothes.equals("服饰")) {
//str = "select * from clothes";
if (search.equals("") || search == null) {
sqlStr = "select * from clothes";
} else {
sqlStr = "select * from clothes where clothes_Name like '%"
+ searches + "%'";
}
} else if (clothes.equals("美食")) {
//str = "select * from food";
if (search.equals("") || search == null) {
sqlStr = "select * from food";
} else {
sqlStr = "select * from food where food_Name like '%"
+ searches + "%'";
}
} String strPage = null;
//获得跳转到的页面
strPage = request.getParameter("showPage");
if (strPage == null) {
showPage = 1;
pages.initialize(sqlStr, pageSize, showPage);
} else {
try {
showPage = Integer.parseInt(strPage);
pages.initialize(sqlStr, pageSize, showPage);
} catch (NumberFormatException ex) {
showPage = 1;
pages.initialize(sqlStr, pageSize, showPage);
}
if (showPage < 1) {
showPage = 1;
pages.initialize(sqlStr, pageSize, showPage);
}
if (showPage > pages.getPageCount()) {
showPage = pages.getPageCount();
pages.initialize(sqlStr, pageSize, showPage);
}
}
//取得要显示的数据集合
Vector vData = pages.getPage();
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>分页显示</title>
</head> <body bgcolor="#ffffff" text="#000000">
<h1 align=center>
个人基本信息
</h1>
<div align=center>
<table border="1" cellspacing="0" cellpadding="0" width="80%">
<%
for (int i = 0; i < vData.size(); i++) {
//显示数据数
String[] sData = (String[]) vData.get(i);
%>
<tr>
<td><%=sData[0]%></td>
<td><%=sData[1]%></td>
<td><%=sData[2]%></td>
</tr>
<%
}
%>
</table>
<p>
<form action="MyJsp.jsp" method="get" target="_self">
<p>
共
<font color=red><%=pages.getRowCount()%></font>条 <%=pageSize%>条/页 第
<font color=red><%=showPage%></font>页/共
<font color=red><%=pages.getPageCount()%></font>页 [
<a href="MyJsp.jsp?showPage=1" target="_self">首页</a>]
<%
//判断“上一页”链接是否要显示
if (showPage > 1) {
%>
[
<a href="MyJsp.jsp?showPage=<%=showPage - 1%>" target="_self">上一页</a>]
<%
} else {
%>
[上一页]
<%
}
//判断“下一页”链接是否显示
if (showPage < pages.getPageCount()) {
%>
[
<a href="MyJsp.jsp?showPage=<%=showPage + 1%>" target="_self">下一页</a>]
<%
} else {
%>
[下一页]
<%
}
%> [
<a href="MyJsp.jsp?showPage=<%=pages.getPageCount()%>"
target="_self">尾页</a>] 转到
<select name="select">
<%
for (int x = 1; x <= pages.getPageCount(); x++) {
%>
<option value="<%=x%>"
<%
if(showPage==x){
out.println("selected");
}
%>><%=x%></option>
<%
}
%>
</select>
页
<input type="submit" name="go" value="提交" />
</p>
</form>
</p>
</div>
</body>
</html>
package data;import java.sql.*;
import java.util.*;public class strongSplitPage {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private ResultSetMetaData rsmd = null;
// sql 查询语句
private String sqlStr;
// 总纪录数目
private int rowCount;
// 所分得逻辑页数
private int pageCount;
// 每页显示的纪录数目
private int pageSize;
// 定义表的列数目
private int columnCount;
private int irows; public void initialize(String sqlStr, int pageSize, int showPage) {
this.sqlStr = sqlStr;
this.irows = pageSize * (showPage - 1);
this.pageSize = pageSize;
try {
LoginData loginData = new data.LoginData();
this.conn = loginData.getConn();
this.stmt = this.conn.createStatement();
this.rs = this.stmt.executeQuery(this.sqlStr);
this.rsmd = this.rs.getMetaData();
if (this.rs != null) {
this.rs.last();
this.rowCount = this.rs.getRow();
this.rs.first();
this.columnCount = this.rsmd.getColumnCount();
this.pageCount = (this.rowCount - 1) / this.pageSize + 1;
this.rs.close();
this.stmt.close();
}
this.sqlStr = this.sqlStr + " limit " + this.irows + ","
+ this.pageSize;
this.stmt = this.conn.createStatement();
this.rs = this.stmt.executeQuery(this.sqlStr);
} catch (Exception ex) {
ex.printStackTrace();
}
} public Vector getPage() {
Vector vData = new Vector();
try {
if (this.rs != null) { while (this.rs.next()) {
String[] sData = new String[this.columnCount];
for (int j = 0; j < this.columnCount; j++) {
sData[j] = this.rs.getString(j + 1);
}
// 对 向 量 增加
vData.addElement(sData);
}
this.rs.close();
this.stmt.close();
this.conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return vData;
} // 获得页面总数
public int getPageCount() {
return this.pageCount;
} // 获得数据表中总纪录数
public int getRowCount() {
return this.rowCount;
}
}
package data;import java.sql.*;public class LoginData{
Connection conn=null;
public LoginData(){
this.connect();
}
public Connection getConn(){
return this.conn;
}
public boolean connect(){
try{
//使用JDBC桥创建数据库连接
Class.forName("com.mysql.jdbc.Driver").newInstance();
//使用DriverManager类的getConnection()方法建立连接
//第一个参数定义用户名,第二个参数定义密码
this.conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=gb2312","root","root");
}catch(Exception ex){
ex.printStackTrace();
return false;
}
return true;
}
}