• SSH框架搭建笔记


    1、建立一个web项目,设置编码格式,建立src下的包,建立资源文件夹
    2、加入Spring运行必须的jar包(5个jar包)
        spring-beans-4.1.4.RELEASE.jar
        spring-context-4.1.4.RELEASE.jar
        spring-core-4.1.4.RELEASE.jar
        spring-expression-4.1.4.RELEASE.jar
        commons-logging-1.2.jar
    3、建立Spring的配置文件
        3.1 applicationContext.xml
        3.2 在spring参考文档中找到3.1中需要的配置头信息
    
    4、加入hibernate需要的必须包(required 10个)+jdbc的jar包
         4.1 在spring参考文档中的15.3.1(搜Hibernate)查找配置 
             4.1.1 使用的是dbcp连接池配置 导入dbcp需要的2个jar包:①commons-dbcp.jar  ②commons-pool.jar
             4.1.2 如果使用c3p0连接池,需要导入3个jar包:①c3p0-0.9.5.jar  ②c3p0-oracle-thin-extras-0.9.5.jar  ③mchange-commons-java-0.2.9.jar
        4.2 修改连接池的配置信息(driver,url,username,password)
        4.3 书写javabean的orm映射文件(xxx.hbm.xml) 在hibernate官方文档中查找1.1.3 中的xml配置
        4.4 配置hibernate中的sessionfactory  
            4.4.1 将orm的映射文件写入Spring配置文件中的集合注入中
            4.4.2 配置hibernate中的property属性   hibernateProperties
            需要加入的jar包:①spring-orm-4.1.4.RELEASE.jar  ②spring-tx-4.1.4.RELEASE.jar
    对上面的配置进行测试,查看是否配置正确
    
    5、导入struts2需要的jar包(12个) 其中有一个jar是重复的(javassist-3.11.0.GA.jar),不要
        5.1导入jar包:①struts2-spring-plugin-2.3.20.jar  ②spring-web-4.1.4.RELEASE.jar
        5.2 建立struts.xml配置文件 拷贝案例中的struts.xml配置文件中的内容  将工厂交给spring进行管理
        5.3修改web.xml文件,添加struts2需要的过滤器   添加监听  上下文参数设置
    6、配置事务 需要导入的jar包(4个)
      aopalliance.jar
      aspectjweaver.jar
      spring-aop-4.1.4.RELEASE.jar
      spring-jdbc-4.1.4.RELEASE.jar
    5(spring basic)+10(hibernate required)+1(jdbc)+2(dbcp)+2(orm与tx)+11(struts2)+2(web,struts-spring-plugin)+4(事务)=37个jar包

    需要的jar包(含有json):

    web.xml中的配置:

     1 <!-- struts2需要的过滤器 -->
     2     <filter>
     3         <filter-name>struts2</filter-name>
     4         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     5     </filter>
     6     <filter-mapping>
     7         <filter-name>struts2</filter-name>
     8         <url-pattern>/*</url-pattern>
     9     </filter-mapping>
    10 
    12     <!-- 上下文参数配置 -->
    13     <context-param>
    14         <param-name>contextConfigLocation</param-name>
    15         <param-value>classpath:applicationContext.xml</param-value>
    16     </context-param>
    17     
    19     <!-- 设置spring监听器 -->
    20     <listener>
    21         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    22     </listener>

    applicationContext.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.xsd
     9         http://www.springframework.org/schema/aop
    10         http://www.springframework.org/schema/aop/spring-aop.xsd
    11         http://www.springframework.org/schema/tx
    12         http://www.springframework.org/schema/tx/spring-tx.xsd">
    13     <!-- 包含其他的配置文件 -->
    14     <!-- <import resource="inventory.xml"/> -->
    15 
    16     <!-- 事务管理 -->
    17     <bean id="myTxManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    18         <property name="sessionFactory" ref="mySessionFactory"/>
    19     </bean>
    20     <!-- 设置切入点,以及要使用的事务管理实例 -->
    21     <aop:config>
    22         <aop:pointcut id="productServiceMethods"  expression="execution(* com.ssh.service..*.*(..))"/>
    23         <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
    24     </aop:config>
    25     <tx:advice id="txAdvice" transaction-manager="myTxManager">
    26         <tx:attributes>
    27             <tx:method name="add*" propagation="REQUIRED"/>
    28             <tx:method name="del*" propagation="REQUIRED"/>
    29             <tx:method name="upd*" propagation="REQUIRED"/>
    30             <tx:method name="get*" propagation="REQUIRED"/>
    31         </tx:attributes>
    32     </tx:advice>
    33     <!-- dbcp连接池配置 -->
    34     <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    35         <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
    36         <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=inventory1"/>
    37         <property name="username" value="sa"/>
    38         <property name="password" value="123456"/>
    39     </bean>
    40     <!-- hibernate相关参数配置:session工厂,映射文件等 -->
    41     <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    42         <property name="dataSource" ref="myDataSource"/>
    43         <!-- 采用通配的方式配置实体类的hbm文件位置 -->
    44         <property name="mappingLocations" value="classpath:com/ssh/entity/*.hbm.xml" />
    45         <!-- 
    46         <property name="mappingLocations">
    47             <list>
    48                 <value>classpath:com/ssh/entity1/*.hbm.xml</value>
    49                 <value>classpath:com/ssh/entity2/*.hbm.xml</value>
    50                 <value>classpath:com/dssh/entity3/*.hbm.xml</value>
    51             </list>
    52         </property> 
    53         -->
    54         <property name="hibernateProperties">
    55             <props>
    56                 <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2005Dialect</prop>
    57                 <prop key="hibernate.show_sql">false</prop>
    58                 <prop key="hibernate.format_sql">true</prop>
    59             </props>
    60         </property>
    61     </bean>
    62 </beans>

    struts.xml文件配置:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7     <!-- 开启DMI动态调用 -->
     8     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
     9     <constant name="struts.devMode" value="true" />
    10     
    11     <!-- 将工厂交给spring进行管理 -->
    12     <constant name="struts.objectFactory" value="spring"/>
    13     
    14     <package name="default" namespace="/" extends="struts-default">
    15         
    16     </package>
    17 </struts>
  • 相关阅读:
    [转]c#的DateTime.Now函数详解
    PHP学习笔记
    【错误】MsDepSvc.exe 站用了80端口/IIS的0×8ffe2740错误解决方
    IIS连接数
    Mybatis3.2.1整合Spring3.1
    linux常用命令大全
    Spring3.2新注解@ControllerAdvice
    SpringMVC强大的数据绑定(2)——第六章 注解式控制器详解
    Console命令详解,让调试js代码变得更简单
    String.format
  • 原文地址:https://www.cnblogs.com/TheoryDance/p/4725723.html
Copyright © 2020-2023  润新知