【Spring九】三大框架的整合

10 篇文章 0 订阅
9 篇文章 0 订阅
三大框架整合的顺序:先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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Struts2、Hibernate5和Spring5三个框架整合可以实现一个完整的企业级Java应用程序的开发,具有以下优点: 1. 提高应用程序的模块化和可维护性:Struts2作为MVC框架,可以将应用程序的业务逻辑和展示逻辑分离,提高了应用程序的模块化和可维护性;Hibernate5作为ORM框架,则可以将Java对象和数据库表之间的映射关系进行自动化处理,大大简化了数据访问层的开发工作;Spring5作为容器框架,则可以将应用程序中的各个组件进行管理和协调,提高了应用程序的模块化和可维护性。 2. 提高应用程序的可扩展性:Struts2、Hibernate5和Spring5三个框架整合可以实现组件化的开发方式,提高了应用程序的可扩展性。例如,可以通过配置文件来实现组件的注入和替换,从而方便地修改应用程序的行为。 3. 提高应用程序的性能:Struts2、Hibernate5和Spring5三个框架整合可以通过缓存机制、连接池等方式来提高应用程序的性能。例如,Hibernate5可以通过二级缓存和查询缓存来减少数据库访问次数,从而提高了应用程序的性能。 4. 提高应用程序的安全性:Struts2、Hibernate5和Spring5三个框架整合可以通过拦截器和过滤器等方式来实现对用户请求的统一处理和验证,从而提高了应用程序的安全性。 5. 提高应用程序的可测试性:Struts2、Hibernate5和Spring5三个框架整合可以通过依赖注入和Mock对象等方式来实现应用程序的单元测试和集成测试,从而提高了应用程序的可测试性。 综上所述,Struts2、Hibernate5和Spring5三个框架整合可以提高应用程序的模块化、可维护性、可扩展性、性能、安全性和可测试性等方面的优点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值