SSH框架集成详细解读

来源:互联网 发布:淘宝造生活怎么报名 编辑:程序博客网 时间:2024/05/16 12:13

来自:http://blog.csdn.net/zndxlxm/article/details/8714383

在三大框架集成中需要几个配置文件,包括如下:

web.xml配置文件:

   (1)配置struts2的过滤器   (2)spring框架的配置文件   (3)会话监听器   (4)编码的配置

[java] view plaincopy
  1. <span style="font-size:14px;"><?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.     <welcome-file-list>  
  7.         <welcome-file>index.jsp</welcome-file>  
  8.     </welcome-file-list>  
  9.     <filter>  
  10.         <filter-name>Struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  12.     </filter>  
  13.     <filter-mapping>  
  14.         <filter-name>Struts2</filter-name>  
  15.         <url-pattern>/*</url-pattern>  
  16.     </filter-mapping>  
  17.   
  18.     <context-param>  
  19.         <param-name>contextConfigLocation</param-name>  
  20.         <param-value>classpath:applicationContext.xml</param-value>  
  21.     </context-param>  
  22.   
  23.     <listener>  
  24.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  25.     </listener>  
  26.   
  27.     <filter>  
  28.         <filter-name>OpenSessionInViewFilter</filter-name>  
  29.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  30.     </filter>  
  31.   
  32.     <filter-mapping>  
  33.         <filter-name>OpenSessionInViewFilter</filter-name>  
  34.         <url-pattern>/*</url-pattern>  
  35.     </filter-mapping>  
  36.   
  37.     <filter>  
  38.         <filter-name>CharacterEncodingFilter</filter-name>  
  39.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  40.         <init-param>  
  41.             <param-name>encoding</param-name>  
  42.             <param-value>utf-8</param-value>  
  43.         </init-param>  
  44.     </filter>  
  45.   
  46.     <filter-mapping>  
  47.         <filter-name>CharacterEncodingFilter</filter-name>  
  48.         <url-pattern>/*</url-pattern>  
  49.     </filter-mapping>  
  50.   
  51. </web-app></span>  
applicationContext.xml配置文件:

      (1)读取dbcp数据源
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      (2)Hibernate的常用属性配置(注意到一定要配置方言dialect,否者会报错的)
      (3)事务管理的配置     
      (4)bean的配置

[html] view plaincopy
  1. <span style="font-size:14px;"><?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" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9.     <context:property-placeholder location="classpath:dbcpconfig.properties" />  
  10.   
  11.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  12.         <property name="driverClassName">  
  13.             <value>${driverClassName}</value>  
  14.         </property>  
  15.   
  16.         <property name="url">  
  17.             <value>${url}</value>  
  18.         </property>  
  19.   
  20.         <property name="username">  
  21.             <value>${username}</value>  
  22.         </property>  
  23.   
  24.         <property name="password">  
  25.             <value>${password}</value>  
  26.         </property>  
  27.   
  28.         <property name="initialSize">  
  29.             <value>${initialSize}</value>  
  30.         </property>  
  31.   
  32.         <property name="maxActive">  
  33.             <value>${maxActive}</value>  
  34.         </property>  
  35.   
  36.         <property name="maxIdle">  
  37.             <value>${maxIdle}</value>  
  38.         </property>  
  39.   
  40.         <property name="minIdle">  
  41.             <value>${minIdle}</value>  
  42.         </property>  
  43.   
  44.         <property name="maxWait">  
  45.             <value>${maxWait}</value>  
  46.         </property>  
  47.     </bean>  
  48.   
  49.     <bean id="sessionFactory"  
  50.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  51.         <property name="dataSource" ref="dataSource" />  
  52.   
  53.         <property name="hibernateProperties">  
  54.             <props>  
  55.                 <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>  
  56.                 <prop key="hibernate.show_sql">true</prop>  
  57.                 <prop key="hibernate.format_sql">true</prop>  
  58.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  59.             </props>  
  60.         </property>  
  61.   
  62.         <property name="mappingResources">  
  63.             <list>  
  64.                 <value>cn/csu/edu/graduateDesign/domain/TestUser.hbm.xml</value>  
  65.             </list>  
  66.         </property>  
  67.     </bean>  
  68.   
  69.     <!-- hibernate的事务管理器 -->  
  70.     <bean id="transactionManager"  
  71.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  72.         <property name="sessionFactory" ref="sessionFactory" />  
  73.     </bean>  
  74.   
  75.     <!-- 事务传播特性 -->  
  76.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  77.         <tx:attributes>  
  78.             <tx:method name="add*" propagation="REQUIRED" />  
  79.             <tx:method name="delete*" propagation="REQUIRED" />  
  80.             <tx:method name="modify*" propagation="REQUIRED" />  
  81.             <tx:method name="*" propagation="NOT_SUPPORTED" />  
  82.         </tx:attributes>  
  83.     </tx:advice>  
  84.   
  85.     <aop:config>  
  86.         <aop:pointcut expression="execution (* cn.csu.edu.graduateDesign.service.*.*(..))"  
  87.             id="AllMethod" />  
  88.         <aop:advisor advice-ref="txAdvice" pointcut-ref="AllMethod" />  
  89.     </aop:config>  
  90.   
  91.     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  92.         <property name="sessionFactory" ref="sessionFactory" />  
  93.     </bean>  
  94.       
  95.     <bean id="TestUserAction" class="cn.csu.edu.graduateDesign.web.struts.TestUserAction">  
  96.         <property name="testUserService" ref="testUserService"/>  
  97.     </bean>  
  98.       
  99.     <bean id="testUserService" class="cn.csu.edu.graduateDesign.service.impl.TestUserServiceImpl">  
  100.         <property name="testUserDao" ref="testUserDao"/>  
  101.     </bean>  
  102.       
  103.     <bean id="testUserDao" class="cn.csu.edu.graduateDesign.dao.impl.TestUserDaoHibernateImpl">  
  104.         <property name="hibernateTemplate" ref="hibernateTemplate"/>  
  105.     </bean>  
  106. </beans></span><span style="font-size: 18px;">  
  107. </span>  
(注:配置文件的bean里面引用了hibernateTemplate,这个是spring价包里面的,其他的bean,就是我自己

定义的一些类,在这里面利用set注入的方式进行实例化了,这里面的实例互相引用,我相信大家应该看得

懂)

struts.xml配置文件:

     (1)在这个配置文件里面有这样一句配置的情况
            <constant name="struts.objectFactory" value="spring"/>

            这句话的意思是Struts2的action由Spring来负责进行实例化  
           <!-- 指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring -->
      (2)这个配置文件的主要作用就是进行action的管理,一般先设定包
      <package name="user" extends="struts-default">
<action name="user" class="TestUserAction">
<result>/user_list.jsp</result>
<result name="login" type="redirect">/index.jsp</result>
</action>
</package>
      /*注意这里面的class没有写完全的路径,因为这里面的这个class的值只需要
       和application.xml的id要相同,就是如下这个配置

[java] view plaincopy
  1.       <bean id="TestUserAction" class="cn.csu.edu.graduateDesign.web.struts.TestUserAction">  
  2.     <property name="testUserService" ref="testUserService"/>  
  3. </bean>  
       这样这个action的实例就交给了spring进行实例化

     */
 dbcp的properties文件:进行数据源的配置(我是连接的oracle数据库)

      

