MyEclipse6.0 整合 Spring2.0 , Strust2 ,Hibernate3.0

MyEclipse6.0 整合 Spring2.0 , Strust2 ,Hibernate3.0

(1) 添加struts2支持

1.导入jar包
freemarker.jar,ognl.jar,struts2-core.jar,xwork.jar,需要注意的是,struts2-core.jarstruts2-all.jar不能共存。

2.添加struts.xml

3.修改web.xml

(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

(4) 整合spring,hibernate

1.修改数据源

a.采用spring的数据源,首先添加类库Spring 2.0 ORM/DAO/Hibernate3.0
数据提供类: org.springframework.jdbc.datasource.DriverManagerDataSource
  1. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource ">
  2.      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  3.      <property name="url" value="jdbc:mysql://localhost:3306/mysql?characterEncoding=gb2312">
  4.      </property>
  5.      <property name="username" value="root"></property>
  6.      <property name="password" value="sa"></property>
  7. </bean>


b.apache数据池,所需jar包,commons-dbcp.jar,commons-pool.jarcommons-collections.jar
数据提供类: org.apache.commons.dbcp.BasicDataSource
  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  2.      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  3.      <property name="url" value="jdbc:mysql://localhost:3306/mysql?characterEncoding=gb2312">
  4.      </property>
  5.      <property name="username" value="root"></property>
  6.      <property name="password" value="sa"></property>
  7. </bean>


c.JNDI数据池
数据提供类: org.springframework.jndi.JndiObjectFactoryBean
  1. <bean id="dataSource" class="org.springframework.indi.JndiObjectFactoryBean"> 
  2.      <property name="jndiName">
  3.          <value>jdbc/TestDB</value> 
  4.      </property>      
  5. </bean>


(5) 整合spring,struts2

1.导入struts2-spring-plugin.jar,struts2目录下可以找到

2.struts.xml 进行常量配置,交给spring管理
  1. <struts/>
  2.      <constant name="struts.objectFactory" value="spring" />
  3. <struts/>


3.web.xml 把监听权交给spring
  1. <listener/>
  2.      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. <listener/>


4.applicationContext.xml 里创建action对象
  1. <bean id="TestAction" class="com.lmj.web.action.TestAction">
  2.      <property name="tempDAO" ref="tempDAO"></property>
  3. </bean>


5.struts.xml 里创建action对象,name必须与applicationContext.xml里bean的id一致
  1. <action name="TestAction" class="com.lmj.web.action.TestAction">
  2.      <result>/service/welcome.jsp</result>
  3. </action>
posted @ 2009-04-19 17:42  情义俩难全  阅读(663)  评论(0编辑  收藏  举报