ssh整合

本人apem,在公司看了4天ssh,再白搭一天终于把ssh的配置配好了。

整合ssh的前提是所需jar包一个不能少,不然就会报错报到你哭,我今天80%的工作都是在测试少了哪些jar包,

其次ssh有很多个版本,不同的版本配置文件是不一样的,所以有必要说一下我的ssh配置的版本

spring3.2.1

struts2.2

hibernate3.x

ssh配置文件有至少需要3个

1、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">
    
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- spring web.xml配置 -->
  <!-- 配置全局参数,spring框架自动读取这些路径的配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/com/base/config/applicationContext.xml</param-value>
  </context-param>
 
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- struts2 -->
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
      <init-param>
          <param-name>actionPackages</param-name>
          <param-value>action</param-value>
      </init-param>
      <init-param>
          <param-name>config</param-name>
          <param-value>struts-default.xml,struts-plugin.xml,/com/base/config/struts.xml</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>
  
  <!-- hibernate配置 -->
  <filter>
      <filter-name>hibernateFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>hibernateFilter</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>

2、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
              http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
           
    <context:component-scan base-package="com.attendance.dao"></context:component-scan>
    <context:component-scan base-package="com.attendance.service"></context:component-scan>
    <context:component-scan base-package="com.base.dao"></context:component-scan>
    <context:component-scan base-package="com.base.service"></context:component-scan>
    

     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">  

        <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433; DatabaseName=DBQihang" />
        <property name="user" value="sa" />
        <property name="password" value="123456" />
        <!-- c3p0的配置 -->
        
        <!--连接池中保留的最小连接数。 -->
        <property name="minPoolSize">
            <value>5</value>
        </property>
    
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">
            <value>30</value>
        </property>
    
        <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize">
            <value>10</value>
        </property>
    
        <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime">
            <value>60</value>
        </property>
    
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement">
            <value>5</value>
        </property>
    
        <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
            如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
        <property name="maxStatements">
            <value>0</value>
        </property>
    
        <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod">
            <value>60</value>
        </property>
    
        <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts">
            <value>30</value>
        </property>
    
        <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 
            获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
        <property name="breakAfterAcquireFailure">
            <value>true</value>
        </property>
    
        <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 
            等方法来提升连接测试的性能。Default: false -->
        <property name="testConnectionOnCheckout">
            <value>false</value>
        </property>
        
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop> 
            <prop key="hibernate.show_sql">true</prop> </props> </property>
    </bean>

        
           

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="del*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
         <tx:method name="process*" propagation="REQUIRED" />
        <tx:method name="*" read-only="true" />
       </tx:attributes>
    </tx:advice>
               
       <tx:annotation-driven transaction-manager="transactionManager"/>

     <!-- 那些类的哪些方法参与事务 -->
     <!-- proxy-target-class="true"必要,不然会报错,而且需要导入 cglib.jar  -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="allManagerMethod" expression="execution(* com.*.action.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    </aop:config> 
</beans>

3、struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 
    <include file="struts-default.xml" />

    <include file="struts-plugin.xml" /> -->
    <!-- 指定Web应用的默认编码集,相当于调用 HttpServletRequest的setCharacterEncoding方法 -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <!-- 该 属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即 所有匹配*.action的请求都由Struts 2处理。如 
        果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开 -->
    <!-- <constant name="struts.action.extension" value="do" /> -->
    <!-- 设 置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好 关闭 -->
    <constant name="struts.serve.static.browserCache " value="false" />
    <!-- 当 struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生 产环境下使用),开发阶段最好打开 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 开 发模式下使用,这样可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="true" />
    <!-- 默 认的视图主题 -->
    <constant name="struts.ui.theme" value="simple" />
    <!-- 该 属性指定
    Struts 2中的action由Spring容器创 建 -->
    <constant name="struts.objectFactory" value="spring" /> 
    
    <package name="default" extends="struts-default" >
        
        <global-results>
            <result name="exception">/common/error.jsp</result>
            <result name="error">/common/error.jsp</result>
            <result name="success">/common/success.jsp</result>
        </global-results>
        
        <!-- 进入某个页面 -->
        <action name="entry_*_*">
            <result>/WEB-INF/page/{1}/{2}.jsp</result>
        </action>
        
        <!--     通配符匹配,匹配所有url对action的访问 -->
        <action name="*_*_*" class="com.{1}.action.{2}Action" method="{3}">
            <!-- 跳转到其他页面 -->
            <result name="view" type="dispatcher">/WEB-INF/{1}/${viewpath}</result>
            
            <!-- 重定向 -->
            <result name="redirect" type="redirect">/{1}_${viewpath}</result>
            <!-- 
            列表页面 -->
            <result name="list" type="dispatcher">/WEB-INF/page/{1}/list.jsp</result>
        
            <!-- 查看页面 -->
            <result name="detail" type="dispatcher">/WEB-INF/page/{1}/detail.jsp</result>
            
            <!-- 下载用的配置文件 -->
            <result name="download" type="stream">
                <param name="contentDisposition">attachment;filename=${attachmentName}</param>
                <param name="inputName">download</param>
            </result>
        </action> 
    </package>
</struts>

4.dao层的实现

package com.base.dao;

import javax.annotation.PostConstruct;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;


/**
 * dao基础类,基于spring的HibernateDaoSupport
 * @author apem
 *
 */

@Repository
public class BaseDao extends HibernateDaoSupport{
    
     /**
      * 注入sessionFactory
      * @Qualifier里的参数,是applicationContext.xml sessionFacotry的id
   * 注意,如果这里注入sessionFactory就会报错
*/ @Autowired @Qualifier("sessionFactory") protected SessionFactory sessionFactory; /*****bean实例化时执行该方法*******/ @PostConstruct protected void injectSessionFactory(){ super.setSessionFactory(sessionFactory); } }

5、Service层

package com.base.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.dao.BaseDao;

/**
 * 所有service的基类
 * @author apem
 *
 */
@Service
public class BaseService {
    
    @Autowired
    BaseDao baseDao;
}

好了,今天就写这么多,如果有不懂得可以回帖,如果在这方面有兴趣可以加我qq499451774,非诚勿扰

原文地址:https://www.cnblogs.com/apem/p/3494915.html