zoukankan      html  css  js  c++  java
  • SSH框架的配置

    SSH =   struts2.2---控制层  +

       hibernate3.3---持久化层 +

       spring3.0---(ioc:创建对象和对象的维护:)

               (aop:主流业务和辅助功能分离:事务管理)

     ****************************************************************************************************************************************************************

    步骤:

    1  创建web项目(struts2只用于web)

    2  导入struts2的jar包(不建议使用工具导入:包过多)  通过课件导入

       >基本jar包即可(8个) + 另外一个包(struts和spring整合:struts2-spring-plugin-2.2.1.1.jar)

       >在src下创建struts2的配置文件  struts.xml(创建package指定action的url)

       >在web.xml中 配置struts2的核心过滤器

    3  通过工具导入hibernate 3.3的jar包(必须使用工具导入,否则反向工程不能使用)

        >不用annotation包(hibernate的注解使用限制较多,不建议使用)

        >不用创建sessionFactory(此任务交给spring)

    4 导入spring 3.0的jar包 

       >spring-ioc  spring-aop spring-jdbc  spring-web(4个jar)

       >选择自动sessionFactory的bean

       >删除cglib.jar(与其他包冲突  并且无用,后果:不能使用cglib方式动态代理,即代理的类必须有接口):先remover build path :再delete  如果不行 就关闭myeclipse 在workspace中删除  再开启

    5 xml配置:

       >struts.xml 声明使用spring来管理对象创建和维护 。

       >web.xml中配置  声明使用spring框架  使用监听器告诉项目 使用spring框架(web.xml直接复制不用更改.)

     6.测试(看是否有错!)

     *******************************************************

    框架搭成后,后续工作

    1 通过反向工程 由表来创建实体类和映射文件

          指定:(自动创建dao)

          把dao类放到dao包中(要拖拽  不要复制粘贴)

    (确认hibernate.cfg.xml中是否加入hbm.xml文件)

    2 在spring的配置文件中创建一个事务管理的bean

           把事务bena和dao bean关联形成一个代理bean

    (springBean中配置较多,易出错,仔细复制并检查)

    3  action类配置:spring配置和struts.xml配置

    4 jsp和Action类(引用也必须有get/set方法。)

    xml配置代码:

    框架出错,最常见的为xml配置错误。xml文件中最容易出错的地方为spring的xml、xml间的对应关系、大小写字母

    1.先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">
    <display-name></display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 配置struts2的核心过滤器 -->
    <filter>
    <filter-name>struts</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>struts</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 配置spring -->
    <!-- 使用监听器告诉项目 使用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>
    </web-app>

    2. 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>
    <!-- 设置字符集struts.i18n.encoding=UTF-8-->
    <constant name="struts.i18n.encoding" value="gbk"></constant>

    <!-- 配置spring 指定对象创建使用spring来管理
    :参数参考:struts.core.jar中的default.properties
    使用spring管理对象:struts.objectFactory = spring
    自动装配方式:struts.objectFactory.spring.autoWire = name -->
    <constant name="struts.objectFactory" value="spring"/>
    <constant name="struts.objectFactory.spring.autoWire" value="name"/>

    <!-- 配置action对应的url -->
    <package name="p1" extends="struts-default" namespace="/">
    <!-- 注意:action的class写spring中bean的id -->
    <action name="regist" class="userAction" method="regist">
      <result name="success">ok.jsp</result>
      </action>
    </package>
    </struts>

    3.hibernate.cfg.xml(为自动生成。可以写到spring的核心文件  ,起名叫 datasource)

    4.实体类的 hbm.xml文件。为反向工程自动生成并加入到hibernate.cfg.xml

    5.applicationContext.XML(此为最基本的配置形式,定义并使用了事务,但未使用AOP和TX标签时的写法)

    <?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="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="configLocation"value="classpath:hibernate.cfg.xml">
      </property>
    </bean>
    <bean id="UserDAO" class="com.bochy.dao.UserDAO">
      <property name="sessionFactory">
        <ref bean="sessionFactory" />
      </property>
    </bean>

    <!-- 创建一个bean 指定事务管理 -->
    <bean id="tranManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
        <ref bean="sessionFactory"/>
      </property>
    </bean>
    <!-- 把事务bean和dao bean关联形成一个代理bean -->
    <bean id="UserDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
       <!-- 指定代理目标对象 -->
        <property name="target">
          <ref bean="UserDAO"/>
        </property>
      <!-- 指定事务bean(类似于aop中的装备) -->
        <property name="transactionManager">
          <ref bean="tranManager"/>
         </property>
      <!-- 指定代理对象替换目标类 -->
        <property name="proxyTargetClass">
          <value>true</value>
        </property>
      <!-- 指定事务的传播属性:为PROPAGATION_REQUIRED -->
        <property name="transactionAttributes">
          <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
          </props>
        </property>
    </bean>

    <!-- 创建action bean -->
    <bean id="userAction" class="com.bochy.action.UserAction">
      <!-- 指定属性userdao的值 -->
      <property name="userDao">
      <!-- 使用代理对象 -->
        <ref bean="UserDAOProxy"/>
      </property>
    </bean>

    </beans>

    =======================================================================================================

    spring的xml详解:

    spring的xml的域名空间:

    <!-- 加入context,tx,aop空间,容易记,(格式一样,换个单词而已)命名时不起作用,很有可能是写错了-->
    <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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

    ============================================================

    tx标签形式的事物管理   

    <!-- 创建一个bean 指定事务管理 -->
    <bean id="tranManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
        <ref bean="sessionFactory"/>
       </property>
    </bean>

       不使用注解
    <!-- 配置事务传播特性 -->
    <tx:advice id="dataAdvice" transaction-manager="tranManager">
      <tx:attributes>
        <tx:method name=" * " propagation="REQUIRED" />
      </tx:attributes>
    </tx:advice>
    <!-- 配置参与事务的类(使用AOP) -->
    <aop:config>
      <aop:pointcut id="allTestServiceMethod" expression="execution(* service.*.*(..))"/>
      <aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="dataAdvice" />
    </aop:config>

    使用注解

    <!-- 开启事务控制的注解支持 -->  

    <tx:annotation-driven transaction-manager="tranManager"/>

    在类上使用@Transactional(属性中可设事物的传播行为propagation=Propagation.XXXX)

    AOP标签的使用   

    不用AOP标签时(只能对一个类设置代理,且在其他类对其的引用标签上,定义为“proxyShop2”不是“”shop2“,必须对前置,后置,环绕,异常写成4个类,实现不同的接口)

      <!-- 为目标对象创建bean -->
    <bean id="shop2" class="com.bochy.entity.Shop"/>

      <!-- 为所有装备/通知创建bean -->
    <bean id="after" class="com.bochy.advice.MyAfterAdvice"/>
    <bean id="before" class="com.bochy.advice.MyBeforeAdvice"/>
    <bean id="around" class="com.bochy.advice.MyAroundAdvice"/>
    <bean id="throw" class="com.bochy.advice.MyThrowsAdvice"/>

    <bean id="proxyShop2" class="org.springframework.aop.framework.ProxyFactoryBean">

      <property name="target" ref="shop2"/>
      <!--指定接口中的方法添加装备(当目标类实现多个接口时,指定代理类对应的接口) -->
      <property name="interfaces">
        <list>
            <value>com.bochy.inter.ShopInter</value>
         </list>
      </property>
      <property name="interceptorNames">
        <list>
          <value>myPointCutAdvice</value>  //使用切点
          <value>around</value>
          <value>throw</value>
        </list>
      </property>
    </bean>
      <!-- 设置切点bean -->
    <bean id="myPointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
      <property name="advice">
        <ref bean="before"/>  //只加  前置通知/增强
      </property>
      <!-- 使用正则表示匹配的方法:
          .表示任意字符        +表示前面的字符出现>=1次
        *表示前面的字符出现>=0次   -->
      <property name="pattern">
        <value>.*Goods.*</value>
      </property>
    </bean>

    使用AOP标签时,(可使用特定语法对多个类进行代理,且前置,后置,环绕,异常分别写成方法 封装进一个类即可,且其他类对其的引用上,不变

      <!-- 创建一个装备类bean -->
    <bean id="advice" class="com.bochy.advice.MyAdvice"/>

    <aop:config >

        <aop:pointcut id="point1" expression="execution(* com.bochy.entity.*.*(..))"/> //定义切点,作用在xx类的xx方法上
        <aop:aspect id="proxy" ref="advice">
          <aop:before method="beforeMethod" pointcut-ref="point1"/>       //切点位置的作用类型(前置。。。)及作用方法。
          <aop:after-returning method="afterMethod" pointcut-ref="point1"/>
          <aop:after method="doReleaseMethod" pointcut-ref="point1"/>
          <aop:after-throwing method="throwsMethod" pointcut-ref="point1"/>
        </aop:aspect>
    </aop:config>

     

    (aop和事务使用标签多于注解,注解形式出错,自己搞不定)

  • 相关阅读:
    containerd 与安全沙箱的 Kubernetes 初体验
    dubbo-go 中的 TPS Limit 设计与实现
    MVC
    DataGridView移动上下行
    Jquery hover 事件
    MVC
    MVC 基本概念
    AJAX简单封装
    ViewState
    PostBack
  • 原文地址:https://www.cnblogs.com/qianfang123-java/p/ssh_kuangjia.html
Copyright © 2011-2022 走看看