SSH开发环境的配置:web.xml、Spring集成hibernate和Spring集成Struts2

来源:互联网 发布:网络推广优化三尾狐 编辑:程序博客网 时间:2024/05/16 16:59

一、struts + hibernate + Spring整合开发包的导入:从myeclipse集成开发工具中导入或者从相应网站下载然后放在工程项目WEB-INF包下的lib包内即可。

二、本文主要说明开发包导入后对配置文件的配置。

三、相应配置文件的配置:web.xml、struts.xml、applicationContext.xml。

1.在web.xml配置中配置Spring和Struts2

在web.xml中需要配置Struts2的Filter、Spring配置文件的位置、加载器。Spring使用Listener进行初始化,Struts2使用Filter作为分发器,将以“.action”结尾的URL及以/struts开的URL交给Struts2处理。代码如下:

web.xml

<context-param>

         <param-name>contextConfigLocation</param-name>            <!--Spring参数-->

          <param-value>

classpath:applicationContext.xml

           </param-value>                                                                              <!--指定需要加载的配置文件的位置-->

</context-param>

<!--spring加载器-->

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>



<filter>
  <filter-name>struts2</filter-name>                                                                                       <!--struts2的Filter-->
  <filter-class>
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter             <!--struts2的入口-->
  </filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>                                                 <!--配置URL的映射-->
  <url-pattern>/*</url-pattern>                                                             <!--接受所有的URL-->
  </filter-mapping>


2.在struts2配置文件中配置Spring

         PersonAction(以PersonAction为例,是一个java类)需要配置到Struts2配置文件struts.xml中。一定要用<constant/>添加名struts.objectFactory的常量,把值设为spring,表示该Action由Spring产生。然后配置PersonAction时,把<action/>的class属性改为personAction(任意的标识符都可以)。Struts2将会到Spring中寻找名为personAction的PersonAction对象。struts.xml配置代码如下:

                        struts.xml

<struts>
<!-- 一定要用这一项添加名为sturts.objectiveFactory的常量,把值设为spring,表示该Action由spring产生 -->
    <constant name="struts.objectFactory" value="spring" />
    <!--当是开发阶段时为true,当上线后可设为false -->
<constant name="struts.devMode" value="true" />


<package name="standard" namespace="" extends="struts-default">

   <!-- 定义一个全局result -->
<global-results>
<!-- 定义名为login的全局result -->
<result name="tologin">/login.jsp</result>
<!-- 定义名为error的全局result -->
<result name="error">/error.jsp</result>
<!-- 定义名为exception的全局result -->
<result name="exception">/exception.jsp</result>
</global-results>

<!-- 配置action -->
<action name="personAction" class="personAction">              <!--name和class的属性值可以不相同-->
<result name="success">/login.jsp</result>
<result name="ok">/success.jsp</result>
</action>
<action name="personList" class="personListAction">
<result name="show">/list.jsp</result>
</action>
</package>
</struts>    


3.Spring中配置Action、Service和Dao.

      Spring负责生成Action对象。id属性对就struts.xml中<action>的class属性。因为Struts2的Action不是单个实例,因此scope属性需要配置为prototype(默认为singleton,只会生成一个实例)。配置代码如下:

                                 applicationContext.xml

<!-- 配置DAO -->
<bean id="personDao" class="com.standard.daoImpl.PersonDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置Service -->
<bean id="famillyService" class="com.standard.serviceImpl.PersonServiceImpl">
<property name="dao" ref="personDao"></property>
</bean>

<!-- 配置action -->
<bean id="personAction"  scope="prototype"                                           <!--Struts2的Action-->

class="com.standard.action.PersonAction" >                            <!--实现类-->

       </bean>       
<bean id="personListAction"  class="com.standard.action.PersonListAction" scope="prototype">
<property name="personService" ref="personService"></property>                <!-- 设置属性 -->
</bean>

提示:一定要配置Struts2的Action的scope的属性为prototype。浏览器每请求一次Struts2.Spring都会生成一个新的Action对象。


4.配置集成Hibernate

       传统上讲,Spring需要一个配置文件,Hibernate也需要一个配置文件。在Spring中,可以只使用Spring的配置文件。下面是Hibernate在Spring中的配置。

       (1).在Spring中配置Hibernate

                由于Hibernate全部交给Spring来管理,Hibernate不再需要自已的配置文件,所有的参数都配置在Spring中。Spring中需要配置数据源、SessionFactory及PersonDaoImpl,并配置实体类。代码如下:

                                     applicationContext.xml

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<!-- 注:jdbcUrl的值不能换行! -->
<property name="jdbcUrl">
<value>jdbc:mysql://127.0.0.1:3306/ssh?useUnicode=true&amp;characterEncoding=UTF-8</value>
</property>
<property name="user">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>


<!--连接池中保留的最小连接数。-->
<property name="minPoolSize">
<value>5</value>
</property>


<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize">
<value>30</value>
</property>


<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>5</value>
</property>


<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">
<value>60</value>
</property>


<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement">
<value>5</value>
</property>


<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>60</value>
</property>
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider.class
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/standard/pojo/Person.hbm.xml</value>
</list>
</property>
</bean>



下面是三个配置文件完整的配置(下面实用的是Familly实例):

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- 初始化spring容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<!-- 指定需加载的spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>



  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


sturts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 一定要用这一项添加名为sturts.objectiveFactory的常量,把值设为spring,表示该Action由spring产生 -->
    <constant name="struts.objectFactory" value="spring" />
    <!--当是开发阶段时为true,当上线后可设为false -->
<constant name="struts.devMode" value="true" />
<!-- 定义一个名为standard的包,继承Struts2.1的默认包 -->
<!-- 如果namespace的值不为空,则ssh前面必须有“/”,否则会报错 。如下所示-->
<!-- <package name="standard" namespace="/ssh" extends="struts-default"> -->
<package name="standard" namespace="" extends="struts-default">

<!-- 定义一个全局result -->
<global-results>
<!-- 定义名为login的全局result -->
<result name="tologin">/login.jsp</result>
<!-- 定义名为error的全局result -->
<result name="error">/error.jsp</result>
<!-- 定义名为exception的全局result -->
<result name="exception">/exception.jsp</result>
</global-results>

<!-- 配置action -->
<action name="loginAction" class="loginActionSpring">
<result name="success">/login.jsp</result>
<result name="ok">/success.jsp</result>
</action>
<action name="famillyListAction" class="listAction">
<result name="show">/list.jsp</result>
</action>
</package>
</struts>    


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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<!-- 注:jdbcUrl的值不能换行! -->
<property name="jdbcUrl">
<value>jdbc:mysql://127.0.0.1:3306/ssh?useUnicode=true&amp;characterEncoding=UTF-8</value>
</property>
<property name="user">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>


<!--连接池中保留的最小连接数。-->
<property name="minPoolSize">
<value>5</value>
</property>


<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize">
<value>30</value>
</property>


<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">
<value>5</value>
</property>


<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">
<value>60</value>
</property>


<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement">
<value>5</value>
</property>


<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod">
<value>60</value>
</property>
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider.class
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/standard/pojo/Familly.hbm.xml</value>
</list>
</property>
</bean>

<!-- hibernate事务管理器 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 配置sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 事务管理规则 -->
<bean id="hibernateTransactionAtttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<!-- 具备事务管理 的方法名 -->
<property name="properties">
<props>
<!-- 所有方法都加上事务 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<!-- 事务工厂代理类 -->
<bean id="transactionProxyFactory" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="hibernateTransactionManager"></property>
<!-- 被管理的对象 -->
<property name="target" ><ref bean="famillyService"></ref>
</property>
<!-- 设置事务管理规则 -->
<property name="transactionAttributeSource" ref="hibernateTransactionAtttributeSource"></property>
</bean>

<!-- 配置DAO -->
<bean id="famillyDao" class="com.standard.daoImpl.FamilyDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 配置Service -->
<bean id="famillyService" class="com.standard.serviceImpl.FamillyServiceImpl">
<property name="dao" ref="famillyDao"></property>
</bean>

<!-- 配置action -->
<bean id="loginActionSpring" class="com.standard.action.LoginAction" scope="prototype"></bean>
<bean id="listAction"  class="com.standard.action.FamillyListAction" scope="prototype">
<property name="famillyService" ref="famillyService"></property>
</bean>



</beans>