Struts2.2.3+spring3.0.2+hibernate3.5整合Struts2.2.3+spring3.0.2+hibernate3.5整合?web.xml??xml vers
Struts2.2.3+spring3.0.2+hibernate3.5整合
Struts2.2.3+spring3.0.2+hibernate3.5整合
?
web.xml
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
?
<display-name>testssh2</display-name>
<!-- 欢迎界面 ?-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param> ?
? ? ? ? <param-name>contextConfigLocation</param-name> ?
? ? ? ? <param-value>classpath:application-*.xml</param-value> ?
? ? </context-param> ?
<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>
?
<!-- 配置Spring的监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Spring的过滤器,解决乱码问题 -->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
?
</web-app>
**********************************************************************************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" />? ??? ? ?<!-- 在struts.xml声明,action交予spring3.x托管 --> ?? ? <constant name="struts.objectFactory" value="spring"/> ?
? ? <package name="default" namespace="/" extends="struts-default" >
? ? ? ? <default-action-ref name="index" />
? ? ? ? <global-results>? ? ? ? ? ? <result name="error">/error.jsp</result>? ? ? ? </global-results>
? ? ? ? <global-exception-mappings>? ? ? ? ? ? <exception-mapping exception="java.lang.Exception" result="error"/>? ? ? ? </global-exception-mappings>
? ? ? ? <action name="login" method="login">? ? ? ? ? ? <result name="success">/index.jsp</result>? ? ? ? </action>? ? ??? ? ? ? <action name="regist" method="regist">? ? ? ? <result name="success">/success.jsp</result>? ? ? ? </action>? ?
? ? </package>
</struts>************************************************************************************application-common.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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="propertyConfigurer"value="${jdbc.driverClassName}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean>
<!-- 配置SessionFactory --><bean id="sessionFactory"ref="dataSource" /><property name="configLocation" value="classpath:hibernate.cfg.xml" /></bean>
<!-- 事务定义 --><bean id="txManager"ref="sessionFactory" /></bean>
<!--- 配置事务的传播特性 --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="login" propagation="REQUIRED"/><tx:method name="*" read-only="true" /></tx:attributes></tx:advice>
<!-- 切面配置 (那些类的哪些方法参与事务) --><aop:config><!-- 定义在service包里的任意方法的执行 --> <aop:pointcut expression="execution(* com.testssh2.service.*.*(..))" id="servicesOperation"/> ?<aop:advisor advice-ref="txAdvice" ?pointcut-ref="servicesOperation"/></aop:config></beans>**********************************************************************************application-beans.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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">?? <!-- 定义hibernateTemplate -->?<bean id="hibernateTemplate"? ref="sessionFactory" />?</bean>??<!-- 把 hibernateTemplate注入到Dao-->?<bean id="userDao" ref="hibernateTemplate"></property>?</bean>??<bean id="userService" ref="userDao"></property>?</bean>
?<bean id="registAction" scope="prototype">?<property name="userService" ref="userService"></property>?</bean>?</beans>**********************************************************************************hibernate.cfg.xml配置文件<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration><!-- <session-factory name="foo"> <property name="show_sql">true</property>?<mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/> <class-cache?region="Simple" usage="read-write"/>?</session-factory> --><session-factory>
<!-- C3P0连接池设定 --><property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider ?</property><property name="hibernate.c3p0.max_size">20</property><property name="hibernate.c3p0.min_size">5</property><property name="hibernate.c3p0.timeout">120</property><property name="hibernate.c3p0.max_statements">100</property><property name="hibernate.c3p0.idle_test_period">120</property><property name="hibernate.c3p0.acquire_increment">2</property>
<!-- SQL dialect(方言)用于指定何种数据库,可以在Hibernate的文档中查到 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- Enable Hibernate's automatic session context management --><property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout 是否显示生成的SQL生成语句!开发时,一定要显示,方便调试! --><property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup hbm是Hibernate Maping?ddl是数据定义语句 这句话的意思是:是否要Hibernate指定映射成数据定义语句。 --><property name="hbm2ddl.auto">update</property>
<!-- 一下配置hbm.xml文件 --><mapping resource="com/testssh2/vo/User.hbm.xml" /><mapping resource="com/testssh2/vo/Log.hbm.xml" /></session-factory></hibernate-configuration>*********************************************************************************jdbc.properties文件jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8jdbc.username=rootjdbc.password=zgz**********************************************************************************log4j.properties文件### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ####log4j.appender.file=org.apache.log4j.FileAppender#log4j.appender.file.File=hibernate.log#log4j.appender.file.layout=org.apache.log4j.PatternLayout#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=infolog4j.logger.org.hibernate=debug
### log HQL query parser activity#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL#log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###log4j.logger.org.hibernate.type=info#log4j.logger.org.hibernate.type=debug
### log schema export/update ###log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees#log4j.logger.org.hibernate.hql=debug
### log cache activity ####log4j.logger.org.hibernate.cache=debug
### log transaction activity#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###### leakages when using DriverManagerConnectionProvider ####log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace