jsp里怎么自动分页
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>获取文本内容</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
function getValue(obj) {
obj.select(); //该对象选取
var a=document.selection.createRange().text; //返回选取项的文本内容
document.getElementById("path").value=a;
}
</script>
</head>
<body bgcolor="#d0d0d0">
<form action="servlet/Test" method="post" >
<table>
<tr>
<td> url: </td>
<td><input type="text" id="url" name="url"/></td>
<td></td>
</tr>
<tr>
<td>title:</td>
<td><input type="text" id="prefix" name="prefix"/></td>
<td><input type="text" id="suffix" name="suffix"></td>
</tr>
<tr>
<td>content: </td>
<td> <input type="text" id="prefix2" name="prefix2"/></td>
<td> <input type="text" id="suffix2" name="suffix2"></td>
</tr>
<tr>
<td>date:</td>
<td><input type="text" id="prefix3" name="prefix3"/></td>
<td><input type="text" id="suffix3" name="suffix3"/></td>
</tr>
<tr>
<td> <input type="submit"/></td>
</tr>
<tr>
<td>Sa:</td>
<td><input type="text" id="prefix4" name="prefix4"/></td>
<td><input type="text" id="suffix4" name="suffix4"/></td>
</tr>
<td><input type="radio" name="radio" onClick="javascript:alert('1');"/></td>
<td><input type="radio" name="radio" onClick=""/></td>
</table>
</form>
<%
if(request.getAttribute("title")!=null ){
out.println(request.getAttribute("title"));
}
if(request.getAttribute("content")!=null){
out.println(request.getAttribute("content"));
}
if(request.getAttribute("date")!=null){
out.println(request.getAttribute("date"));
}
if(request.getAttribute("Sa")!=null){
out.println(request.getAttribute("Sa"));
}
%>
</body>
</html>
这个我写的JSP 我想让 “Sa”打印出来后的结果 可以自动分页 要怎么写?求指教 --------------------编程问答-------------------- 只能跟你说下逻辑思路,自己写代码
分页就是处理查询出来的数据,前台jsp页面查询的时候传入页数和页面显示的数据数,后台查询的时候根据传入的页数和页面显示数去查询数据就对了撒。 比如传进来的是第一页,每页20条数据,你只要查询的sql查询前20条数据就是了。依此类推,第二页就查询第21条到第40条数据。 --------------------编程问答-------------------- 需要传2个参数,第一个是当前页,和每一页要显示多少条数据。。。 --------------------编程问答-------------------- 还真有点难 兄弟自己领悟哦 --------------------编程问答-------------------- 麻烦啊 呵呵 给你粘些代码吧。。。
--------------------编程问答-------------------- 分页其实也没什么的,懂写SQL语句就行了!不懂的进我博客看看吧! --------------------编程问答-------------------- 给你参考个分页TAG,在页面上引入下列CSS,
JSP页面
int cp=1; //当前页码
int num=5; //每页显示记录数
String currpage=request.getParameter("p");
if(currpage!=null&&currpage.length()>0){
cp=Integer.parseInt(currpage);
}else{
cp=1;
}
int index=1+(cp-1)*num; //起始序号
Page u=new Page(cp,num);
u.setQs(request.getQueryString());
ResultSet rs=u.Research();//此处返回查询条数的结果
//使用
<tr><td align="right" ><%=u.pageStr("?") %></td></tr>
//下面是java类page
public class Page {
private int total; //总记录条数
private int page=1;
private int num=20;
private String Qs;
fcmsProperties fcms=new fcmsProperties();
private String imgPath="/"+fcms.getPath();
public Page(){}
public Page(int p,int n){
page=p;
num=n;
}
private String outputPage(String jspPage){
int totalPage=(int)total/num;
int mod=total%num;
if(mod>0) totalPage++;
String pageStr="总页数:"+totalPage;
pageStr+=" 总记录数:"+total;
pageStr+=" 每页显示:"+num;
pageStr+=" 当前页:"+page;
if(page>1){
pageStr+=" <a href=\""+jspPage+"p=1\">首页</a>";
pageStr+=" <a href=\""+jspPage+"p="+(page-1)+"\">上一页</a>";
}else{
pageStr+=" 首页";
pageStr+=" 上一页";
}
if(totalPage>page){
pageStr+=" <a href=\""+jspPage+"p="+(page+1)+"\">下一页</a>";
pageStr+=" <a href=\""+jspPage+"p="+totalPage+"\">尾页</a>";
}else{
pageStr+=" 下一页";
pageStr+=" 尾页";
}
return pageStr;
}
<style>
.main{
border: 1px solid #B1C8D7;
float: left;
width: 400px;
}
.tab-hd {
background-position: right -29px;
height: 26px;
line-height: 25px;
overflow: hidden;
}
.tab-hd .tab-u {
border-right: 1px solid #B1C8D7;
border-top: 1px solid #B1C8D7;
}
.tab-u {
font-size: 14px;
width: 96px;
}
.tab-u .current {
background: none repeat scroll 0 0 #FFFFFF;
width: 97px;
font-weight: bold;
}
.tab-u {
cursor: pointer;
float: left;
font-family: Verdana,宋体,san-serif;
text-align: center;
}
.bd {
height: 598px;
padding: 12px 8px 0 10px;
}
</style>
可以实现腾讯搜搜的分页效果。
package com.mvc.demo.commons;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class PaginationTag extends SimpleTagSupport {
private String currentPage = "1";
private String totalPage = "0";
private String numberPerPage = "10";
public String getCurrentPage() {
return currentPage;
}
public void setCurrentPage(String currentPage) {
this.currentPage = currentPage;
}
public String getTotalPage() {
return totalPage;
}
public void setTotalPage(String totalPage) {
this.totalPage = totalPage;
}
public String getNumberPerPage() {
return numberPerPage;
}
public void setNumberPerPage(String numberPerPage) {
this.numberPerPage = numberPerPage;
}
public PaginationTag() {
super();
}
@Override
public void doTag() throws JspException, IOException {
StringBuffer sb = new StringBuffer("");
int total = Integer.parseInt(this.getTotalPage());
if (total != 0) {
int current = Integer.parseInt(this.getCurrentPage());
int number = Integer.parseInt(this.getNumberPerPage());
int startRow = ((current - 1) / number) * number;
startRow = startRow == 0 ? 1 : startRow;
int endRow = startRow + number;
sb.append("<div id='pager'><div class='pg'>");
String params = "'" + total + "','" + number + "','" + "{current}"
+ "'";
if (current != 1)
sb.append("<a class='pre' href='javascript:void(0)' onclick=\"pre("
+ params.replace("{current}", String.valueOf(current))
+ ")\">上一页<</a>");
for (int i = startRow; i < endRow; i++) {
if (current == i)
sb.append("<span class='current'>" + i + "</span>");
else {
sb.append("<a class='flag_pg' href='javascript:void(0)' href='javascript:void(0)' onclick=\"current("
+ params.replace("{current}", String.valueOf(i))
+ ")\">" + i + "</a>");
}
}
if (current != total)
sb.append("<a class='next' href='javascript:void(0)' onclick=\"next("
+ params.replace("{current}", String.valueOf(current))
+ ")\">下一页></a>");
sb.append("</div></div>");
}
this.getJspContext().getOut().write(sb.toString());
}
}
--------------------编程问答-------------------- 自己写一个Page工具类 --------------------编程问答--------------------
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%
int currentPage=1 ;
int pageSize=1 ;
int lineSize=10 ;
int allRecords=1 ;
int[] lsData={5,10,15,20,25,30,35,40} ;
String keyWord=request.getParameter("kw") ;
String url=request.getParameter("url") ;
%>
<%
try{
currentPage=Integer.parseInt(request.getParameter("cp"));
}catch(Exception e){
}
try{
lineSize=Integer.parseInt(request.getParameter("ls"));
}catch(Exception e){
}
try{
allRecords=Integer.parseInt(request.getParameter("allRecords")) ;
}catch(Exception e){
}
if(keyWord==null){
keyWord="";
}
if(allRecords==0){
allRecords=1;
}
pageSize=(allRecords + lineSize -1)/lineSize ;
%>
<script language="javascript">
function go(num){
document.getElementById("cp").value=num ;
document.spform.submit();
}
</script>
<form name="spform" action="<%=url%>">
按关键字查询:<input type="text" name="kw" value="<%=keyWord%>"><input type="submit" value="查询"><br>
<input type="button" value="首页" onClick="go(1)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="上一页" onClick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="下一页" onClick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>>
<input type="button" value="尾页" onClick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>>
<input type="hidden" name="deptno" value="<%=request.getParameter("deptno")%>">
<input type="hidden" name="status" value="<%=request.getParameter("status")%>">
跳转到
<select name="selcp" onchange="go(this.value)">
<%
for(int x=1;x<=pageSize;x++){
%>
<option value="<%=x%>" <%=x==currentPage?"SELECTED":""%>><%=x%></option>
<%
}
%>
</select>
页
每页显示
<select name="ls" onchange="go(1)">
<%
for(int x=0;x<lsData.length;x++){
%>
<option value="<%=lsData[x]%>" <%=lsData[x]==lineSize?"SELECTED":""%>><%=lsData[x]%></option>
<%
}
%>
</select>
行
<input type="hidden" name="cp" value="1">
</form>
这是我的JS分页组件,送给你。 --------------------编程问答-------------------- 都要传参数的,查询的时候就要分页,然后在页面上有当前页的页码,数据条数等参数。 --------------------编程问答-------------------- --------------------编程问答-------------------- 使用jquery就不用这么麻烦了,想怎么分就怎么分 --------------------编程问答-------------------- 建议LZ看下displaytag,分页超简单..下个jar包引入jsp页面.
然后<display:table> 里面+个pagesize="X"属性就能把读取出来的数据分页了(X为每页显示数量) --------------------编程问答--------------------
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<script>
var _page = ${page};
function setPage( pageNumber ) {
_page = pageNumber;
list();
}
function getPage() {
return "page=" + _page;
}
</script>
<table class="bodybar" cellspacing="0" cellpadding="0" height="11">
<tr>
<td>
<table width=100% cellspacing="0" cellpadding="0" border="0">
<s:form name="listAction" theme="易做图">
<tr align=left>
<td>
</td>
<td align=right>
第[
<font color=red><b>${page}/${totalpage}</b> </font> ]页 每页50条记录 共
<font color=red><b>${findresultcount}</b> </font> 条记录
</td>
<td width=26 align=center title="第一页" onclick="setPage(1)" class="btn" onmouseover="_buttonOver(this)" onmouseout="_buttonOut(this)">
<img src="<s:url value="/images/start.gif" />">
</td>
<td width=26 align=center title="上一页" onclick="setPage(${page-1})" class="btn" onmouseover="_buttonOver(this)"
onmouseout="_buttonOut(this)">
<img src="<s:url value="/images/previous.gif" />">
</td>
<td width=26 align=center title="下一页" onclick="setPage(${page+1})" class="btn" onmouseover="_buttonOver(this)"
onmouseout="_buttonOut(this)">
<img src="<s:url value="/images/next.gif" />">
</td>
<td width=26 align=center title="最后一页" onclick="setPage(${totalpage})" class="btn" onmouseover="_buttonOver(this)"
onmouseout="_buttonOut(this)">
<img src="<s:url value="/images/end.gif" />">
</td>
<td width=140 align=center>
跳到第[
<input type="text" name="page" value="${page}" size="2" style="font-size: 9pt; border: 0px; text-align: center;">
]页
</td>
<td width=24 align=center title="跳转到请求页数" onclick="setPage(listAction.page.value)" class="btn" onmouseover="_buttonOver(this)"
onmouseout="_buttonOut(this)">
<img src="<s:url value="/images/go.gif" />">
</td>
</tr>
</s:form>
</table>
</td>
</tr>
</table>
<script>
setTableListWidth();
</script>
补充:Java , Web 开发