三大框架整合步骤(详细)

以是从struts2-------->>>hibernate--------------->>>>spring的整合过程。

环境:tomcat7+MyEclipse10.0+struts2+hibernate3+spring

说明:

1)、基本全部是使用注解方式。

2)、适合初学者

3)、文章最后有项目的源码

------------------struts2-------------------------
步骤:
1、导入必须框架jar()
2、jar addtoBuildPath
3、 建立对应各个层包
4、建立bean(entry)-----加上hibernate需要的对应的注解
5、web.xml加入struts需要的拦截(只针对struts的配置)
在action中实现ModelDriven 接口,可以快速从表单装入对应实体属性
6、新建struts.xml,设置好对应跳转关系

7、根据struts建立对应jsp和action


注意事项:
1、需要去掉整合spring需要的struts2-spring-plugin-2.1.6.jar,不然运行只有struts配置时,会报错。
2、注意路径问题。在struts.xml中的package name="user" 是跳转的“相对值”,所以,如果设置jsp的所在的文件夹(如果设置文件夹的话)和strut对应包的name对应,
可以使用同一个对应值,转发时比较方便,直接写对应jsp即可,因为会默认包含对应


所在的包。或者,使用绝对路径(比较好理解),直接           /jsp所在文件夹/xxx.jsp


关键配置:

/web.xml配置(只针对于struts2,没有涉及spring)//

<?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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<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>
</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.devMode" value="true"/>
	<package name="user" extends="struts-default" namespace="/user">
		<action name="register" 


class="com.zzia.action.UserAction" method="register">
			<result name="success">/registerOk.jsp</result>
			<result name="fail">/registerFail.jsp</result>
		</action>
		<action name="login" class="com.zzia.action.UserAction" 


method="login">
			<result name="success">/loginOk.jsp</result>
			<result name="fail">/loginFail.jsp</result>
		</action>
		<action name="list" class="com.zzia.action.UserAction" 


method="list">
			<result name="success">/listOk.jsp</result>
			<result name="fail">/listFail.jsp</result>
		</action>
	</package>
</struts>

-----------------------------hibernate-------------------------------
步骤:
1、编写HibernateUtils
2、编写hibernate.cfg.xml:

注意两种配置文件的配置(一个注解方式,一个xml方式)

<!-- 该语句是才用xml编写的配置文件(不推荐) -->
		
<!-- <mapping resource="com/test/entry/<span style="font-family: Arial, Helvetica, sans-serif;">User</span><span style="font-family: Arial, Helvetica, sans-serif;">.hbm.xml"/> --></span>
		<!-- 才用注解方式编写 -->
<mapping class="com.test.entry.User" />


3、加入log4j日志配置文件配置

注意事项:
1、bean上不要忘了加必须的注解
@Entity
public class User....
@GeneratedValue
@Id
public int getId() {
return id;
}


2、如果使用.getCurrentSession(); 不能再手动关闭session();
使用openSession()需要自己手动关闭。
3、如果不想要log4j上输出过多警告信息,可以在最后面加上
log4j.logger.com.opensymphony.xwork2=ERROR,stdout,logfile
4、注意每个业务.层需要new对象时,全部设置为 private Object o=new Object();(因为没有加入spring),然后get\set,便于后期加入spring,并且也可以便于观察AOP和IOC
关键配置:
///HibernateUtils。java///
<pre name="code" class="java">public class HibernateAnnotationUtils {
	private static SessionFactory sf = null;
	static {
		sf = new AnnotationConfiguration().configure


().buildSessionFactory();
	}


	public static SessionFactory getSessionFactory() {
		return sf;
	}


	public static Session getCurrentSession() {
		return sf.getCurrentSession();
	}


	public static Session openSession() {
		return sf.openSession();
	}
}


 ///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="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property 


name="connection.url">jdbc:mysql://localhost/ssh</property>
		<property name="connection.username">root</property>
		<property 


name="connection.password">xxx</property>


		<!-- 一些方便调试的配置 -->
		<property 


name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<!-- 一些优化以及连接池的配置 -->
		<!-- JDBC connection pool (use the built-in) -->
		<property name="connection.pool_size">1</property>


		<!-- Enable Hibernate's automatic session context 


management -->
		<property 


name="current_session_context_class">thread</property>


		<!-- Disable the second-level cache -->
		<property 


name="cache.provider_class">org.hibernate.cache.NoCacheProvider</propert


y>


		<!-- 该语句是才用xml编写的配置文件(过时) -->
		<!-- <mapping 


resource="com/test/entry/User.hbm.xml"/> -->
		<!-- 才用注解方式编写 -->
		<mapping class="com.test.entry.User" />
	</session-factory>


</hibernate-configuration>




-------------------spring--------------------------
步骤:
1、加入之前去掉的struts2-spring-plugin-2.1.6.jar
2、在web.xml加入spring的配置
1)、配置spring配置文件的索引路径(监听器和配置文件所在路径)
2)、配置中文乱码的解决:设置为自己需要的编码
3、配置applicationContext.xml
1)、配置bean的扫描路径(可以自动化,当然可以手动配置)
2)、配置jdbc配置文件,从jdbc.properties读取
3)、得到jdbc配置文件中的配置信息
4)、构建sessionfactory,加入实体扫描包,加入一些需要的配置信息,如数据库方言,格式化输出句等。
4、加入jdbc.properties
5、在每个bean中加入
 @Component 
public class...
@Resource
public void setUserService...


注意事项:
1、这是一个整合hibernate.cfg.xml到applicationContext.xml
中的配置spring。 整个项目中不在需要hibernate.cfg.xml
2、在action层必须加上
@Scope("prototype")//多例(不加上设计多线程错误,非常debug)
public class UserAction....

关键配置:
web.xml/
<pre name="code" class="html"><listener>
		<listener-


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


class>
		<!-- default: /WEB-INF/applicationContext.xml -->
	</listener>


	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- <param-value>/WEB-INF/applicationContext-


*.xml,classpath*:applicationContext-*.xml</param-value>  -->
		<param-value>classpath:applicationContext.xml</param-


value>
	</context-param>




<!-- 解决中文乱码问题 -->
<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>




 /applicationContext.xml/ 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" 


xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" 


xmlns:tx="http://www.springframework.org/schema/tx"
	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-3.0.xsd   
   http://www.springframework.org/schema/context 


http://www.springframework.org/schema/context/spring-context-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">
	<!-- 配置bean的扫描路径 -->
	<context:annotation-config />
	<context:component-scan base-package="com.zzia" />
	<!-- 整合hibernate数据库 -->
	<!-- 配置jdbc -->
	<bean
		


class="org.springframework.beans.factory.config.PropertyPlaceholderConfi


gurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	<bean id="dataSource" destroy-method="close"
	class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" 


value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>


	<bean id="sessionFactory"
		


class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFa


ctoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<value>com.zzia.model</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					


org.hibernate.dialect.MySQLDialect
				</prop>
				<prop 


key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>
<!-- new code -->
<!-- 	<bean id="hibernateTemplate" 


class="org.springframework.orm.hibernate3.HibernateTemplate"> -->
<!-- 		<property name="sessionFactory" 


ref="sessionFactory"></property> -->
<!-- 	</bean> -->


</beans>




jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh
jdbc.username=root
jdbc.password=xxx





------------------------------------------------------------------------------------------------------------------------------


源码下载地址:http://download.csdn.net/detail/wgyscsf/9250325














  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值