struts2 mysql 分页代码,Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql调整+分页模板

Struts2.1.6+Spring2.5.6+Hibernate3.3.2+mysql整合+分页模板

1、导入29个JAR包

JAR包名称

作用

Struts2.1.6(7个)

struts2-core-2.1.6.jar

struts2开发的核心类库

freemarker-2.3.13.jar

struts2的UI标签的模板使用freemarker编写

commons-logging-1.0.4.jar

ASF出的日志包,支持Log4J和JDK的日志记录

ognl-2.6.11.jar

对象图导航语言,通过它来读写对象属性

xwork-2.1.2.jar

xwork类库,struts2在其上进行构建

commons-fileupload-1.2.1.jar

文件上传组件,2.1.6版本后必须加入此jar包

commons-io-1.3.2.jar

以后文件上传下载需要

Hibernate3.3.2(13个)

hibernate3.jar

hibernate3开发的核心类库

antlr-2.7.6.jar

解析HQL

commons-collections-3.1.jar

集合框架

dom4j-1.6.1.jar

解析xml

javassist-3.9.0.GA.jar

jta-1.1.jar

junit-4.8.1.jar

Junit test包

ejb3-persistence.jar

@Entity

Hibernate-annotations.jar

Hibernate-commons-annotations.jar

log4j-1.2.15.jar

是log4j实现类

slf4j-api-1.5.8.jar

标准接口

slf4j-log4j12-1.5.8.jar

是slf4j转换log4j的中间接口

Spring 2.5.6(8个)

spring.jar

Spring核心文件

common-annotations.jar

IOC支持,例如@resource

aspectjrt.jar

AOP支持:aspectj运行时候需要的

aspectjweaver.jar

AOP支持:织入

cglib-nodep-2.1_3.jar

动态生成字节码

Commons-dbcp.jar

数据源

commons-po ol.jar

数据源

struts2-spring-plugin-2.1.6.jar

Struts2和Spring结合所需包

commons-logging-1.0.4.jar

Struts2加入了就不需要导入

log4j-1.2.15.jar

Hibernate加入了就不需要导入

数据库包(1个)

mysql-connector-java-3.1.10-bin.jar

MySql的驱动程序

2、导入框架的配置文件

SRC目录下的配置文件

log4j.properties

log4j的配置文件,放到SRC根目录下

hibernate.cfg.xml

Hibernate的配置文件

beans.xml

Spring的配置文件

struts.xml

Struts的配置文件

WEB-INF下配置文件

web.xml

Struts2和Spring的结合配置

PS:如果需要使用JSTL标签需要导入2个包

jstl.jar

standard.jar

3、建立对应的package和对应的接口与类框架

1758355168.jpg

hibernate.cfg.xml

/p>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/ssh

root

lee

org.hibernate.dialect.MySQLDialect

thread

org.hibernate.cache.NoCacheProvider

true

update

beans:xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

ssh.model

struts.xml

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/index.jsp

/success.jsp

web.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:beans.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

GBK

encodingFilter

/*

openSessionInView

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

openSessionInView

/*

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

4、建立实体类,并加入Hibernate的注解

1)在实体类上加相应注解@Entity @Id等

2)在字段属性的get方法上加--@Column(name = "表字段名")

User.java

5、定义工具类

1)定义查询返回结果

QueryResult.java

package ssh.utils;

import java.util.List;

/*

* 定义查询返回的结果,泛型定义在类上

*/

public class QueryResult {

private List resultlist;//记录查询的结果

private long totalrecord;//记录查询得到的总条数

public List getResultlist() {

return resultlist;

}

public void setResultlist(List resultlist) {

this.resultlist = resultlist;

}

public long getTotalrecord() {

return totalrecord;

}

public void setTotalrecord(long totalrecord) {

this.totalrecord = totalrecord;

}

}

2)定义分页工具类

PageView.java

package ssh.utils;

import java.util.List;

/**

* 在Action里的调用方法

//这里必须要构造新对象,不然刚打开没有currentPage参数传递过来,如果不新建也行,第一次打开必须传递currentPage参数过来

private PageViewpageView=new PageView();

public PageView getPageView() {

return pageView;

}

public void setPageView(PageView pageView) {

this.pageView = pageView;

}

int maxresult=1;

int firstindex=(pageView.getCurrentPage()-1)*maxresult;

QueryResult Service.getScrollData(firstindex,maxresult, null, null, null);

pageView.setQueryResult(maxresult,qr);

request.put("pageView", pageView);

*/

public class PageView {

/** 分页数据 **/

private List records;

/** 页码开始索引 ,例如显示第1 2 3 4 5 6 7 8 9 ,开始索引为1 **/

private long startIndex;

/** 页码结束索引 ,例如显示第1 2 3 4 5 6 7 8 9 ,结束索引为9 **/

private long endIndex;

/** 总页数 ,没有0页,所以设置默认值为1 **/

private long totalPage = 1;

/** 每页显示记录数 **/

private int maxResult = 10;

/** 当前页 **/

private int currentPage = 1;

/** 总记录数 **/

private long totalRecord;

/** 工具条上显示的页码数量 **/

private int pageBarSize = 8;

// 这只方法触发记录查询结果和总条数

public void setQueryResult(int maxResult,QueryResult qr) {

this.maxResult = maxResult;

this.records = qr.getResultlist();

this.totalRecord = qr.getTotalrecord();

this.totalPage = this.totalRecord % this.maxResult == 0 ? this.totalRecord/ this.maxResult : this.totalRecord / this.maxResult + 1;

/*****************************************************/

this.startIndex = currentPage - (pageBarSize % 2 == 0 ? pageBarSize / 2 - 1 : pageBarSize / 2);

this.endIndex = currentPage + pageBarSize / 2;

if (startIndex < 1) {

startIndex = 1;

if (totalPage >= pageBarSize)

endIndex = pageBarSize;

else

endIndex = totalPage;

}

if (endIndex > totalPage) {

endIndex = totalPage;

if ((endIndex - pageBarSize) > 0)

startIndex = endIndex - pageBarSize +1;//最后一页显示多小条页

else

startIndex = 1;

}

}

public List getRecords() {

return records;

}

public void setRecords(List records) {

this.records = records;

}

public long getStartIndex() {

return startIndex;

}

public void setStartIndex(long startIndex) {

this.startIndex = startIndex;

}

public long getEndIndex() {

return endIndex;

}

public void setEndIndex(long endIndex) {

this.endIndex = endIndex;

}

public long getTotalPage() {

return totalPage;

}

public void setTotalPage(long totalPage) {

this.totalPage = totalPage;

}

public int getMaxResult() {

return maxResult;

}

public void setMaxResult(int maxResult) {

this.maxResult = maxResult;

}

public int getCurrentPage() {

return currentPage;

}

public void setCurrentPage(int currentPage) {

this.currentPage = currentPage<1?1:currentPage;//如果当前页为0,则显示第一页

}

public long getTotalRecord() {

return totalRecord;

}

public void setTotalRecord(long totalRecord) {

this.totalRecord = totalRecord;

}

public int getPageBarSize() {

return pageBarSize;

}

public void setPageBarSize(int pageBarSize) {

this.pageBarSize = pageBarSize;

}

}

1 楼

lenka_xiu

2011-10-30

icon_smile.gif 谢谢你的分享,可是我运行的时候就是有问题哦。数据库是有数据的,可是查不出来数据啊。能加我qq教教我不? qq:1009610689

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值