每日一句: Knowledge is a treasure, but practice the key to it. 知识是一座宝库,实践是打开宝库的钥匙。

Struts2.1.8 + Spring3.0+ Hibernate3.2整合笔记

1. 需要的jar包列表

jar包名称

所在位置

说明

antlr-2.7.6.jar

hibernate/lib/required

解析HQL

aspectjrt

spring/lib/aspectj

AOP

aspectjweaver

..

AOP

cglib-nodep-2.1_3.jar

spring/lib/cglib

代理,二进制增强

common-annotations.jar

spring/lib/j2ee

@Resource

commons-collections-3.1.jar

hibernate/lib/required

集合框架

commons-fileupload-1.2.1.jar

struts/lib

struts

commons-io-1.3.2

struts/lib

struts

commons-logging-1.1.1

单独下载,删除1.0.4(struts/lib)

struts

spring

dom4j-1.6.1.jar

hibernate/required

解析xml

ejb3-persistence

hibernate-annotation/lib

@Entity

freemarker-2.3.13

struts/lib

struts

hibernate3.jar

hibernate

 

hibernate-annotations

hibernate-annotation/

 

hibernate-common-annotations

hibernate-annotation/lib

 

javassist-3.9.0.GA.jar

hiberante/lib/required

hibernate

jta-1.1.jar

..

hibernate transaction

junit4.5

 

 

mysql-

 

 

ognl-2.6.11.jar

struts/lib

 

slf4j-api-1.5.8.jar

hibernate/lib/required

hibernate-log

slf4j-nop-1.5.8.jar

hibernate/lib/required

 

spring.jar

spring/dist

 

struts2-core-2.1.8.jar

struts/lib

 

xwork-2.1.2.jar

struts/lib

struts2

commons-dbcp

spring/lib/jarkata-commons

 

commons-pool.jar

..

 

struts2-spring-plugin-2.1.8.jar

struts/lib

 

2. BestPractice

a) 将这些所有的jar包保存到一个位置,使用的时候直接copy

3. 步骤

a) 加入jar

b) 首先整合Spring + Hibernate

i. 建立对应的package

1. dao / dao.impl / model / service / service.impl/ test

ii. 建立对应的接口与类框架

1. S2SH_01

iii. 建立spring的配置文件(建议自己保留一份经常使用的配置文件,以后用到的时候直接copy改)

iv. 建立数据库

v. 加入Hibernate注解

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

2. beans配置文件配置对应的实体类,使之受管

vi. dao service的实现

vii. 加入Spring注解

1. 在对应ServiceDAO实现中加入@Component,让spring对其初始化

2. Service上加入@Transactional或者使用xml方式(此处建议后者,因为更简单)

3. DAO中注入sessionFactory

4. Service中注入DAO

5. DAOService的实现

viii. 写测试

c) 整合Struts2

i. 结合点:Struts2ActionSpring产生

ii. 步骤:

1. 修改web.xml加入 strutsfilter

2. 再加入springlistener,这样的话,webapp一旦启动,spring容器就初始化了

3. 规划strutsactionjsp展现

4. 加入struts.xml

a) 修改配置,由spring替代struts产生Action对象

5. 修改action配置

a) 把类名改为bean对象的名称,这个时候就可以使用首字母小写了

b) @Scope(prototype)不要忘记

iii. struts的读常量:

1. struts-default.xml

2. struts-plugin.xml

3. struts.xml

4. struts.properties

5. web.xml

iv. 中文问题:

1. Struts2.1.8已经修正,只需要改i18n.encoding = gbk

2. 使用springcharacterencoding

3. 需要严格注意filter的顺序

4. 需要加到Struts2filter前面

v. LazyInitializationException

1. OpenSessionInViewFilter

2. 需要严格顺序问题

3. 需要加到struts2filter前面

posted @ 2013-09-05 10:18  wuyongmao  阅读(269)  评论(0编辑  收藏  举报