【Spring九】三大框架的整合

来源:互联网 发布:镇魔曲 知乎 编辑:程序博客网 时间:2024/04/27 17:06
三大框架整合的顺序:先hibernate,后spring,struts2
   1、建立工程
   2、设置编码格式
   3、设置所有的jsp的编码格式(preference->jsp)
   4、导入jar包
   5、写hibernate的配置文件、持久化类、映射文件
Classes.hbm.xml:
<?xml version= "1.0" encoding ="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
     <class name="com.oterman.domain.Classes" >
           <id name= "cid" type ="long" length="5">
               <column name= "cid"></column >
               <generator class= "increment"></generator >
           </id>
           <property name= "cname" length ="20"></ property>
           <property name= "description" length="200" ></property>
     </class >
</hibernate-mapping>
================================
hibernate.cfg.xml:
<?xml version= '1.0' encoding ='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
     <property name="connection.username"> root</ property>
     <property name="connection.password"> root</ property>
     <property name="connection.url">
          jdbc:mysql:// localhost:3306/hibernate_itheima03
     </property >
     <property name="dialect">
          org.hibernate.dialect.MySQLDialect
     </property >
     <property name="connection.driver_class">
          com.mysql.jdbc.Driver
     </property >
     
     <property name="hbm2ddl.auto"> update</ property>
     <property name="show_sql"> true</ property>

     <mapping resource="com/oterman/domain/Classes.hbm.xml" />

</session-factory>
</hibernate-configuration>

 
   6、写dao
   7、写service
   8、spring的配置文件
       (1)、写sessionFactory
applicationContext.xml:
<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/aop
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
     <import resource="applicationContext-db.xml"/>
     <import resource="applicationContext-classes.xml"/>
     
</beans>  
=======================================================
applicationContext-db.xml:
<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/aop
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
           <!-- 配置sessionFactory及aop事务 -->
           <bean id= "sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
               <property name= "configLocation" >
                    <value> classpath:hibernate/hibernate.cfg.xml</value >
               </property>
           </bean>
          
           <bean id= "transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
               <property name= "sessionFactory">
                    <ref bean= "sessionFactory"/>
               </property>
           </bean>
          
           <tx:advice id= "tx" transaction-manager="transactionManager" > 
               <tx:attributes>
                    <tx:method name= "save*" read-only="false" />
               </tx:attributes>
           </tx:advice>
          
           <aop:config>
           <!--      <aop:pointcut expression="execution(* com.oterman.dao.impl.*.*(..))" id="trans"/> -->
               <aop:pointcut expression="execution(* com.oterman.service.*.*(..))" id= "trans"/>
               <aop:advisor advice-ref="tx" pointcut-ref="trans" />
           </aop:config>

</beans>
       (2)、测试
     @Test
     public void testSessionFactory(){
          ApplicationContext context= new ClassPathXmlApplicationContext("spring/applicationContext.xml" );
          SessionFactory sessionFactory=(SessionFactory) context.getBean("sessionFactory" );
     }

       (3)、写dao和service
       (4)、测service
       (5)、aop的配置
       (6)、测试service,看service是否是代理对象
     @Test
     public void testSaveClasses(){
          ApplicationContext context= new ClassPathXmlApplicationContext("spring/applicationContext.xml" );
          ClassesService classesService=(ClassesService) context.getBean("classesService" );
          
          Classes classes= new Classes();
          classes.setCname( "hah");
          classesService.saveClasses(classes);
          
     }

   9、写action
   10、把action放入到spring中
   11、写action的配置文件
struts.xml:
<?xml version= "1.0" encoding ="UTF-8"?>
<!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
      "http://struts.apache.org/dtds/struts-2.1.7.dtd" >
<struts>
   <!-- 配置文件改了以后不用重新启动 -->
   <constant name="struts.devMode" value= "true"/>
   <!--全局错误处理  -->
   <package name="struts-global" namespace="/" extends="struts-default" >
           <global-results>
               <result name= "errHandler" type ="chain">
                    <param name="actionName" >errorProcessor </param>
               </result>
           </global-results>
          
           <global-exception-mappings>
               <exception-mapping exception="java.lang.Exception"
                    result= "errHandler" />
           </global-exception-mappings>

           <action name= "errorProcessor" class="com.oterman.exception.ErrorProcessor" >
               <result> error.jsp</result >
           </action>
     </package >
 
   <include file="struts2/struts-classes.xml"></include >
</struts>  

================================
struts-classes.xml:
<?xml version= "1.0" encoding ="UTF-8"?>
<!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
      "http://struts.apache.org/dtds/struts-2.1.7.dtd" >
<struts>
     <package name="classes"  namespace="/" extends="struts-global" >
           <!--class属性要和applicationContext.xml的id属性相对应,这样action才能有spring来产生  -->
           <action name= "classesAction_*" method ="{1}" class="classesAction" >
               <result> index.jsp</result >
           </action>
     </package >

</struts>  


   12、写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" >
  <welcome-file-list >
    <welcome-file >index.jsp </welcome-file>
  </welcome-file-list >
 
     <!--
     初始化spring容器时,会按照如下的规则加载spring的配置文件:
             1、如果在web.xml文件中有contextConfigLocation参数
                 则会按照该参数所指定的路径加载配置文件
             2、如果在web.xml文件中没有配置<context- param>,则会根据默认的路径
                 加载配置文件(WEB-INF/applicationContext.xml)
             3、在web- inf下的路径可以有如下的写法:
                 WEB-INF/*../application-*. xml
         说明:在web- inf下的配置文件没有办法进行测试
       -->
     <listener >
           <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class >
     </listener >
     <context-param >
           <param-name> contextConfigLocation</param-name >
           <param-value> classpath:spring/applicationContext.xml</param-value >
     </context-param >
     
     <!--加载struts容器
          加载了4个配置文件
              default.properties
              struts-default.xml
              struts-plugin.xml
          多加载了一个struts2-spring- plugin-2.1.8.1.jar 包下的xml文件
          <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring"
                class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
          <constant name="struts.objectFactory" value="spring" />
           说明:该类改变了action的创建方式,交由spring容器来创建action,
                 先从spring容器中查找action,根据struts2的配置文件中的action元素的class属性的值和spring容器中配置文件中的ID值做匹配
       -->
     <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>

   13、写jsp
   14、测试
整个工程结构视图:



0 0