SSH 三大框架整合

      以前搞一个SSH框架配置很麻烦,要不就是不合乎自己的要求,要不就是超简单的哪种,根本不容易扩展,一下是我对SSH做的一个一个比较大些的扩展,如果哪里写的不当,还请各位多多指教。

第一步,按照附件压缩文件中名为SSH2框架组合基本步骤与配置的文档,一步一步的把基本框架搭建起来

第二步,配置web.xml:

    以下是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">
 <!-- proxool 配置 start  -->
 <servlet>
    <servlet-name>servletConfigurator</servlet-name>
    <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
    <init-param>
     <param-name>xmlFile</param-name>
     <param-value>WEB-INF/conf/proxool.xml</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <!-- proxool 配置 end -->
  
  
  <!-- spring 配置 start-->
 <!-- 用servlet代替listener来启动spring,因为用了连接池proxool,
 而proxool加载时,要比spring更提前,如果用监听器则没法控制其顺序,所以才采用servlet -->
 <servlet>
    <servlet-name>contextLoaderServlet</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>WEB-INF/conf/applicationContext.xml</param-value>
 </context-param>
<!-- spring 配置 end -->

 


<!-- proxool 销毁问题 start-->
<servlet>
   <servlet-name>proxoolDestroyServlet</servlet-name>
   <servlet-class>com.helen.web.servlet.ProxoolDestroyServlet</servlet-class>
   <load-on-startup>2</load-on-startup>
  </servlet> 
<!-- proxool 销毁问题 end-->

<!-- 过滤器 编码方式(采用spring) start  -->
  <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
</filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
<!-- 过滤器 编码方式 end  -->
 <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>
 
 
   <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
</web-app>






   第三步: 配置数据库链接文件proxool.xml

  
<?xml version="1.0" encoding="gbk"?>

<!-- the proxool configuration can be embedded within your own application's.

Anything outside the "proxool" tag is ignored. -->

<something-else-entirely>
  <proxool>
    <alias>proxool</alias>
    <!-- oracle 配置 -->
     
    <driver-url>jdbc:oracle:thin:@127.0.0.1:1521:ORCL</driver-url>
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
   
   <!-- ms sql 配置

    <driver-url>jdbc:sqlserver://127.0.0.1:1433;databaseName=jdp_template</driver-url>
    <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
-->
      
    <!-- mysql 配置 -->
    <!--
    <driver-url>jdbc:mysql://localhost:3306/jdp_template?useUnicode=true&amp;characterEncoding=utf8</driver-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
     -->
     
     
    <driver-properties>
    <property name="user" value="Helen"/>
    <property name="password" value="admin"/>
    </driver-properties>
   
   <!--proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 默认30秒-->     
   <house-keeping-sleep-time>90000</house-keeping-sleep-time>

   <!--没有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->     
   <maximum-new-connections>20</maximum-new-connections>
   
   <!--最大连接数(默认15个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定 -->     
   <maximum-connection-count>50</maximum-connection-count> 
   
   <!--最小连接数(默认5个)-->     
   <minimum-connection-count>10</minimum-connection-count> 

   <!--最少保持的空闲连接数-->   
   <prototype-count>5</prototype-count>    
   
   <!-- 如果housekeeper 检测到某个线程的活动时间大于这个数值.它将会杀掉这个线程.确认一下你的服务器的带宽.然后定一个合适的值.默认是5分钟-->
   <maximum-active-time>300000</maximum-active-time>
   
   <!-- 最大连接生命周期 默认值:4小时 -->
   <maximum-connection-lifetime>14400000</maximum-connection-lifetime>
   
    <!--在使用之前测试-->  
   <test-before-use>true</test-before-use>  
   
   <!-- 如果发现了空闲的数据库连接.house keeper 将会用这个语句来测试.这个语句最好非常快的被执行 -->     
  <!-- oracle 配置-->
     
   <house-keeping-test-sql>select sysdate from dual</house-keeping-test-sql> 
      
 <!-- ms sql 配置/my sql配置
 
    <house-keeping-test-sql>select 1</house-keeping-test-sql> 
  -->
  </proxool>
</something-else-entirely>








第四步,配置Sping的配置文件applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                    http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config/>
 <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
 <context:component-scan base-package="com.helen.domain" />
 
<import resource="applicationContext-mvc.xml"/>
<import resource="application-context.xml"/>
<import resource="applicationContext-config.xml"/>

</beans>


   

第五步,配置其他副配置文件application-context.xml,applicationContext-mvc.xml,applicationContext-config.xml



applicationContext-config.xml:





<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                    http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
               ">

<!-- 数据源  -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="url">
   <!-- 格式为:proxool.数据源别名,要跟proxool.xml内节点alias一致 -->
   <value>proxool.proxool</value>
  </property>
 </bean>

<bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 
 <property name="dataSource" ref="dataSource"></property>
 
   <property name="hibernateProperties">
   <props>
    <!-- hibernate方言 oracle-->
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
   <!-- hibernate方言 mysql -->
  <!--
  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
   -->
   <!-- hibernate方言 ms sql2000或ms sql2005 -->
   <!--  
   <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
 -->
    
    <!-- 显示sql语句 -->
    <prop key="hibernate.show_sql">true</prop>

 <prop key="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</prop>
 
    <prop key="hibernate.connection.autocommit">true</prop>
    <!-- 指定Hibernate在何时释放JDBC连接
    对于应用程序服务器的JTA数据源, 你应当使用after_statement, 这样在每次JDBC调用后,都会主动的释放连接. 
    对于非JTA的连接, 使用after_transaction在每个事务结束时释放连接是合理的. 
    auto将为JTA和CMT事务策略选择after_statement, 为JDBC事务策略选择after_transaction. 
    取值 on_close | after_transaction | after_statement | auto
     -->
    <prop key="hibernate.connection.release_mode">auto</prop>
    
    <!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 --> 
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
   
    <!-- ehcache配置文件路径 -->
    <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
   
    <!-- 二级缓存 -->
    <prop key="hibernate.cache.use_second_level_cache">true</prop> 
     
    <!-- 使用查询缓存 -->
    <prop key="hibernate.cache.use_query_cache">true</prop>

    <prop key="hibernate.jdbc.batch_size">50</prop>
    
 <prop key="hibernate.jdbc.fetch_size">50</prop>
 
 <prop key="hibernate.current_session_context_class">thread</prop>
 
   </props>
  </property>

<!-- 实体类之注解 -->
 <property name="packagesToScan">
 <list>
  <value>com.helen.domain</value>
 </list>
 </property>
</bean>

 

<bean id="jdbcTemplate"
  class="org.springframework.jdbc.core.JdbcTemplate">
 <property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
 <!-- 事务通知 --> 
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <!-- 方法名以add*开头 -->
   <tx:method name="add*" propagation="REQUIRED"
    rollback-for="Exception" />
   <!-- 方法名以delete*开头 -->
   <tx:method name="delete*" propagation="REQUIRED"
    rollback-for="Exception" />
   <!-- 方法名以update*开头 -->
   <tx:method name="update*" propagation="REQUIRED"
    rollback-for="Exception" />
   <!-- 方法名以get*开头 -->
   <tx:method name="get*" propagation="SUPPORTS"
    read-only="true" />
   <!-- 其它方法 -->
   <tx:method name="*" propagation="SUPPORTS" read-only="true" />
  </tx:attributes>
 </tx:advice>

<aop:config>
  <!-- 定义一个切面 -->
  <aop:pointcut id="bizMethods"
   expression="execution(* com.jdp.pro.*.biz..*.*(..))" />
  <!-- 织入 -->
  <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
</aop:config>
</beans>






application-context.xml:

<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                    http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <!-- 
   <bean id="personService" class="com.helen.web.service.impl.PersonServiceImp" />  
    <bean id="loginAction" class="com.helen.web.action.LoginAction">
        <property name="personService">  
            <ref bean="personService"/>
        </property>  
    </bean>  
    -->
</beans>  




  第六步,配置struts.xml





<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker 、velocity的输出 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
    <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" />
    <!--与spring集成时,指定由spring负责action对象的创建 -->
    <constant name="struts.objectFactory" value="spring" />
  <!-- 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。 -->
 <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
 
 <package name="login" extends="struts-default">
  
 </package>
 
</struts>






第七步,配置日志文件log4j.properties

#set log levels#
log4j.rootLogger=debug,ConsoleSet,DailyRollingFileSet,DailyRollingFileSet4Error


#set Console #
log4j.appender.ConsoleSet=org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleSet.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleSet.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] [%l] - %m%n

