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的类了。这两部分就先不贴图了。

 

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSH整合示例(详情见我博客专栏)之前的博客我们总结了spring基础、spring分别整合struts2、hibernate、mybatis等,今天我们来同时整合strutsspringhibernate,也就是所谓的 ssh 。 整合流程: 1 首先整合springhibernate,这次我们在spring 中配置bean使用注解的方式 ,hibernate实体映射关系也使用注解的方式,配置完毕后用简单方法测试下hibernate是否整合成功。 a 加入支持:添加 spring核心包、hibernate 3.6 包、 spring整合hibernate包 , 在src下建立applicationContext.xml (先建立src下便于测试hibernate)。 b 编写实体类,加入hibernate注解,编写方法类测试类,在applicationContext.xml中添加hibernate模板类配置以及包扫描语句 。在类中添加spring bean注解。 c 测试类中 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试 2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts json包(ajax要用到),spring整合struts2包,spring web 包,在src目录下建立struts.xml,复制头文件进去。将applicationContext.xml移到WEB-INF目录下。web容器中(web.xml)中添加struts2 filter以及spring 监听器。 b 在struts.xml中添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ioc注解的方式, 所以默认的bean的id 为 类名(首字母小写) c 编写action类、页面进行测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值