Struts2 Spring Hibernate 的简单整合
2008-11-12 19:06
489 查看
首先,加载Spring框架,如图:
为了便于以后添加新的应用,这里把spring所有的Jar包都添加了,下一步,要将Spring的配置文件创建在
WEB-INF目录下,或许不理它,到项目中去移动也可以。单击Finish, 对Spring的添加到此结束。
接着我们再添加Hibernate框架,如图:
接着选择将Hibernate的配置文件交给Spring来进行管理,如图:
再为Hibernate创建一个sessionFactory,如图:
接着再选择数据源,
接着是提示你是否建立sessionFactory,因为已经将sessionFactory交给Spring管理了,所以在这里不用创建了
单击Finish,并将Spring中与Hibernate中一样的Jar包全部替换,这样就完成了对Hibernate框架的加载了。
接着再加载struts2框架,这里就不再详细说明了,详情参考Struts2 + 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">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="3348635"></property>
</bean>
<bean id="sessionfactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 以下是添加的,不是自动生成的 -->
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
<!--上面是方便我们对程序的调试,和操作 -->
</props>
</property>
</bean>
<!-- 以下是添加的,不是自动生成的 -->
<!-- HibernateTemplate是一个帮助类,它能简化Hibernate Session的编码和处理HibernateExceptions-->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionfactory"/>
</property>
</bean>
<!-- 以上是手动添加的内容 -->
</beans>
这里我们要注意添加注释的一段,这些是对我们接下去的程序很有帮助的。
再编写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>
</struts>
接着就是最重要的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">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这时,还不能进行编写详细的代码,应该要先测试下这个环境出错了没。部署项目到 tomcat 里,运行没有
出现错误,如出现错误,请详细检查上面每一步的操作是否一致。
到此为止,开发环境三大框架整合的准备工作就完成了。
作者:COOL_CR 发表于2008-11-12 19:06:00 原文链接
阅读:1132 评论:1 查看评论
为了便于以后添加新的应用,这里把spring所有的Jar包都添加了,下一步,要将Spring的配置文件创建在
WEB-INF目录下,或许不理它,到项目中去移动也可以。单击Finish, 对Spring的添加到此结束。
接着我们再添加Hibernate框架,如图:
接着选择将Hibernate的配置文件交给Spring来进行管理,如图:
再为Hibernate创建一个sessionFactory,如图:
接着再选择数据源,
接着是提示你是否建立sessionFactory,因为已经将sessionFactory交给Spring管理了,所以在这里不用创建了
单击Finish,并将Spring中与Hibernate中一样的Jar包全部替换,这样就完成了对Hibernate框架的加载了。
接着再加载struts2框架,这里就不再详细说明了,详情参考Struts2 + 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">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="3348635"></property>
</bean>
<bean id="sessionfactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 以下是添加的,不是自动生成的 -->
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
<!--上面是方便我们对程序的调试,和操作 -->
</props>
</property>
</bean>
<!-- 以下是添加的,不是自动生成的 -->
<!-- HibernateTemplate是一个帮助类,它能简化Hibernate Session的编码和处理HibernateExceptions-->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionfactory"/>
</property>
</bean>
<!-- 以上是手动添加的内容 -->
</beans>
这里我们要注意添加注释的一段,这些是对我们接下去的程序很有帮助的。
再编写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>
</struts>
接着就是最重要的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">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这时,还不能进行编写详细的代码,应该要先测试下这个环境出错了没。部署项目到 tomcat 里,运行没有
出现错误,如出现错误,请详细检查上面每一步的操作是否一致。
到此为止,开发环境三大框架整合的准备工作就完成了。
作者:COOL_CR 发表于2008-11-12 19:06:00 原文链接
阅读:1132 评论:1 查看评论
相关文章推荐
- 整合Struts2,Hibernate和Spring的一个简单例子
- struts2 spring3 hibernate4 最简单整合
- struts2 spring3 hibernate4 最简单整合
- 整合开发Struts2,Hibernate,Spring简单框架的搭建详解
- 最近S2SH框架整合和简单测试 Struts2-2.3.14+Spring-4.0.6+Hibernate-4.3.6
- struts2 spring4 hibernate4(s2sh)整合开发--简单实体的增删改查操作
- 最简单的 Struts2 Spring2 Hibernate3 整合 实例
- Struts2 Spring Hibernate 的简单整合
- Struts2SpringHibernate框架的简单整合1
- Spring3.x_Struts2.x_Hibernate3.x整合之分页pager-taglib的简单使用
- Spring Struts2 Hibernate - - 注解版 demo整合
- struts2、hibernate、spring整合
- Struts2、Spring、Hibernate整合ExtJS
- 整合Struts2、Spring、Hibernate构建J2EE应用
- 学习三大框架Struts2 、 Hibernate 、 Spring时,涉及到三大框架的配置文件以及整合。
- Spring3.0、Hibernate3.0、Struts2整合文件配置
- 第五章 Spring3.0 、Hibernate3.3与Struts2的整合
- spring 学习之三(spring 与hibernate, struts2整合)
- Spring 框架整合Struts2 框架和 Hibernate 框架
- Struts2、Hibernate、Spring整合时如何向Servlet注入属性