寒,忘了留EMAIL了,
[email protected]
[email protected]
解决方案 »
- struts2中有关值栈问题
- 有关get提交方式的初级问题
- 有两种对数据库的操作,不知道用sql还是存储过程好?
- web开发如何将数据导出到excel中
- 请问如何获取数据表的字段名称?
- tomcat疯了吗?
- 请教servlet配制问题。(解决了我才能吃饭)
- 感谢sparkwu(吳府明)多次用短消息帮助我用JDBC连接数据库!快进来回复这个贴子
- Resin下,WEB-INF/lib目录怎么用法?有没有相关的例子?
- JSP运行错误!!
- 急:关于<html:select property="name" />的问题,在线等
- Spring + Struts 配置文件加载不上问题!请大侠帮忙看看!!!!!!
郁闷,要么就是象我一样,直接在页面内套上去的。
- -#
<%!int rowsPerPage=15; %>
<%@ include file="connect.jsp" %>
<%
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">
-->
</head>
<body>
<table border="1" bgcolor="#00ff00" align="center" name="table">
<%
int currentPage=1;
try
{
stmt=con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql="select * from course order by course_no desc";
rs=stmt.executeQuery(sql);
if(rs!=null)
{
rs.last();
int maxRows=rs.getRow();//总共行数
//总页数
int pageCount=(maxRows/rowsPerPage==0)?maxRows/rowsPerPage:(maxRows/rowsPerPage+1);
String strCurPage=request.getParameter("curPage");
if(strCurPage==null)
currentPage=1;
else
{
currentPage=Integer.parseInt(strCurPage);
}
if(currentPage>pageCount)
currentPage=pageCount;
if(currentPage<1)
currentPage=1;
int position=(currentPage-1)*rowsPerPage+1;//光标移动到当前页的第一行
rs.absolute(position);
%>
<tr>
<td>课程编号</td>
<td>课程名</td>
<td>学分</td>
<td>教师编号</td>
<td>删除</td>
</tr>
<%
for(int i=0;i<rowsPerPage;i++)
{
String name=rs.getString("course_no");
if(rs.getRow()>maxRows) break;
%>
<tr>
<td><%=rs.getString(1) %></td>
<td><%=rs.getString(2) %></td>
<td><%=rs.getString(3) %></td>
<td><%=rs.getString(4) %></td>
<td><a href="delete.jsp?name=<%=name %>">删除</a></td>
</tr>
<%
rs.next();
}
}
}catch(SQLException e){
e.printStackTrace();
e.getMessage();
}
%>
</table>
<div align="center">
<form action="changePage.jsp" method="post">
<a href="changePage.jsp?curPage=<%=currentPage-1 %>">上一页</a>
<a href="changePage.jsp?curPage=<%=currentPage+1%>">下一页</a>
第<input type="text" name="curPage" size="5">页
<input type="submit" name="submit" value="GO">
<input type="submit" name="excel" value="save">
</form></div>
</body>
</html>
也不是很理想,但还是能实现功能的connect.jsp是和数据库建立连接的
<%@ page contentType="text/html; charset=GBK" %>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="bean.xxx.bean名"%>
<jsp:useBean id="db" scope="page" class="database.连接数据库的类"/>
<jsp:useBean id="bean自己起的名" scope="page" class="bean.xxx.bean名"/>......
<!--分页================================================================-->
<%
String p = request.getParameter("page");
int tempPage = 1;
if (p != null)
tempPage = Integer.parseInt(p);
//从会话里得到的 ArrayList
ArrayList list = (ArrayList) session.getAttribute("放入会话的 ArrayList 名");
//如果会话里没有值 也就是第一次到本页面进行的处理 可保证不出现空指针异常
if(list == null){
String sql = "select * from 表名";
list = db.自己写的得到ArrayList的方法(sql);
}
int recordsCount = list.size(); //总记录数
// System.out.println(recordsCount);
int pageSize = 10; //每页记录数 可以改成任意数值
int pagesCount = (recordsCount + pageSize - 1) / pageSize; //总页数
// System.out.println(pagesCount);
if (tempPage < 0)
tempPage = 1;
if (tempPage > pagesCount)
tempPage = pagesCount;
int curPage = tempPage; //当前页结束页码
// System.out.println(curPage);
int start = (curPage - 1)*pageSize; //当前页起始页码
// System.out.println(start);
int end = curPage*pageSize + 1; //当前页结束页码
// System.out.println(end);
int i = 0;
Iterator it = list.iterator(); //叠代
while (it.hasNext()) { //it.hasNext()判断有无下一条记录
bean自己起的名 = (bean名) it.next(); //it.next()指针指向下一条
i++;
if (i > start && i < end) {
%>
<!--=====================================================================-->
........
<tr>
<td><%=bean自己起的名.getXXXXX()%></td>
</tr>
.......
<!--分页==========================================-->
<%
}
}
%>
<tr bgcolor="#ffffff">
<td height="27" colspan="3"><div align="center">
<%if (curPage != 1) {%>
<a href="本页面名.jsp?page=1">第一页</a> <a href="本页面名.jsp?page=<%=curPage-1%>">上一页</a>
<%
}
if (curPage != pagesCount) {
%>
<a href="本页面名.jsp?page=<%=curPage+1%>">下一页</a> <a href="本页面名.jsp?page=<%=pagesCount%>">最后一页</a>
<%}%>
</div></td>
</tr>
<!--=========================================================-->
<%
int startRow = 0,totalRow=0;
try {startRow=Integer.parseInt(request.getParameter("statrow"));}catch (Exception ex) {}
try {totalRow=Integer.parseInt((String)request.getAttribute("totalRow"));}catch (Exception ex) {}
PageDivider pageDiv = new PageDivider(selfUrl,"a=a",totalRow,startRow,15);%>
<table border="0">
<%=pageDiv.getFontTypeOut()%>
</table>
pageDiv类
package com.sc.util;/**
* <p>Title:用于分页时简化在jsp页面出现太多java代码 </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company:drgsqd </p>
* @author unascribed
* @version 1.0
*/public class PageDivider implements java.io.Serializable {
private String _selfPath=null;
private String _paraStr=null;
private int _totalRow=0;
private int _startRow=0;
private int _showRow=0;/////////////////自身参数
private int _priRow = 0;
private int _nextRow = 0;
private int _endRow = 0;
private int _totalPage = 0;
private int _nowPage = 0;
public PageDivider(String selfPath,String paraStr,int totalRow,int startRow,int showRow) {
this._selfPath = selfPath;
this._paraStr = paraStr;
this._totalRow = totalRow;
this._startRow = startRow;
this._showRow = showRow;
initValues();
}
private void initValues(){
if(this._paraStr==null)this._paraStr="";
if(this._selfPath==null)this._selfPath="";
if(this._showRow>this._totalRow)this._startRow=0;
if(this._startRow>this._totalRow)this._startRow=this._totalRow; this._priRow = this._startRow - this._showRow;
this._nextRow = this._startRow + this._showRow;
if(this._totalRow%this._showRow!=0)
this._totalPage = this._totalRow/this._showRow+1;
else this._totalPage = this._totalRow/this._showRow; this._endRow = this._showRow*(this._totalPage-1);
this._nowPage = this._startRow/this._showRow+1;
}
public String getFontTypeOut(){
StringBuffer outStrBuf=new StringBuffer();
outStrBuf.append("<tr><td> "); outStrBuf.append(" 第<font color=red>"+this._nowPage+"</font>页 / 共<font color=red>"+this._totalPage+"</font>页 共<font color=red>"+this._totalRow+"</font>条</td><td>");
if(this._startRow>0){
outStrBuf.append("<a href=\""+this._selfPath+"?"+this._paraStr+"&statrow=0\">首页</a> \n");
outStrBuf.append("<a href=\""+this._selfPath+"?"+this._paraStr+"&statrow="+this._priRow+"\">上一页</a>\n");
}else{
outStrBuf.append("<font color=#cccccc>首页 上一页</font>\n");
}
outStrBuf.append("</td><td> ");
if(this._nowPage<this._totalPage){
outStrBuf.append("<a href=\""+this._selfPath+"?"+this._paraStr+"&statrow="+this._nextRow+"\">下一页</a> \n");
outStrBuf.append("<a href=\""+this._selfPath+"?"+this._paraStr+"&statrow="+this._endRow+"\">尾页</a>\n");
}else{
outStrBuf.append("<font color=#cccccc>下一页 尾页</font>\n");
}
if(this._totalPage>1){
outStrBuf.append("</td><td>");
outStrBuf.append("<script> function pageGoTo(objUrl,objcont,objValue){window.location= objUrl+(objValue*objcont);}</script>");
outStrBuf.append("转到第<SELECT name=\"page\" class=\"select\" onchange=\"pageGoTo('"+this._selfPath+"?"+this._paraStr+"&statrow=',"+this._showRow+",this.value);\">");
for(int i=1;i<=this._totalPage;i++){
outStrBuf.append("<OPTION "+(this._nowPage==i?"selected":"")+" value="+(i-1)+">"+i+"</OPTION>");
}
outStrBuf.append("</SELECT>页");
}
outStrBuf.append(" </td></tr>\n");
return outStrBuf.toString();
}
public String getInputTypeOut(){
String outStr="";
outStr+="";
return outStr;
}
}
查询语句加上参数startRow,表示页数
JAVA实现关系数据库的翻页
我们写了一个分页的标签就很方便,使用也很简单:
如在想要分页的地方加入代码:
如
<TD><util:page name="*ListForm" property="list" withScript="y" /></TD>
就可以了,name是对应的页面的actionform,property是要统计的记录数组,withScript是否显示分页标志.通用,任何项struts目都可以使用. 功能显示了当前页,总页数,每页的记录数,总记录数,直接跳到第一页或最后一页,前一页,后一页,当前页附近的两页,下拉选择所有页中的任意一页等.功能还算是强大了.显示的效果:
"第1页,共4页 每页16条记录,共69条记录 |<< << 1,2,3,4 >> >>| 到 i(下拉选择框) 页 "
所以你也可以写一个标签来实现,你现在写的不通用的原因主要是你考虑的还不够全面,所以建议你写之前应该想好到底要传什么参数,怎么传,写就没有问题了.
由于我这标签不能单独使用,涉及到很多的类文件,主要是整个地层的数据库访问机制,当然也包括一些设置分页参数的类等.所以就不在此帖出给你了.只是告诉你,要想做通用的东西,必须要想好,考虑周全才行
排序,查找,分页,导出pdf什么的都有
这里有struts的分页例子
我也才写了个
当然是说做成通用的……
rs.next();//结果集滚动
}
for(int j=0;j<perNum;j++){
rs.next();
把需要的结果拿出来。
}
* Created by cyril.gu on Mar 24, 2007 12:00:57 PM
* Email:[email protected]
**/
import java.util.List;public class PageList {
private int currentPage = 1; //当前页码
private int countPage = 0; //总页数
private int pageRows = 0; //每页显示记录数
private int countRows = 0; //总记录数
//private int currentSize; //当前页记录数
private List list; //结果集
public String toString() {
StringBuffer s = new StringBuffer();
s.append("<input type=\"hidden\" id=\"pageList.currentPage\" name=\"pageList.currentPage\"/>");
s.append("第"+currentPage+"页/共"+countPage+"页");
s.append(" 总"+countRows+"条记录");
/**首页**/
if(this.countPage>1 && this.currentPage>1)
s.append(" <a href=\"javascript:document.getElementById('pageList.currentPage').value='1';doPagination();\">首页</a>");
else
s.append(" 首页");
/**上一页**/
if(this.currentPage>1)
s.append(" <a href=\"javascript:document.getElementById('pageList.currentPage').value='"+(this.currentPage-1)+"';doPagination();\">上一页</a>");
else
s.append(" 上一页");
/**下一页**/
if(this.countPage>1 && this.currentPage<this.countPage)
s.append(" <a href=\"javascript:document.getElementById('pageList.currentPage').value='"+(this.currentPage+1)+"';doPagination();\">下一页</a>");
else
s.append(" 下一页");
/**尾页**/
if(this.countPage>1 && this.currentPage<this.countPage)
s.append(" <a href=\"javascript:document.getElementById('pageList.currentPage').value='"+this.countPage+"';doPagination();\">尾页</a>");
else
s.append(" 尾页");
/**转到**/
s.append(" 转至:<input type=\"text\" id=\"pageList.toPage\" name=\"pageList.toPage\" value=\""+this.currentPage+"\" style=\"width:25px;\"/>");
s.append("<input type=\"button\" value=\"GO\" onclick=\"document.getElementById('pageList.currentPage').value=document.getElementById('pageList.toPage').value;doPagination();\"/ style=\"width:25px;\">");
s.append(" 每页显示:<input type=\"text\" id=\"pageList.pageRows\" name=\"pageList.pageRows\" value=\""+this.pageRows+"\" style=\"width:25px;\"/>");
s.append("<input type=\"button\" value=\"OK\" onclick=\"doPagination();\"/ style=\"width:25px;\">");
return s.toString();
} public int getCountPage() {
return countPage;
}
public int getCountRows() {
return countRows;
}
/**
* 判断要跳至的页不能大于总页数
* @return
*/
public int getCurrentPage() {
return currentPage;
}
/**
* 当每页设置数为0时,自动调整为每页显示20
* @return
*/
public int getPageRows() {
if(pageRows==0)
pageRows = 20;
return pageRows;
}
public void setCountPage(int countPage) {
this.countPage = countPage;
}
public void setCountRows(int countRows) {
this.countRows = countRows;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageRows(int pageRows) {
this.pageRows = pageRows;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
另外你现在作jsp程序用的是什么框架
mvc吗
大家5.1快乐啊 :)
---------------------------
To lijuan33828(哈哈)就是想找到一个通用的,然后再以后能够直接修改部分代码就能拿到另外一个系统中用最好,刚学JSP没多久,呵呵。如果你有好的话,Email下,谢谢勒
不过如果是初级的话,可以参考
public interface I_Page {
// 数据
public List getCollection();
// 全部匹配的数据数
public int getTotalSize();
// 当前第几页
public int getCurrentPage();
// 每页多少数据
public int getPageSize();
// 其他方法
……
}public interface I_PageProxy extends I_Page {
//
public void setPage(I_Page page);
// 取得页面HTML代码
public String getPageView();
}然后用一个抽象类实现I_PageProxy除getPageView()以外的方法,还可以写一些辅助方法
public abstract class AbstractPageProxy implements I_PageProxy {
private I_Page page;
// 数据
public List getCollection() {
return this.page.getCollection();
}
// 全部匹配的数据数
public int getTotalSize() {
return this.page.getTotalSize();
}
// 当前第几页
public int getCurrentPage() {
return this.page.getCurrentPage();
}
// 每页多少数据
public int getPageSize() {
return this.page.getPageSize();
}
// 其他方法
……
//
pulic void setPage(I_Page page) {
this.page = page;
}
}最后用一个具体类继承AbstractPageProxy
public class NumberPageProxy extends AbstractPageProxy {
public String getPageView() {
// 可以有多种实现方式,这里只做示范
return "共"+ this.getTotalSize() + "条记录,当前第" + this.getCurrentPage() + "页";
}
}我用的webwork+spring框架,Dao层从数据库中取出的I_Page对象只包含数据,通过本地或者WEBSERVICE传递到Action,Action通过setPageProxy方法从spring容器中得到一个I_PageProxy实例(只要修改ApplicationContext.xml就可以改变所有页面分页显示的逻辑),将I_Page对象用I_PageProxy进行代理,页面再对I_PageProxy进行调用。
<ww:property value="userList.pageView" escape="false" />(好像是这样的)好处是数据和前台分页逻辑完全分开,坏处是在getPageView()方法中要写html和javascript代码,也就是javaBean和页面耦合。也想过用自定义标签做,不过现在用的挺顺手,就算了。
然后再把两个链表放在一个总的链表里.... 放到session里传给jsp页
2.根据每页要显示的记录数acount及要定位的页page将记录集指针定位到相应位置
page*acount+1
3.刷新页面
package com.nyhr.struts.page;/**
* 分页实体类,保存当前分页状态变量
* @author Yeno.hhr
*/
public class Page { /** imply if the page has previous page */
private boolean hasPrePage;
/** imply if the page has next page */
private boolean hasNextPage;
/** the number of every page */
private int everyPage;
/** the total page number */
private int totalPage;
/** the total record number */
private int totalRecords;
/** the number of current page */
private int currentPage;
/** the begin index of the records by the current query */
private int beginIndex;
/** The default constructor */
public Page(){
}
/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage, int totalRecords,
int currentPage, int beginIndex) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.totalRecords = totalRecords;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
} /**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex() {
return beginIndex;
}
/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return
* Returns the everyPage.
*/
public int getEveryPage() {
return everyPage;
}
/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage() {
return hasPrePage;
}
/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
/**
* @return Returns the totalPage.
*
*/
public int getTotalPage() {
return totalPage;
}
/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
/**
* @param totalRecords
* The totalRecords to set.
*/
public void settotalRecords(int totalRecords)
{
this.totalRecords = totalRecords;
}
/**
* @return Returns the totalRecords.
*
*/
public int getTotalRecords()
{
return this.totalRecords;
}
}/**********************************PageUtil类*********************************************/
package com.nyhr.struts.page;/**
* 分页工具类,初始化Page对象
* @author Yeno.hhr
*/
public class PageUtil {
/**
* Use the origin page to create a new page
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords){
return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
}
/**
* the basic page utils not including exception handler
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage, int totalRecords){
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage,
everyPage, totalPage, totalRecords,
currentPage, beginIndex);
}
private static int getEveryPage(int everyPage){
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage){
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage){
return (currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords){
int totalPage = 0;
if(totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1 ;
return totalPage;
}
private static boolean hasPrePage(int currentPage){
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage){
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}