`
shaojiashuai123456
  • 浏览: 256976 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
社区版块
存档分类
最新评论

struts2.0+hibernate3.0+spring2.0整合(2)

阅读更多

1。添加jar包 (下面有所需的jar包,可下载 )


 


2。在web。xml中添加对struts的加载和对spring的监听( 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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>ssh</display-name>
                <!-- 配置struts  -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:*.xml</param-value>
	</context-param>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

3.配置hibernate文件(hibernate.cfg.xml)

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
<hibernate-configuration>  
    <session-factory>  
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>  
        <property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>  
        <property name="hibernate.connection.url">jdbc:microsoft:sqlserver://localhost:1433;databaseName=good</property>  
        <property name="hibernate.connection.username">sa</property>  
        <property name="hibernate.connection.password">a7788250</property>  
        <property name="hibernate.hbm2ddl.auto">update</property>  
        <property name="hibernate.show_sql">false</property>  
        <property name="hibernate.format_sql">false</property>  
        <property name="hibernate.current_session_context_class">thread</property>         
  
        <!-- 最大连接数 -->  
        <property name="hibernate.c3p0.max_size">20</property>  
        <!-- 最小连接数 -->  
        <property name="hibernate.c3p0.min_size">5</property>  
        <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->  
        <property name="hibernate.c3p0.timeout">120</property>  
        <!-- 最大的PreparedStatement的数量 -->  
        <property name="hibernate.c3p0.max_statements">100</property>  
        <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->  
        <property name="hibernate.c3p0.idle_test_period">120</property>  
        <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->  
        <property name="hibernate.c3p0.acquire_increment">2</property>  
        <!-- 每次都验证连接是否可用 -->  
        <property name="hibernate.c3p0.validate">true</property>
        <mapping resource="com/zx/test/model/User.hbm.xml" />
    </session-factory>  
</hibernate-configuration>  

 
4.配置spring需要的配置文件(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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	<!-- 配置sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	<!-- 配置dao层 -->
	<bean id="userDao" class="com.zx.test.dao.UserDao">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
    <!-- 配置service层 --> 
	<bean id="userService" class="com.zx.test.service.UserService">
		<property name="userDao" ref="userDao"></property>
	</bean>
	<!-- 配置事务管理器 --> 
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	<!-- 手动配置事务代理 --> 
	<bean id="userProxy"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="proxyTargetClass">
			<value>true</value>
		</property>
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
		<property name="target">
			<ref local="userService" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	<!-- 配置action层 --> 
	<bean id="userAction" class="com.zx.test.action.UserAction" scope="prototype">
		<property name="userService" ref="userProxy"></property>
	</bean>

</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>
  <include file="struts-default.xml"/>
  <package name="user" extends="struts-default" namespace="/user">
    <action name="userAdd" class="userAction" method="userAdd">
       <result name="success">/userShow.jsp</result>
       <result name="input">/index.jsp</result> 
    </action>
  </package>
</struts>






 

  • lib.rar (8.8 MB)
  • 下载次数: 102
  • 大小: 6.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics