MyEclipse6.0 整合 Spring2.0 , Strust2 ,Hibernate3.0
(1) 添加struts2支持
1.导入jar包
freemarker.jar,
ognl.jar,
struts2-core.jar,
xwork.jar,需要注意的是,
struts2-core.jar和
struts2-all.jar不能共存。
2.添加
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
- <!-- 是否为Struts的开发模式 -->
- <constant name="struts.devMode" value="true" />
- <!-- action的后缀 -->
- <constant name="struts.action.extension" value="do" />
- <!-- 是否重新加载国际化属性文件 -->
- <constant name="struts.i18n.reload" value="true" />
- <!-- 是否为Struts的开发模式 -->
- <constant name="struts.objectFactory" value="spring" />
- <!-- 是否重新加载xml配置 -->
- <constant name="struts.configuration.xml.reload" value="true"/>
- <!-- 是否允许动态方法调用 -->
- <constant name="struts.enable.DynamicMethodInvocation" value="true" />
-
- <package name="action" extends="struts-default">
- <action name="TestAction" class="com.lmj.web.action.TestAction">
- <result>/service/welcome.jsp</result>
- </action>
- </package>
- </struts>
3.修改
web.xml
- <filter>
- <filter-name>struts-cleanup</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.ActionContextCleanUp
- </filter-class>
- </filter>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- </filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>struts-cleanup</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
(2) 添加hibernate支持
1.先在MyEclipse Derby测试数据库连接成功后,然后添加hibernate支持。再切换到MyEclipse Database Explorer, 找到需要的表,右键选择Hibernate Reverse Engineering...,导入实体类以及与表相关联的xml即可。
如果手写,到网上找些Hibernate.cfg.xml,表关联的xml的配置更改一下,再导入jar包就行了。
(3) 添加spring支持
1.MyEclipse添加spring支持时,勾选常用的Spring 2.0 AOP,Spring 2.0 Core,Spring 2.0 Web
创建
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
-
- <!-- 数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url" value="jdbc:mysql://localhost:3306/mysql?characterEncoding=gb2312">
- </property>
- <property name="username" value="root"></property>
- <property name="password" value="sa"></property>
- </bean>
-
- <!-- session工厂 -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
- </bean>
-
- <!-- daos -->
- <bean id="tempDAO" class="com.lmj.web.dao.impl.TempDAO">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
-
- <!-- actions -->
- <bean id="TestAction" class="com.lmj.web.action.TestAction">
- <property name="tempDAO" ref="tempDAO"></property>
- </bean>
-
- <!-- 配置TransactionManager -->
- <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
-
- <!-- 配置事务策略 -->
- <tx:advice id="txAdvice" transaction-manager="myTxManager">
- <tx:attributes>
- <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
- <tx:method name="save*" propagation="REQUIRED"/>
- <tx:method name="update*" propagation="REQUIRED"/>
- <tx:method name="delete*" propagation="REQUIRED"/>
- <tx:method name="*" propagation="SUPPORTS" read-only="true" />
- </tx:attributes>
- </tx:advice>
-
- <aop:config>
- <aop:pointcut id="productServiceMethods" expression="execution(* service.*.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
- </aop:config>
- </beans>
(4) 整合spring,hibernate
1.修改数据源
a.采用spring的数据源,首先添加类库Spring 2.0 ORM/DAO/Hibernate3.0
数据提供类: org.springframework.jdbc.datasource.DriverManagerDataSource
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource ">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url" value="jdbc:mysql://localhost:3306/mysql?characterEncoding=gb2312">
- </property>
- <property name="username" value="root"></property>
- <property name="password" value="sa"></property>
- </bean>
b.apache数据池,所需jar包,
commons-dbcp.jar,
commons-pool.jar与
commons-collections.jar 数据提供类: org.apache.commons.dbcp.BasicDataSource
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url" value="jdbc:mysql://localhost:3306/mysql?characterEncoding=gb2312">
- </property>
- <property name="username" value="root"></property>
- <property name="password" value="sa"></property>
- </bean>
c.JNDI数据池
数据提供类: org.springframework.jndi.JndiObjectFactoryBean
- <bean id="dataSource" class="org.springframework.indi.JndiObjectFactoryBean">
- <property name="jndiName">
- <value>jdbc/TestDB</value>
- </property>
- </bean>
(5) 整合spring,struts2
1.导入
struts2-spring-plugin.jar,struts2目录下可以找到
2.struts.xml 进行常量配置,交给spring管理
- <struts/>
- <constant name="struts.objectFactory" value="spring" />
- <struts/>
3.web.xml 把监听权交给spring
- <listener/>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- <listener/>
4.applicationContext.xml 里创建action对象
- <bean id="TestAction" class="com.lmj.web.action.TestAction">
- <property name="tempDAO" ref="tempDAO"></property>
- </bean>
5.struts.xml 里创建action对象,name必须与applicationContext.xml里bean的id一致
- <action name="TestAction" class="com.lmj.web.action.TestAction">
- <result>/service/welcome.jsp</result>
- </action>