第一步:
下面给出整合开发时Struts2、Hibernate3.5、Spring2.5需要的jar。
Struts2所需要的jar如下:
1、struts2-core-2.x.x.x.jar :Struts 2框架的核心类库
2、xwork-2.x.x.jar :XWork类库,Struts 2在其上构建
3、ognl-2.x.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性
4、freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写
5、commons-logging-1.1.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录。
6、commons-fileupload-1.2.1.jar 文件上传组件,2.1.6版本后必须加入此文件
7、struts2-json-plugin.jar struts2与json的插件
8、struts2-spring-plugin.jar struts2与spring的插件
Hibernate3.5所需要的jar如下:
hibernate核心安装包下的jar
1、hibernate3.jar
2、lib\required\*.jar
hibernate注解安装包下的jar
hibernate针对JPA的实现包
Spring安装包下的jar
1、spring.jar
2、aspectjrt.jar
3、aspectjweaver.jar
4、c3p0-0.9.1.2.jar
5、cglib-nodep-2.1_3.jar
6、common-annotations.jar
7、commons-logging.jar
8、log4j-1.2.15.jar
等等。具体情况因具体而定。如下图的jar
第二步:
在源文件夹下新建Spring配置文件beans.xml,在配置文件中配置c3p0数据源:
Xml代码
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver" />
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/s2sh?useUnicode=true&characterEncoding=UTF-8" />
- <property name="user" value="root" />
- <property name="password" value="root" />
- <property name="initialPoolSize" value="1" />
- <property name="minPoolSize" value="1" />
- <property name="maxPoolSize" value="300" />
- <property name="maxIdleTime" value="60" />
- <property name="acquireIncrement" value="5" />
- <property name="idleConnectionTestPeriod" value="60" />
- < span>bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingResources">
- <list>
- <value>cn/z_xiaofei168/domain/User.hbm.xml< span>value>
- < span>list>
- < span>property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect< span>prop>
- <prop key="hibernate.hbm2ddl.auto">update< span>prop>
- <prop key="hibernate.show_sql">true< span>prop>
- <prop key="hibernate.format_sql">false< span>prop>
- < span>props>
- < span>property>
- < span>bean>
第三步:
实体bean配置模板.hbm.xml
Xml代码
- xml version="1.0" encoding="UTF-8"?>
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="cn.z_xiaofei168.domain.User" table="users">
- <id name="id">
- <generator class="native" />
- < span>id>
- <property name="name" />
- <property name="pass" />
- <property name="email" />
- <property name="rdate" />
- < span>class>
- < span>hibernate-mapping>
第四步:
在web容器中实例化spring容器和配置struts2
Xml代码
- <STRONG>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener< span>listener-class>
- < span>listener>
- <filter>
- <filter-name>struts2< span>filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter< span>filter-class>
- < span>filter>
- <filter-mapping>
- <filter-name>struts2< span>filter-name>
- <url-pattern>/*< span>url-pattern>
- < span>filter-mapping>< span>STRONG>
第五步:
struts2的配置文件模板struts.xml如下。常量struts.objectFactory=spring明确指出将由spring负责创建Action实例。
Xml代码
- xml version="1.0" encoding="UTF-8" ?>
- "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
- "http://struts.apache.org/dtds/struts-2.1.7.dtd">
- <struts>
- <package name="z_xiaofei168" extends="struts-default" namespace="/csdn">
- <global-results>
- <result name="input">/index.jsp< span>result>
- < span>global-results>
- <filter>
- <filter-name>encoding< span>filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter< span>filter-class>
- <init-param>
- <param-name>encoding< span>param-name>
- <param-value>UTF-8< span>param-value>
- < span>init-param>
- < span>filter>
- <filter-mapping>
- <filter-name>encoding< span>filter-name>
- <url-pattern>/*< span>url-pattern>
- < span>filter-mapping>
第八步:
使用spring解决hibernate因session关闭导致的延迟加载例外问题。
【编辑推荐】