记录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>
相关推荐
### MyEclips集成SSH环境搭建详解 #### 一、为什么要进行SSH整合? 在软件开发领域,特别是Java Web开发中,Struts2、Hibernate、Spring(简称SSH)这三大框架各自独立时,都能完成一定的任务。然而,在实际项目中...
下面将详细讲解SSH框架的搭建和配置。 1. **Struts框架**: Struts是MVC(Model-View-Controller)设计模式的实现,主要用于控制应用程序的流程。在Struts框架中,`struts-config.xml`文件是核心配置文件,用于...
手动配置SSH(Structs+Spring+Hibernate)环境是一项在Java开发中常见的任务,尤其是在学习或深入理解框架原理时。...以上步骤仅为搭建SSH环境的基本流程,实际开发中可能还需要根据具体需求进行更多的配置和调整。
- **交换应用**:介绍了在交换机环境下配置SSH的具体步骤。 - **密码认证**:详细说明了使用密码进行身份验证的过程。 - **公钥认证**:提供了使用公钥进行认证的方法。 #### 六、SFTP服务配置 除了SSH外,文档还...
`ssh_packages_part1`可能包含了SSH相关的软件包或配置文件,可能是用于简化SSH环境搭建的一部分。这部分可能包含SSH客户端、服务器软件或者相关工具,帮助快速构建SSH连接环境。 总之,通过SSH和DB2的结合,开发者...
整合SSH框架需要进行一些配置工作,例如在Spring的配置文件中声明Hibernate的SessionFactory和事务管理器,以及配置Struts与Spring的整合,让Struts的Action能够使用Spring管理的DAO(数据访问对象)和Service(服务...
在这个SSH环境搭建的Demo中,我们将深入理解这三个框架的基本概念、工作原理以及如何将它们整合到一起。 首先,Spring作为核心容器,负责管理应用程序的对象(Bean)和依赖注入(Dependency Injection,简称DI)。...
本教程将详细介绍如何在Eclipse环境中搭建一个SSH新闻发布系统。 首先,我们需要了解SSH框架的各自作用: 1. **Struts2**:作为MVC(Model-View-Controller)框架,负责处理HTTP请求,控制应用流程,以及视图和模型...
1. **环境准备**:确保JDK、Tomcat服务器、Maven或Ant构建工具等已安装配置好。 2. **添加依赖**:在项目中引入Struts2、Spring和Hibernate的相应jar包。 3. **配置web.xml**:配置Struts2的Filter,以及Spring的...
**SSH框架环境搭建步骤**: 1. **安装JDK**:首先确保系统已安装Java开发工具,因为SSH框架都基于Java运行。 2. **设置开发环境**:推荐使用Eclipse或IntelliJ IDEA等集成开发环境,便于管理和配置项目。 3. **创建...
以下是对SSH整合环境配置的详细说明: 1. **Spring配置**: - `ContextLoaderListener` 是一个监听器,用于在Web应用启动时加载Spring的IoC(Inversion of Control)容器。它会查找`contextConfigLocation`指定的...
在这个"SSH环境.rar"压缩包中,我们很可能找到了与SSH框架配置相关的XML文件,这通常包括Spring的beans.xml,Struts2的struts.xml以及Hibernate的hibernate.cfg.xml。 Spring框架是核心的依赖注入(DI)和面向切面...
这些配置通常通过特定命令完成,例如使用`ip ssh logging`命令开启SSH日志记录;使用`ip ssh max-sessions`命令设置最大并发SSH会话数量。 ##### 2.7 配置客户端用户的RSA公钥 客户端用户的RSA公钥配置也是SSH连接...
- **目的**:本教程旨在为开发者提供一个清晰的SSH框架集成指南,帮助他们快速搭建基于这三个组件的应用。 - **范围**:教程覆盖了从环境准备到项目构建的全过程,适合对Java EE有一定基础的学习者。 - **说明**...
本实例将详述如何手动搭建一个基于SSH的Web应用程序。SSH框架提供了模型-视图-控制器(MVC)架构,使得开发过程更加模块化,提高了代码的可维护性和复用性。 一、Struts2:这是一个强大的MVC框架,负责处理HTTP请求...
在IT行业中,SSH通常指的是Spring、Struts和Hibernate这三个开源框架的组合,它们是Java Web开发中的重要组件,尤其对于初学者来说,掌握SSH框架的搭建是迈入企业级开发的关键一步。以下将详细介绍SSH框架及其搭建...
在这个"ssh框架搭建源码"的资源中,你将找到关于如何配置和运行SSH框架的详细步骤。 Spring框架作为核心,它负责管理应用程序的业务对象和数据访问对象,提供了依赖注入(DI)和面向切面编程(AOP)的功能,使得...
5. **配置SSH**:接下来,需要修改SSH配置文件(通常位于`/etc/ssh/sshd_config`)以限制用户登录到特定的shell。例如,可以将`PermitRootLogin`设为`no`,并为受限用户设置`ForceCommand`选项,使其登录时直接进入...
Spring的`@Profile`注解还可以用于环境特定的配置,让应用程序在不同环境下具有可配置性。 接着,Struts2框架也支持注解,例如`@Action`、`@Result`和`@InterceptorRef`等,这些注解可以直接在Action类或方法上使用...