您的位置:首页 > 编程语言 > Java开发

JAVA_WEB项目之Struts2、Spring和Hibernate三大框架的整合

2014-07-14 15:56 841 查看
我们使用三大框架开发web项目可以提高我们的开发效率,下面贴出我在学习过程中如何整合三大框架。

在整合之前,我们先了解用户库(User Library)

User Library:可以对我们添加的第三方包进行管理。

说明:刚开始初学者在学习的过程中导入第三方包都是直接复制到WEB-INF/lib目录下,我也是这样过来的,然而如果在用导入的包过多的过程中就会涉及到一些不必要的麻烦,比如说:团队开发中,每个团队成员开发自己负责的某个模块时,如果按照把第三方包复制到WEB-INF/lib就会导致包重复要进行覆盖等问题。因而在团队开发中此种方式完全没必要运用到,我们可以使用myecplise提供的利用User Library进行第三包的管理。

如何用User Library进行管理呢,接下来贴出我在整合三大框架中详细的过程:

1、首先装备好你的包,放到某个磁盘下,位置任你自己放,方便你自己管理,我贴出的是我放置的位置:





2、打开myecplise,新建项目叫做shop,点击项目,右键选择Build path->Add User Lbraries,弹出Add User Lbraries对话框:



3、选择User Library,点击next。



说明:这里的用户库我之前早已新建好,如果你是第一次打开,则应该是空白。

3、点击User Libraries



4、点击new,新建用户库,名字自定义:



5、名字自定义好之后,点击Add JARS,添加jar包,比如说:我这里定义的用户库添加的是我在第一步中贴出来的jar包。

添加用户库介绍到这里。

接下来介绍如何用myecplise整合三大框架:

过程:

(1)点击shop项目,右键选择myecplise->Add Hibernate Capabitities,这时会弹出对话框:我们选择User Libraries:选中hibernate3.6用户库,



(2)点击next,



(5)点击next,会出现如下对话框:

到了这一个步骤,由于DB Drive要跟数据库连接,r接下来介绍如何在myecplise连接mysql数据库,

在菜单栏中window->show view 打开 DB Borwser,会出现默认的数据库连接买我们删除掉,让 DB Borwser空白,没有任何东西。接着鼠标放在DB Borwser窗口中,点击右键选择new,弹出窗口如下:录入数据和导入jar包:



最后点击finish,选择mysql,右键点击 open connection连接数据库,出现结果如下:



myecplise连接数据库就到了这里,接下来回到第五个步骤,继续hibernate的配置,在DB Driver下拉框中会出现我们刚才配置mysql数据库连接,选中它:



继续next,新建包com.shop.utils:

点击finish,目录如下:



接下来配置spring,点击项目,右键选择myecplise->add Spring Cataplities,弹出去对话框如下:



点击next:





点击finish。

接下来配置struts,点击项目,右键选择myecplise->add Struts Cataplities,弹出去对话框如下:





点击finish。

接下来是通过配置文件来整合三大框架:

项目的目录结构如下:



在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"
xmlns:p="http://www.springframework.org/schema/p"
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/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 id="date" class="java.util.Date"></bean>
<!--
1、配置连接池
2、配置localSessionFactoryBean
3、配置HibernateTemplate
4/配置事务管理器
5.配置aop
6、spring和sturts2的整合
6.1:添加struts2-spring-plugin-2.1.6.jar包,
6.2:启动的时候通过监听器加载spring的配置文件
6.3:把Action交给spring管理告诉struts创建action的时候到Spring中创建
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shop"/>
<property name="user" value="root"/>
<property name="password" value="04010"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<!-- save开头的方法需要添加事务,有就不需要创建,没有就要创建 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<!-- NEVER不需要事务,如果运行环境有错误就会抛出异常 -->
<!-- "NOT_SUPPORTED" 有就有,没有就没有 ,read-only="true"处理以上三个方法都是只读的-->
<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 配置切面表达式 ,配置哪些报下的哪些类的哪些方法需要切入,但是没有事务的传播特性-->
<aop:pointcut expression="execution(* com.shop.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<span style="white-space:pre">	</span><bean id="categoryService" class="com.shop.service.impl.CategoryServiceImpl">
<span style="white-space:pre">		</span><property name="ht" ref="hibernateTemplate"></property>
<span style="white-space:pre">	</span></bean>
</beans>
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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
<!-- 数据库连接的配置信息 -->
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 	<property name="connection.url">jdbc:mysql://localhost:3306/shop</property>
<property name="connection.username">root</property>
<property name="connection.password">04010</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
-->
<!-- Hibernate自身的配置信息 -->
<property name="show_sql">true</property>
<!-- 映射文件的配置信息 -->
<mapping resource="com/shop/pojo/Category.hbm.xml" />

</session-factory>

</hibernate-configuration>
struts.xml中的配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf8" />
<package name="shop" extends="struts-default">
<action name="category_*" class="categoryAction" method="{1}"></action>
</package>
</struts>


web.xml中的配置:

<?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"> <!--  <filter>
<filter-name>characterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