`

Struts2+Spring+Hibernate

    博客分类:
  • SSH
阅读更多

现在分享部分源码,来说明一些注意事项。

以下是部分搭建过程及源码:

1.先组合实现Hibernate3.2+Spring2.5支持,删除hibernate.cfg.xml文件,修改applicationContext.xml文件的内容,增加SessionFactory和dataSource的设置。

2.通过MyEclipse的向导方式,生成POJO类和对应的映射文件。

3.修改applicationContext.xml文件中<property name="mappingResources">元素的内容。

4.编写DAO接口和实现类。

5.修改applicationContext.xml文件,增加对Dao实现类的配置。

6.组合Struts2和Spring2.5,修改web.xml文件,增加struts2的所需要的过滤器配置。

7.增加struts2相应类库,增加struts2与spring的配置jar包。

8.拷贝struts.xml文件到src根目录下,再修改struts.xml文件,进行常量配置。

9.修改web.xml文件,配置Spring监听器,和上下文变量。并增加OpenSessionInViewFilter的设置。

10.写入action类。

11.配置struts.xml文件。

12.修改applicationContext.xml

13.编写Jsp文件。

14.加载运行项目。

下面是关键文件的源码:

struts.xml源码:

Java代码
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd" >  
  5. <struts>  
  6. <!-- struts2委托spring管理 -->  
  7.     <constant name="struts.objectFactory"  value= "spring" />  
  8.     <!-- /crm/emp/add.action -->  
  9.     <package  name= "crm_employee"   extends = "struts-default"  namespace= "/emp" >  
  10.         <action name="add"   class = "addBean"  method= "add" >  
  11.             <result>add.action</result>  
  12.             <result>/emp/add_suc.jsp</result>  
  13.         </action>  
  14.         <action name="list"   class = "listBean"  method= "list" >  
  15.             <result>/emp/list.jsp</result>  
  16.         </action>  
  17.         <action name="delete"   class = "deleteBean"  method= "delete" >  
  18.             <result>delete.action</result>  
  19.             <result>/emp/delete_suc.jsp</result>  
  20.         </action>  
  21.         <action name="update"   class = "updateBean"  method= "update" >  
  22.             <result>update.action</result>  
  23.             <result>/emp/edit_suc.jsp</result>  
  24.         </action>  
  25.         <action name="edit"   class = "editBean"  method= "edit" >  
  26.             <result>/emp/edit.jsp</result>  
  27.         </action>  
  28.         <!-- Add actions here -->  
  29.     </package >  
  30. </struts>  
<?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>
<!-- struts2委托spring管理 -->
    <constant name="struts.objectFactory" value="spring"/>
    <!-- /crm/emp/add.action -->
    <package name="crm_employee" extends="struts-default" namespace="/emp">
        <action name="add" class="addBean" method="add">
            <result>add.action</result>
            <result>/emp/add_suc.jsp</result>
        </action>
        <action name="list" class="listBean" method="list">
            <result>/emp/list.jsp</result>
        </action>
        <action name="delete" class="deleteBean" method="delete">
            <result>delete.action</result>
            <result>/emp/delete_suc.jsp</result>
        </action>
        <action name="update" class="updateBean" method="update">
            <result>update.action</result>
            <result>/emp/edit_suc.jsp</result>
        </action>
        <action name="edit" class="editBean" method="edit">
            <result>/emp/edit.jsp</result>
        </action>
        <!-- Add actions here -->
    </package>
</struts>

 

web.xml源码:

Xml代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < web-app   version = "2.5"   xmlns = "http://java.sun.com/xml/ns/javaee"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  4.     xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  6.     <!-- 配置spring的监听器 -->   
  7.     < context-param >   
  8.         < param-name > contextConfigLocation </ param-name >   
  9.         < param-value > /WEB-INF/applicationContext*.xml </ param-value >   
  10.     </ context-param >   
  11.     <!-- 开启监听 -->   
  12.     < listener >   
  13.         < listener-class >   
  14.             org.springframework.web.context.ContextLoaderListener  
  15.         </ listener-class >   
  16.     </ listener >   
  17.     <!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->   
  18.     < filter >   
  19.         < filter-name > lazyLoadingFilter </ filter-name >   
  20.         < filter-class >   
  21.             org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  
  22.         </ filter-class >   
  23.     </ filter >   
  24.     <!-- 设置监听加载上下文 -->   
  25.     < filter >   
  26.         < filter-name > struts2 </ filter-name >   
  27.         < filter-class >   
  28.             org.apache.struts2.dispatcher.FilterDispatcher  
  29.         </ filter-class >   
  30.     </ filter >   
  31.     < filter-mapping >   
  32.     < filter-name > lazyLoadingFilter </ filter-name >   
  33.     < url-pattern > *.action </ url-pattern >   
  34.     </ filter-mapping >   
  35.     < filter-mapping >   
  36.         < filter-name > struts2 </ filter-name >   
  37.         < url-pattern > /* </ url-pattern >   
  38.     </ filter-mapping >   
  39.     < welcome-file-list >   
  40.     < welcome-file > index.jsp </ welcome-file >   
  41.     </ welcome-file-list >   
  42. </ web-app >   
<?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的监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>
    <!-- 开启监听 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->
    <filter>
        <filter-name>lazyLoadingFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
    </filter>
    <!-- 设置监听加载上下文 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 applicationContext.xml源码:

 

 

 

Xml代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < beans   xmlns = "http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:aop = "http://www.springframework.org/schema/aop"   
  5.     xmlns:tx = "http://www.springframework.org/schema/tx"   
  6.     xsi:schemaLocation ="  
  7.     http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.     http://www.springframework.org/schema/tx  
  10.     http://www.springframework.org/schema/tx/spring-tx-2.0.xsd  
  11.     http://www.springframework.org/schema/aop  
  12.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">   
  13.     <!-- 配置Hibernate支持 -->   
  14.     < bean   id = "dataSource"   
  15.         class = "org.apache.commons.dbcp.BasicDataSource" >   
  16.         < property   name = "driverClassName"   
  17.             value = "com.mysql.jdbc.Driver" >   
  18.         </ property >   
  19.         < property   name = "url"   
  20.             value = "jdbc:mysql://localhost:3306/tables" >   
  21.         </ property >   
  22.         < property   name = "username"   value = "root" > </ property >   
  23.         < property   name = "password"   value = "hicc" > </ property >   
  24.     </ bean >   
  25.     < bean   id = "sessionFactory"   
  26.         class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
  27.         < property   name = "dataSource" >   
  28.             < ref   bean = "dataSource"   />   
  29.         </ property >   
  30.         < property   name = "hibernateProperties" >   
  31.             < props >   
  32.                 < prop   key = "hibernate.dialect" >   
  33.                     org.hibernate.dialect.MySQLDialect  
  34.                 </ prop >   
  35.                 < prop   key = "hibernate.show_sql" > true </ prop >   
  36.             </ props >   
  37.         </ property >   
  38.         < property   name = "mappingResources" >   
  39.             < list >   
  40.                 < value > com/sy/crm/model/Employee.hbm.xml </ value >   
  41.             </ list >   
  42.         </ property >   
  43.     </ bean >   
  44.     < bean   id = "employeeDao"   
  45.         class = "com.sy.crm.dao.hibernate.EmployeeDaoHibernate" >   
  46.         < property   name = "sessionFactory" >   
  47.             < ref   bean = "sessionFactory"   />   
  48.         </ property >   
  49.     </ bean >   
  50.     < bean   id = "employeeManager"   
  51.         class = "com.sy.crm.service.impl.EmployeeManagerImpl" >   
  52.         < property   name = "employeeDao" >   
  53.             < ref   bean = "employeeDao"   />   
  54.         </ property >   
  55.     </ bean >   
  56.       
  57.     < bean   id = "addBean"   class = "com.sy.crm.action.EmployeeAction"   scope = "prototype" >   
  58.         < property   name = "employeeManager" >   
  59.             < ref   bean = "employeeManager"   />   
  60.         </ property >   
  61.     </ bean >   
  62.     < bean   id = "listBean"   class = "com.sy.crm.action.EmployeeAction"   scope = "prototype" >   
  63.         < property   name = "employeeManager" >   
  64.             < ref   bean = "employeeManager"   />   
  65.         </ property >   
  66.     </ bean >   
  67.     < bean   id = "deleteBean"   class = "com.sy.crm.action.EmployeeAction"   scope = "prototype" >   
  68.         < property   name = "employeeManager" >   
  69.             < ref   bean = "employeeManager"   />   
  70.         </ property >   
  71.     </ bean >   
  72.     < bean   id = "updateBean"   class = "com.sy.crm.action.EmployeeAction"   scope = "prototype" >   
  73.         < property   name = "employeeManager" >   
  74.             < ref   bean = "employeeManager"   />   
  75.         </ property >   
  76.     </ bean >   
  77.     < bean   id = "editBean"   class = "com.sy.crm.action.EmployeeAction"   scope = "prototype" >   
  78.         < property   name = "employeeManager" >   
  79.             < ref   bean = "employeeManager"   />   
  80.         </ property >   
  81.     </ bean >   
  82.     <!-- 事务管理器 -->   
  83.     < bean   id = "transactionManager"    
  84.     class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >   
  85.     < property   name = "sessionFactory" >   
  86.     < ref   local = "sessionFactory" />   
  87.     </ property >   
  88.     </ bean >   
  89.     <!-- 配置事务特性,配置add,delete,update开始的方法,事务传播特性为required -->   
  90.     < tx:advice   id = "txAdvice"   transaction-manager = "transactionManager" >   
  91.     < tx:attributes >   
  92.     < tx:method   name = "add*"   propagation = "REQUIRED" />   
  93.     < tx:method   name = "delete*"   propagation = "REQUIRED" />   
  94.     < tx:method   name = "update*"   propagation = "REQUIRED" />   
  95.     < tx:method   name = "*"   read-only = "true" />   
  96.     </ tx:attributes >   
  97.     </ tx:advice >   
  98.     <!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包,  
  99.     类中所有方法需要,还需要参考tx:advice的设置 -->   
  100.     < aop:config >   
  101.     < aop:pointcut   id = "allManagerMethod"   expression ="execution(*  
  102.     com.sy.crm.service.*.*(..))"/>   
  103.     < aop:advisor   advice-ref = "txAdvice"   pointcut-ref = "allManagerMethod" />   
  104.     </ aop:config >   
  105.     </ beans >   
<?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.5.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">
    <!-- 配置Hibernate支持 -->
    <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/tables">
        </property>
        <property name="username" value="root"></property>
        <property name="password" value="hicc"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/sy/crm/model/Employee.hbm.xml</value>
            </list>
        </property>
    </bean>
    <bean id="employeeDao"
        class="com.sy.crm.dao.hibernate.EmployeeDaoHibernate">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="employeeManager"
        class="com.sy.crm.service.impl.EmployeeManagerImpl">
        <property name="employeeDao">
            <ref bean="employeeDao" />
        </property>
    </bean>
    
    <bean id="addBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        <property name="employeeManager">
            <ref bean="employeeManager" />
        </property>
    </bean>
    <bean id="listBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        <property name="employeeManager">
            <ref bean="employeeManager" />
        </property>
    </bean>
    <bean id="deleteBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        <property name="employeeManager">
            <ref bean="employeeManager" />
        </property>
    </bean>
    <bean id="updateBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        <property name="employeeManager">
            <ref bean="employeeManager" />
        </property>
    </bean>
    <bean id="editBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        <property name="employeeManager">
            <ref bean="employeeManager" />
        </property>
    </bean>
    <!-- 事务管理器 -->
    <bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>
    <!-- 配置事务特性,配置add,delete,update开始的方法,事务传播特性为required -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="*" read-only="true"/>
    </tx:attributes>
    </tx:advice>
    <!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包,
    类中所有方法需要,还需要参考tx:advice的设置 -->
    <aop:config>
    <aop:pointcut id="allManagerMethod" expression="execution(*
    com.sy.crm.service.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    </aop:config>
    </beans>

 

 

第一点注意的是,搭建出项目,一定会报错,因为Spring 2.5 AOP Libraries中的asm的三个jar包会和

Hibernate 3.2 Core Libraries中的asm的jar包中的某些类中有冲突。所以一定要删除Spring中的三个asm的jar包。

第二点要注意的是,struts2的配置包的导入,需要的是5个jar包分别是:

struts2-core-2.0.11.2.jar

freemarker-2.3.8.jar

ognl-2.6.11.jar

xwork-2.05.jar

commons-logging-1.0.4.jar

struts2+spring配置包:struts2-spring-plugin-2.0.11.2.jar

网上有些还说需要把4个spring的包拷到lib下,我是拷了但是,并不确定这样做是否有必要。

总之是正常运行了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics