struts spring hibernate整合
2012-03-31 14:28
344 查看
最近等着项目启动,没事情干,把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>
多个配置文件应该是中间加空格就可以了,具体的我没试过,找机会试一下。
bean.xml 这里面把hibernate的sessionFactory也配置进去了,还加了事务的,注意这里用的是注解的方式annotation,比较方便
struts.xml 需要注意的地方:<action name="login" class="userAction" />这里面的class不是类的路径,而是spring中配置的bean的id名
好的,剩下的就是jsp和java的类了。这两部分就先不贴图了。
好了,这样就整合好了,谢谢大家!!欢迎大家进来讨论
项目所需要的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的类了。这两部分就先不贴图了。
好了,这样就整合好了,谢谢大家!!欢迎大家进来讨论
相关文章推荐
- struts整合spring整合hibernate
- 轻量级Java EE企业应用实战(第3版)——Struts 2+Spring 3+Hibernate整合开发
- [转]Struts+Spring+Hibernate整合笔记
- Struts1.2/2.1、spring、hibernate整合笔记
- Struts 1.3、Hibernate 3、Spring 2.5、Jpa整合所需jar包
- Struts,hibernate,Spring整合开发各层之间联系的配置说明(初学者值得看下)
- 最新版的SSH框整合(Spring 3.1.1 + Struts 2.3.1.2 + BoneCP + Hibernate 4.1)更新增加C3P0和EhCache配置
- struts-2.3.1+spring-framework-3.2.2+hibernate-4.1.1整合历程<第一部分>(详解)
- struts-2.3.1+spring-framework-3.2.2+hibernate-4.1.1整合历程<第一部分>(详解)
- Struts,Hibernate,Spring的整合
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
- SSH(Spring3.0 + Struts2.1 + Hibernate3.3)整合示例
- Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0 Annotations注解框架整合及其代码分享
- Struts+Spring+Hibernate--SSH整合实例
- spring+struts+hibernate 的整合,采用proxool连接池
- MyEclipse整合SSH(Struts+Spring+Hibernate)简单登录范例(二)
- Spring+Hibernate+struts整合开发模板
- struts,hibernate,spring整合
- ssh框架整合 hibernate5.2.8_spring4.3.7_struts2.5.1
- struts-2.3.1+spring-framework-3.2.2+hibernate-4.1.1整合历程<第二部分>(详解)