STRUTS+HIBERNATE+ORACLE分页

简介: 主要由:message.jspMessageFormMessageActionPlayer.hbm.xmlstruts-config.xmlMeisaiBean.

主要由:
message.jsp
MessageForm
MessageAction
Player.hbm.xml
struts-config.xml
MeisaiBean.java
来实现:
先看数据库结构:
CREATE TABLE player
(
  PLAYER_ID   VARCHAR(32) PRIMARY KEY,
  TEAM        VARCHAR(3),
  displayName  VARCHAR(100),
  LAST_NAME   VARCHAR(100),
  MIDDLE_NAME VARCHAR(100),
  STATUS      VARCHAR(32),
  POS         VARCHAR(1),
  PPG         INTEGER default 25
);

INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('2113999','DAL','Dirk',null,'',null,'F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30361','IND','Ron','Artest','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30273','ORL','Tracy','McGrady','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30190','LAL','Kobe','Bryant','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30328','SEA','Rashard','Lewis','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30197','NJN','Kerry','Kittles','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30341','HOU','Steve','Francis','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30146','MIN','Kevin','Garnett','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('2113993','BOS','Paul','Pierce','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30050','NJN','Jason','Kidd','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30215','ATL','Shareef','Abdur-Rahim','','I','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('1861922','ORL','Mike','Miller','','I','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30144','ATL','Glenn','Robinson','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('2019703','GSW','Gilbert','Arenas','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('2028505','UTH','Andrei','Kirilenko','','I','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30382','PHO','Shawn','Marion','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('29941','SEA','Brent','Barry','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('2114000','DAL','Michael','Finley','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30187','BOS','Antoine','Walker','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30234','SAC','Bobby','Jackson','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('29968','WAS','Jerry','Stackhouse','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('29944','SAC','Doug','Christie','','I','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30095','CHI','Donyell','Marshall','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('2028865','ATL','Ira','Newble','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30029','SEA','Gary','Payton','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30141','TOR','Voshon','Lenard','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30020','MIL','Toni','Kukoc','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30154','NOR','P.J.','Brown','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30125','MIA','Eddie','Jones','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30359','ATL','Jason','Terry','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30291','UTH','Matt','Harpring','','A','F');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('30228','MIL','Ray','Allen','','A','G');
INSERT INTO player (PLAYER_ID,TEAM,displayName,LAST_NAME,MIDDLE_NAME,STATUS,POS) VALUES ('2028503','SEA','Vladimir','Radmanovic','','A','F');
对应的player.hbm.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>


<!-- test player-->
<class name="com.ecc.hibernate.hbm.Player" table="PLAYER">
<id name="PlayerId" column="player_id" type="string" length="32">
<generator class="uuid.string"/>
</id>
<property name="Team" column="team" type="string" length="3"/>
<property name="DisplayName" column="displayname" type="string" length="100"/>
<property name="LastName" column="last_name" type="string" length="100"/>
<property name="MiddleName" column="middle_name" type="string" length="100"/>
<property name="Status" column="status" type="string" length="32"/>
<property name="Pos" column="pos" type="string" length="1"/>
<property name="Ppg" column="ppg" type="long" length="22"/>
</class>
</hibernate-mapping>
现在看MeisaiBean.java
package com.ecc.util;

import java.io.Serializable;

 

public class MeisaiBean implements Serializable{

  public MeisaiBean() {
  }

  public String label_1 = "";
  public void setLabel_1(String str) {
    this.label_1 = str;
  }

  public String getLabel_1() {
    return this.label_1;
  }

  public String label_2 = "";
  public void setLabel_2(String str) {
    this.label_2 = str;
  }

  public String getLabel_2() {
    return this.label_2;
  }

  public String label_3 = "";
  public void setLabel_3(String str) {
    this.label_3 = str;
  }

  public String getLabel_3() {
    return this.label_3;
  }

  public String label_4 = "";
  public void setLabel_4(String str) {
    this.label_4 = str;
  }

  public String getLabel_4() {
    return this.label_4;
  }

  public String label_5 = "";
  public void setLabel_5(String str) {
    this.label_5 = str;
  }

  public String getLabel_5() {
    return this.label_5;
  }

  public String label_6 = "";
  public void setLabel_6(String str) {
    this.label_6 = str;
  }

  public String getLabel_6() {
    return this.label_6;
  }

  public String label_7 = "";
  public void setLabel_7(String str) {
    this.label_7 = str;
  }

  public String getLabel_7() {
    return this.label_7;
  }

  public String label_8 = "";
  public void setLabel_8(String str) {
    this.label_8 = str;
  }

  public String getLabel_8() {
    return this.label_8;
  }

  public String label_9 = "";
  public void setLabel_9(String str) {
    this.label_9 = str;
  }

  public String getLabel_9() {
    return this.label_9;
  }

  public String label_10 = "";
  public void setLabel_10(String str) {
    this.label_10 = str;
  }

  public String getLabel_10() {
    return this.label_10;
  }

  public String label_11 = "";
  public void setLabel_11(String str) {
    this.label_11 = str;
  }

  public String getLabel_11() {
    return this.label_11;
  }

  public String label_12 = "";
  public void setLabel_12(String str) {
    this.label_12 = str;
  }

  public String getLabel_12() {
    return this.label_12;
  }

  public String label_13 = "";
  public void setLabel_13(String str) {
    this.label_13 = str;
  }

  public String getLabel_13() {
    return this.label_13;
  }

  public String label_14 = "";
  public void setLabel_14(String str) {
    this.label_14 = str;
  }

  public String getLabel_14() {
    return this.label_14;
  }

  public String label_15 = "";
  public void setLabel_15(String str) {
    this.label_15 = str;
  }

  public String getLabel_15() {
    return this.label_15;
  }

  public String label_16 = "";
  public void setLabel_16(String str) {
    this.label_16 = str;
  }

  public String getLabel_16() {
    return this.label_16;
  }

  public String label_17 = "";
  public void setLabel_17(String str) {
    this.label_17 = str;
  }

  public String getLabel_17() {
    return this.label_17;
  }

  public String label_18 = "";
  public void setLabel_18(String str) {
    this.label_18 = str;
  }

  public String getLabel_18() {
    return this.label_18;
  }

  public String label_19 = "";
  public void setLabel_19(String str) {
    this.label_19 = str;
  }

  public String getLabel_19() {
    return this.label_19;
  }

  public String label_20 = "";
  public void setLabel_20(String str) {
    this.label_20 = str;
  }

  public String getLabel_20() {
    return this.label_20;
  }

  public String label_21 = "";
  public void setLabel_21(String str) {
    this.label_21 = str;
  }

  public String getLabel_21() {
    return this.label_21;
  }
}
MessageForm.java:

//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.2/xslt/JavaClass.xsl

package com.ecc.struts.message.formbean;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import java.util.*;
/**
 * MyEclipse Struts
 * Creation date: 01-06-2005
 *
 * XDoclet definition:
 * @struts:form name="testRegistForm"
 */
public class MessageForm extends ActionForm {
    public MessageForm(){}
 private String label1="";

 /**
  * @return
  */
 public String getLabel1() {
  return label1;
 }

 /**
  * @param string
  */
 public void setLabel1(String string) {
  label1 = string;
 }
 private Vector meisai = new Vector();
 public void setMeisai(Vector meisai) {this.meisai = meisai; }
 public Vector getMeisai(){return meisai; }
 
 private int currentPage=0;
 
 /**
  * @return
  */
 public int getCurrentPage() {
  return currentPage;
 }

 /**
  * @param i
  */
 public void setCurrentPage(int i) {
  currentPage = i;
 }
 private int totalPage=0;
 
 /**
  * @return
  */
 public int getTotalPage() {
  return totalPage;
 }

 /**
  * @param i
  */
 public void setTotalPage(int i) {
  totalPage = i;
 }

}
MessageAction.java:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.2/xslt/JavaClass.xsl

package com.ecc.struts.message.action;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.SessionActionMapping;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import com.ecc.struts.message.formbean.*;
import com.ecc.util.*;
import java.security.*;
import com.ecc.util.security.password.*;
import com.ecc.exception.regist.*;
import com.ecc.hibernate.db.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import com.ecc.hibernate.*;
import com.ecc.hibernate.hbm.*;
import java.util.*;
public class MessageAction extends Action{

// --------------------------------------------------------- Instance Variables

// --------------------------------------------------------- Methods

/**
 * Method execute
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 */
private void setPage(ActionForm form,int currentPage){
Transaction tx=null;
Session session=null;
int totalCount=0;
String playerid="";
String team="";
String displayname="";
int firstIndex=0;
int itemsInPerPage=10;
try{
MessageForm messageForm = (MessageForm) form;
session = HbUnit.getSession();
tx= session.beginTransaction();
currentPage=messageForm.getCurrentPage();
if (currentPage<=0){
messageForm.setCurrentPage(1);
firstIndex=0;
}else
{
firstIndex=currentPage*itemsInPerPage-itemsInPerPage;
}
String sql="select player from Player as player";
Query query = session.createQuery
(sql);
query.setFirstResult(firstIndex);
query.setMaxResults(itemsInPerPage);
Iterator it=query.iterate();
Vector meisaiList=new Vector();
while(it.hasNext()){
Player player=(Player)it.next();
playerid=StrHandle.replaNull(player.getPlayerId());
team=StrHandle.replaNull(player.getTeam());
displayname=StrHandle.replaNull(player.getDisplayName());
MeisaiBean meisai=new MeisaiBean();

meisai.setLabel_1(playerid);
meisai.setLabel_2(team);
meisai.setLabel_3(displayname);
meisaiList.add(meisai);
}
messageForm.setMeisai(meisaiList);
tx.commit();
}
catch(Exception ex){
try{
tx.rollback();
}catch(Exception eex){
}
System.out.println(ex);
ex.printStackTrace();
}finally{
try{
HbUnit.closeSession();
}catch(Exception ex){
}
}
}
private int getRecordSize(){
Transaction tx=null;
Session session=null;
int totalCount=0;
try{
session = HbUnit.getSession();
tx= session.beginTransaction();
totalCount=( (Integer) session.iterate("select count(*) from Player").next() ).intValue();
tx.commit();
return totalCount;
}
catch(Exception ex){
try{
tx.rollback();
}catch(Exception eex){
}
System.out.println(ex);
ex.printStackTrace();
return -1;
}finally{
try{
HbUnit.closeSession();
}catch(Exception ex){
}
}
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
MessageForm messageForm = (MessageForm) form;

String target="";
int size=0;
int perItemsPage=10;
int modItems=0;
int totalPages=0;
int currentPage=0;
try{
currentPage=messageForm.getCurrentPage();
size=getRecordSize();
if(size<=0){
target="noitemfound";
}
else
{
totalPages=size/perItemsPage;
modItems=size%perItemsPage;
if(modItems>0)totalPages+=1;
System.out.println("Record Size:"+size);
System.out.println("Total page:"+totalPages);
messageForm.setTotalPage(totalPages);
setPage(form,currentPage);
System.out.println("total page:"+totalPages);
target="success";
}
}catch(Exception ex){
System.out.println(ex);
ex.printStackTrace();
}
return (mapping.findForward(target));
}
}
Message.jsp如下:
 
<%@ page language="java"%>
<%@ page contentType="text/html; charset=GB2312" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
 
<html>
<head>
<title>JSP for testRegistForm form</title>
<STYLE type=text/css>
FONT-SIZE: 9pt{
}
A {
COLOR: #000000; TEXT-DECORATION: none
}
A:hover {
TEXT-DECORATION: underline
}
.bt {
BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; FONT-SIZE: 9pt; BORDER-BOTTOM-WIDTH: 0px; WIDTH: 50px; CURSOR: hand; HEIGHT: 16px; BACKGROUND-COLOR: #808080; BORDER-RIGHT-WIDTH: 0px
}
.tx1 {
BORDER-RIGHT: black 1px ridge; BORDER-TOP: black 1px ridge; FONT-SIZE: 9pt; BORDER-LEFT: black 1px ridge; WIDTH: 60pt; COLOR: #000000; BORDER-BOTTOM: #000000 1px ridge; HEIGHT: 20px
}
</STYLE>
</head>
<Script Language="JavaScript">
function gotoNext(){
var currentPage=parseInt(MessageForm.currentPage.value);
var totalPage=parseInt(MessageForm.totalPage.value);
currentPage=currentPage+1;
if(currentPage>totalPage){
currentPage=totalPage;
}
var url="message.do?currentPage="+currentPage;
alert(url);
document.location=url;
}
function gotoPrev(){
var currentPage=parseInt(MessageForm.currentPage.value);
var totalPage=parseInt(MessageForm.totalPage.value);
if(currentPage<=0){
  currentPage=0;
}else{
currentPage=currentPage-1;
}
var url="message.do?currentPage="+currentPage;
alert(url);
document.location=url;
}
function gotoTop(){

var url="message.do?currentPage=1";
alert(url);
document.location=url;
}
function gotoLast(){
var totalPage=parseInt(MessageForm.totalPage.value);
var url="message.do?currentPage="+totalPage;
alert(url);
document.location=url;
}
</Script>
<body>
<html:form name="MessageForm" action="/message.do" type="com.ecc.struts.message.formbean.MessageForm" scope="request">
<html:text property="currentPage"/></br>
<html:text property="totalPage"/></br>
</html:form>
<table>
<tr>
  <td><input value="top" type="button" onclick="gotoTop();">
<td><input value="prev" type="button" onclick="gotoPrev();">
<td><input value="next" type="button" onclick="gotoNext();">
<td><input value="last" type="button" onclick="gotoLast();">
</tr>
</table>
<table border='1'>
<logic:present name="MessageForm" scope="request">
  <logic:iterate id="meisaiList" name="MessageForm" property="meisai" scope="request">
    <tr>
  <td>
   <bean:write name="meisaiList" property="label_1" />
  </td>
  <td>
  <bean:write name="meisaiList" property="label_2" />
  </td>
  <td>
  <bean:write name="meisaiList" property="label_3" />
  </td>
    </tr>
    </logic:iterate>
    </logic:present>
</table>
</body>
</html>

目录
相关文章
|
7月前
|
JavaScript 前端开发 Java
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
|
7月前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——OpSessionview实现
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——OpSessionview实现
|
7月前
|
Java
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Action的实现类
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Action的实现类
|
6月前
|
Oracle Java 关系型数据库
[学习笔记] 在Eclipse中使用Hibernate,并创建第一个Demo工程,数据库为Oracle XE
[学习笔记] 在Eclipse中使用Hibernate,并创建第一个Demo工程,数据库为Oracle XE
|
7月前
|
SQL Oracle 关系型数据库
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
|
2月前
|
存储 Oracle 关系型数据库
Oracle数据库的应用场景有哪些?
【10月更文挑战第15天】Oracle数据库的应用场景有哪些?
203 64
|
20天前
|
存储 Oracle 关系型数据库
数据库数据恢复—ORACLE常见故障的数据恢复方案
Oracle数据库常见故障表现: 1、ORACLE数据库无法启动或无法正常工作。 2、ORACLE ASM存储破坏。 3、ORACLE数据文件丢失。 4、ORACLE数据文件部分损坏。 5、ORACLE DUMP文件损坏。
67 11
|
1月前
|
Oracle 关系型数据库 数据库
Oracle数据恢复—Oracle数据库文件有坏快损坏的数据恢复案例
一台Oracle数据库打开报错,报错信息: “system01.dbf需要更多的恢复来保持一致性,数据库无法打开”。管理员联系我们数据恢复中心寻求帮助,并提供了Oracle_Home目录的所有文件。用户方要求恢复zxfg用户下的数据。 由于数据库没有备份,无法通过备份去恢复数据库。
|
1月前
|
存储 Oracle 关系型数据库
oracle数据恢复—Oracle数据库文件大小变为0kb的数据恢复案例
存储掉盘超过上限,lun无法识别。管理员重组存储的位图信息并导出lun,发现linux操作系统上部署的oracle数据库中有上百个数据文件的大小变为0kb。数据库的大小缩水了80%以上。 取出&并分析oracle数据库的控制文件。重组存储位图信息,重新导出控制文件中记录的数据文件,发现这些文件的大小依然为0kb。
|
26天前
|
存储 Oracle 关系型数据库
服务器数据恢复—华为S5300存储Oracle数据库恢复案例
服务器存储数据恢复环境: 华为S5300存储中有12块FC硬盘,其中11块硬盘作为数据盘组建了一组RAID5阵列,剩下的1块硬盘作为热备盘使用。基于RAID的LUN分配给linux操作系统使用,存放的数据主要是Oracle数据库。 服务器存储故障: RAID5阵列中1块硬盘出现故障离线,热备盘自动激活开始同步数据,在同步数据的过程中又一块硬盘离线,RAID5阵列瘫痪,上层LUN无法使用。