1.配置Spring
(1)引入Spring相关架包(core,annotaion等)。
(2)创建applicationContext.xml配置文件。
2.配置Hibernate
(1)引入Hibernate所需要的架包。
(2)将dataSource和SessionFactory配置到applicationContext.xml,交给Spring管理。
(3)配置Hibernate相关属性(数据库方言,show_sql,formate_sql等)。
3.配置Struts2
(1)引入Struts2的架包。
(2)创建struts.xml配置文件。
(3)在web.xml中配置struts2。
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.在web.xml中配置spring文件的位置以及监听spring。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5.到后面struts的Action类交给Spring容器管理,则需要引入架包来关联Spring与Struts。
struts2-spring-plugin-2.0.14.jar
6.创建JavaBean类以及与数据库关联的XXX.hbm.xml的映射文件。
将XXX.hbm.xml配置到applicationContext.xml中,使得Hibernate能够通过实体类操作数据库。
7.将DAO,Service,Action类交给Spring管理,两种方法(任选一种):
(1)在applicationContext.xml定义bean注入。
(2)通过IOC控制反转进行注入,@Resource标签注入。必须写set和get方法
8.Action类交给Spring管理后,在struts.xml中action标签的属性class的值可以直接写bean的ID
9.Hibernate操作数据库方法:
(1)使用DAO实现类继承HibernateDAOSupport,然后通过getHibernateTemplete获取HibernateTemplete对象操作数据库。
在applicationContext.xml中不需要定义,只需要定义SessionFactory对象,HibernateTemplete会自动注入SessionFactory对象。
(2)也可以在applicationContext.xml中定义一个HibernateTemplete对象,通过IOC注入到DAO类中。
对于Spring AOP以及事务管理,需要另外研究。
(1)引入Spring相关架包(core,annotaion等)。
(2)创建applicationContext.xml配置文件。
2.配置Hibernate
(1)引入Hibernate所需要的架包。
(2)将dataSource和SessionFactory配置到applicationContext.xml,交给Spring管理。
(3)配置Hibernate相关属性(数据库方言,show_sql,formate_sql等)。
3.配置Struts2
(1)引入Struts2的架包。
(2)创建struts.xml配置文件。
(3)在web.xml中配置struts2。
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.在web.xml中配置spring文件的位置以及监听spring。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5.到后面struts的Action类交给Spring容器管理,则需要引入架包来关联Spring与Struts。
struts2-spring-plugin-2.0.14.jar
6.创建JavaBean类以及与数据库关联的XXX.hbm.xml的映射文件。
将XXX.hbm.xml配置到applicationContext.xml中,使得Hibernate能够通过实体类操作数据库。
7.将DAO,Service,Action类交给Spring管理,两种方法(任选一种):
(1)在applicationContext.xml定义bean注入。
(2)通过IOC控制反转进行注入,@Resource标签注入。必须写set和get方法
8.Action类交给Spring管理后,在struts.xml中action标签的属性class的值可以直接写bean的ID
9.Hibernate操作数据库方法:
(1)使用DAO实现类继承HibernateDAOSupport,然后通过getHibernateTemplete获取HibernateTemplete对象操作数据库。
在applicationContext.xml中不需要定义,只需要定义SessionFactory对象,HibernateTemplete会自动注入SessionFactory对象。
(2)也可以在applicationContext.xml中定义一个HibernateTemplete对象,通过IOC注入到DAO类中。
对于Spring AOP以及事务管理,需要另外研究。