#set writer log for file where levels in debug,warn,info #
log4j.appender.DailyRollingFileSet = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyRollingFileSet.File=log/daily/log
log4j.appender.DailyRollingFileSet.DatePattern='.'yyyy-MM-dd
log4j.appender.DailyRollingFileSet.Append=true
log4j.appender.DailyRollingFileSet.Threshold = info
log4j.appender.DailyRollingFileSet.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyRollingFileSet.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] [%l] - %m%n

#set writer log for file where levels is error
log4j.appender.DailyRollingFileSet4Error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyRollingFileSet4Error.File=log/error/log
log4j.appender.DailyRollingFileSet4Error.Append = true
log4j.appender.DailyRollingFileSet4Error.Threshold = ERROR
log4j.appender.DailyRollingFileSet4Error.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyRollingFileSet4Error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] [%l] - %m%n






第八步,配置缓存ehcache.xml

 <?xml version="1.0" encoding="UTF-8"?>
<ehcache>

 <diskStore path="java.io.tmpdir/Helen_SSH/" />

 <!-- DefaultCache setting. -->
 <defaultCache maxElementsInMemory="10000" memoryStoreEvictionPolicy="LRU" eternal="false"
  timeToIdleSeconds="300" timeToLiveSeconds="300" overflowToDisk="false" diskPersistent="false" />

 <!-- Special objects setting. -->
 <!-- Refresh ContentInfoCache every hour. -->
 <cache name="contentInfoCache" overflowToDisk="false" eternal="false" diskPersistent="false" timeToLiveSeconds="3600" timeToIdleSeconds="3600" maxElementsInMemory="10000" memoryStoreEvictionPolicy="LRU"/>
</ehcache>




第九步,在com.helen.web.servlet下面添加一个ProxoolDestroyServlet.java文件

package com.helen.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.logicalcobwebs.proxool.ProxoolFacade;

public class ProxoolDestroyServlet extends HttpServlet {

 /**
  * 
  */
 private static final long serialVersionUID = -576763880863730788L;

 /**
  * Destruction of the servlet.
  */
 @Override
 public void destroy() {
   ProxoolFacade.shutdown();
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 @Override
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  super.doGet(request, response);
 }

 @Override
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  super.doPost(request, response);
 }

 @Override
 public void init() throws ServletException {
  // Put your code here
 }

}




第十步,把附件中所有的jar包导进去



由于jar文件包太大,所以如果有谁想要的话,给我发邮件:[email protected]

现在运行以下您的服务,看看如何了,希望能跟各位做个朋友!


猜你喜欢

转载自jeenry119.iteye.com/blog/1198687