[html] view plaincopy
  1. #连接设置  
  2. driverClassName=oracle.jdbc.driver.OracleDriver  
  3. url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl  
  4. username=liuxiaoming  
  5. password=liuxiaoming  
  6.   
  7. #<!-- 初始化连接 -->  
  8. initialSize=10  
  9.   
  10. #最大连接数量  
  11. maxActive=50  
  12.   
  13. #<!-- 最大空闲连接 -->  
  14. maxIdle=20  
  15.   
  16. #<!-- 最小空闲连接 -->  
  17. minIdle=5  
  18.   
  19. #<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->  
  20. maxWait=60000  
  21.   
  22.   
  23. #JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]   
  24. #注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。  
  25. connectionProperties=useUnicode\=true;characterEncoding\=utf-8  
  26.   
  27. #指定由连接池所创建的连接的自动提交(auto-commit)状态。  
  28. defaultAutoCommit=true  
  29.   
  30. #driver default 指定由连接池所创建的连接的只读(read-only)状态。  
  31. #如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)  
  32. defaultReadOnly=  
  33.   
  34. #driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。  
  35. #可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE  
  36. defaultTransactionIsolation=READ_UNCOMMITTED  

 我们发现在集成之后,3大框架的配置文件互相关联,hibernate的配置文件被集成到了application.xml里面了

spring里面封装了hibernate模板,struts里面action的实例化也交给了spring,在整个数据操作的时候的事务也

被进行AOP设置,这些都给我们开发带来了很大的方便

0 0
原创粉丝点击