天天看点

ssh环境搭建记录

记录struts2 spring hibernate环境的搭建。

jar包链接http://download.csdn.net/detail/xgcai/6474223 

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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>BaseSSHDemo</display-name>

	<!-- 定义spring配置文件的位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>

	<!-- 定义log4j配置文件的位置 -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>

	<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond -->
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>

	<!--web容器启动,自动加载ApplicationContext信息 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- web容器启动,加载log4j-->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	
	<!-- spring支持request scope -->
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	
	<!-- 设置编码为UTF-8 -->
	<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>

	<!-- struts2 过滤器 -->
	<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.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>
</web-app>
           

2、log4j.properties

log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${baseWebApp.root}/WEB-INF/baseWebApp.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
           

3、定义数据源属性和hibernate属性initMySQL.properties

#################################################
#####     datasource setting             ########
#################################################

datasource.type=mysql
datasource.driverClassName=org.gjt.mm.mysql.Driver
datasource.url=jdbc:mysql://localhost:3306/sshDemo?useUnicode=true&characterEncoding=UTF-8
datasource.username=root
datasource.password=root
datasource.systemUserId=2
datasource.userOnlineTimeOut=3600
datasource.backListLoginFailedTimeOut=300

datasource.maxActive=10
datasource.maxIdle=2
datasource.maxwait=120000
#datasource.whenExhaustedAction=1
#datasource.validationQuery=select 1 from dual
#datasource.testOnBorrow=true
#datasource.testOnReturn=false

datasource.minPoolSize=10
datasource.maxPoolSize=20
datasource.maxStatements=0
datasource.numHelperThreads=10
datasource.maxIdleTime=1800
datasource.acquireIncrement=3
datasource.initialPoolSize=3
datasource.idleConnectionTestPeriod=900
datasource.acquireRetryAttempts=30
datasource.breakAfterAcquireFailure=true
datasource.testConnectionOnCheckout=false

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
connection.characterEncoding=UTF-8
hibernate.jdbc.batch_size=25
hibernate.fetch_size=50
hibernate.show_sql=true
hibernate.show_sql_format= true
hibernate.connection.release_mode=after_transaction
hibernate.generate_statistics=true
hibernate.connection_release_mode=auto
hibernate.autoReconnect=true
hibernate.cglib.use_reflection_optimizer=true

remind.removetime=2
hibernate.cache.use_second_level_cache true
hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider

           

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

	<!-- DataSource Definition, using c3p0 connection pool -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>classpath:initMySQL.properties</value>
		</property>
	</bean>

	<!-- 定义数据源c3p0 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${datasource.driverClassName}" />
		<property name="jdbcUrl" value="${datasource.url}" />
		<property name="user" value="${datasource.username}" />
		<property name="password" value="${datasource.password}" />
		<property name="minPoolSize" value="${datasource.minPoolSize}" />
		<property name="maxPoolSize" value="${datasource.maxPoolSize}" />
		<property name="maxIdleTime" value="${datasource.maxIdleTime}" />
		<property name="acquireIncrement" value="${datasource.acquireIncrement}" />
		<property name="maxStatements" value="${datasource.maxStatements}" />
		<property name="initialPoolSize" value="${datasource.initialPoolSize}" />
		<property name="idleConnectionTestPeriod" value="${datasource.idleConnectionTestPeriod}" />
		<property name="acquireRetryAttempts" value="${datasource.acquireRetryAttempts}" />
		<property name="breakAfterAcquireFailure" value="${datasource.breakAfterAcquireFailure}" />
		<property name="testConnectionOnCheckout" value="${datasource.testConnectionOnCheckout}" />
	</bean>

	<!-- 会话工厂 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>

		<!-- 映射文件位置 1、mappingResources 2、mappingLocations 3、mappingDirectoryLocations 
		<property name="mappingLocations"> 
			<list> 
				<value>classpath:/org/rico/business/entity/hbm/*.hbm.xml</value> 
			</list> 
		</property>
		 
		<property name="packagesToScan">
			<list>
				<value>org.rico.business.entity</value>
			</list>
		</property>-->

		<!-- hibernate属性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="connection.characterEncoding">${connection.characterEncoding}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.show_sql_format}</prop>
				<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
				<prop key="hibernate.connection.release_mode">${hibernate.connection_release_mode}</prop>
				<prop key="hibernate.autoReconnect">${hibernate.autoReconnect}</prop>
				<prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop>
				<!-- <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop> 
					<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> 
					<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> 
					<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_second_level_cache}</prop> -->
			</props>
		</property>
	</bean>

	<!-- =========== openSessionInViewInterceptor 延迟加载问题============== -->
	<bean id="openSessionInViewInterceptor"
		class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
		<property name="sessionFactory" ref="sessionFactory" />
		<!--如果使用延迟关闭方式 <property name="singleSession"> <value>false</value> </property> -->
	</bean>

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

	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
		<property name="allowCreate">
			<value>false</value>
		</property>
	</bean>

	<!-- 利用拦截器来管理事务 ,所有的bean共享一个代理 -->
	<bean id="transactionProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		lazy-init="true" abstract="true">
		<!-- 配置事务管理器 -->
		<property name="transactionManager" ref="transactionManager" />

		<!-- 配置事务属性 -->
		<property name="transactionAttributes">
			<props>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>

	<!-- 配置事务 -->

	<!-- AOP和TX配置 -->
	<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->

	<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
		<tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> 
		</tx:advice> <aop:config> <!- - 切入点指明了在执行Service的所有方法时产生事务拦截操作 - -> <aop:pointcut 
		id="allMethod" expression="execution(* org.rico.***.*(..))" /> <!- - 定义了将采用何种拦截操作,这里引用到 
		txAdvice - -> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/> 
		</aop:config> -->

	<!-- 定义logic <bean id="logic" parent="transactionProxyFactoryBean"> <property 
		name="target"> <bean class="org.rico.business.logic.*Impl"></bean> </property> 
		</bean> <import resource="appContext-organizes.xml"/> -->
</beans>
           

 5、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>

	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="false" />

	<!-- <include file="example.xml"/> -->

	<package name="default" namespace="/" extends="struts-default">
		<default-action-ref name="index" />
		<action name="index">
			<result type="redirectAction">
				<param name="actionName">HelloWorld</param>
				<param name="namespace">/example</param>
			</result>
		</action>
		
		<action name="test" class="testAction">
			<result name="success">/index.jsp</result>
		</action>
	</package>

	<!-- Add packages here -->

</struts>