struts spring hibernate整合

最近等着项目启动,没事情干,把struts2 spring hibernate 又复习了一遍,参考资料是尚学堂-马老师的视频讲解,感觉很到位,现在把整合的步骤给大家公布出来,有问题和疑问的大家可以进来讨论。

项目所需要的jar包,一共分4部分,spring.jar,hibernate.jar,struts2.jar和其他一些辅助jar(reference.jar)

第一个jar  spring的,版本是spring-framework-3.1.0.RELEASE

第二个是hibernate.jar,版本是hibernate-distribution-3.3.2.GA-dist,这里需要说明的是hibernate现在新的版本是4,但是我在整合的时候老是找不到cacheprovide这个类,其实hibernate4照3还是有比较大的改动的,这里就不讨论了。

第三个是struts2.jar  版本是struts-2.3.1.2  没有把所有的struts2的jar都导入进来,导入的是struts2的jar包中有个例子程序struts-bank里面的jar包,除了asm都导入进来了

第四个是reference.jar  这里面主要是日志,数据库连接的包,struts-spring-plugin-2.3.1.2.jar这个包其实是struts2的包,但是这里我把它放在外面是为了管理方便,因为我用了user libraries把jar分开了,这样看着方面点。

在啰嗦几句,我是把每一个分别搭好最后再整合一起的,包括spring和hibernate2的整合。导包时要注意宁可少包不能多加包,因为加多了会出现一些莫名其妙的错误。你可以看缺少哪个在引入哪个。

几个重要的配置文件:web.xml  bean.xml(就是spring的配置文件,一般名字为applicationContext.xml) struts.xml

web.xml  要web容器把spring的配置文件都加载进去,就要使用

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

读配置文件的方法是

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

多个配置文件应该是中间加空格就可以了,具体的我没试过,找机会试一下。

<?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">


	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:bean.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!--
		or use the ContextLoaderServlet instead of the above listener
		<servlet> <servlet-name>context</servlet-name>
		<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
		<load-on-startup>1</load-on-startup> </servlet>
	-->
	<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>

bean.xml 这里面把hibernate的sessionFactory也配置进去了,还加了事务的,注意这里用的是注解的方式annotation,比较方便

<?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"

	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       	   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<!-- more bean definitions for data access objects go here -->
	
	<bean id="baseDao" class="com.mf.spring3.dao.impl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="userDao" class="com.mf.spring3.dao.impl.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="logDao" class="com.mf.spring3.dao.impl.LogDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	
	<bean id="userService" class="com.mf.spring3.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao"></property>
		<property name="logDao" ref="logDao"></property>
	</bean>
	<bean id="logService" class="com.mf.spring3.service.impl.LogServiceImpl">
		<property name="logDao" ref="logDao"></property>
	</bean>

	<bean id="userAction" class="com.mf.spring3.action.UserAction">
		<property name="userService" ref="userService"></property>
	</bean>

	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost/springhibernate" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="myDataSource" />
		<!--
		<property name="annotatedClasses">
			<list>
				<value>com.mf.spring3.model.User</value>
				<value>com.mf.spring3.model.TLog</value>
			</list>
		</property> 
		-->
		<property name="packagesToScan">
			<list>
				<value>com.mf.spring3.model</value>
			</list>
		</property> 
		
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
	      </value>
		</property>
	</bean>

	<tx:annotation-driven transaction-manager="myTxManager"/>
	<bean id="myTxManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

</beans>

struts.xml  需要注意的地方:<action name="login" class="userAction" />这里面的class不是类的路径,而是spring中配置的bean的id名

<?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.devMode" value="true" />
	<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
	
    <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>
		 -->
        <default-action-ref name="login"></default-action-ref>
        <action name="login" class="userAction">
        	<result>/login/list.jsp</result>
        	<result name="input">/login/add.jsp</result>
        </action>
        
    </package>
    <!-- Add packages here -->

</struts>

好的,剩下的就是jsp和java的类了。这两部分就先不贴图了。

好了,这样就整合好了,谢谢大家!!欢迎大家进来讨论

猜你喜欢

转载自blog.csdn.net/qwerty123501029/article/details/7415558