STRUTS+HIBERNATE+ORACLE分页

主要由:
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>

你可能感兴趣的:(Hibernate)