Struts2+Hibernate+Spring整合教程
来源:互联网 发布:java二维数组初始化为0 编辑:程序博客网 时间:2024/06/16 22:11
IDE: Eclipse Mars Release (4.5.0)
DB: MySQL 5.1.73
服务器: Tomcat 7.0
1,首先在Eclipse中新建一个Dynamic Web Project,暂且起名为SSH;
2,新建包名:
com.juyuan238.ssh.daocom.juyuan238.ssh.dao.implcom.juyuan238.ssh.domincom.juyuan238.ssh.servicecom.juyuan238.ssh.service.implcom.juyuan238.ssh.webcom.juyuan238.ssh.util
3,导入jar包:
http://download.csdn.net/detail/samile6899/9155539(SSH整合jar包)
a,在 web.xml 中配置struts2过滤器:
<filter><filter-name>action</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>action</filter-name><url-pattern>/*</url-pattern></filter-mapping>
b,新建config源文件夹,并在该文件夹中新建struts.xml配置文件
<?xml version="1.0"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><struts><package name="shw" namespace="/" extends="struts-default"></package></struts>5,将 log4j.properties 文件复制粘贴到 config 文件夹中;
6,新建 jdbc.properties 文件在 config 文件夹中:
7,新建 hibernate.cfg.xml 文件到 config 文件夹;
<?xml version="1.0"?><!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="dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="hbm2ddl.auto">update</property><property name="show_sql">true</property></session-factory></hibernate-configuration>8,新建 ApplicationContext.xml 文件在 config 文件夹中;
<?xml version="1.0"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "><!-- 组件扫描 --><context:component-scan base-package="com.juyuan238.ssh.dao.impl,com.juyuan238.ssh.service.impl,com.juyuan238.ssh.web" /><!-- 分散配置 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverclass}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxPoolSize" value="${c3p0.pool.size.max}" /><property name="minPoolSize" value="${c3p0.pool.size.min}" /><property name="initialPoolSize" value="${c3p0.pool.size.ini}" /><property name="acquireIncrement" value="${c3p0.pool.size.increment}" /></bean><!-- 本地会话工厂bean,spring整合hibernate的核心入口 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 注入数据源 --><property name="dataSource" ref="dataSource" /><!-- 指定hibernate配置文件 --><property name="configLocation" value="classpath:hibernate.cfg.xml" /><!-- 指定映射文件目录 --><property name="mappingDirectoryLocations"><list><value>classpath:com/juyuan238/ssh/domin</value></list></property></bean><!-- 事务管理器,在service层面上实现事务管理,而且达到平台无关性 --><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置事务通知 --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" /><tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" /><tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" /><tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT" /><tx:method name="new*" propagation="REQUIRED" isolation="DEFAULT" /><tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/><tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/><tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/><tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" /></tx:attributes></tx:advice><!-- aop事务配置 --><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))"/></aop:config></beans>9,配置 Spring 的监听器在 web.xml 中;
10,新建测试类,测试数据源:
注: 如果正常的话,控制台会打印出信息如下:
11,接下来的话,我们再来测试下发布成 web 应用时,框架是否能正确运行:
a,在 com.juyuan238.ssh.web 中新建一个类,暂且起名为: ShwAction
@Controller("shwAction")public class ShwAction {public String index(){return "load";}}b,到 struts2 配置文件 struts.xml 中,编写配置文件:
<struts><package name="struts2" namespace="/" extends="struts-default"><action name="shw" class="shwAction" method="{1}"><result name="load">WEB-INF/jsp/success.jsp</result></action></package></struts>c,在WEB-INF 目录下新建 jsp 文件夹,并在 jsp 文件夹中新建 success.jsp ;
注: 新建 jsp 页面的时候,可能会发现 jsp 页面报错,没事,这是正常的。解决办法如下 :
右键当前项目---->Build Path---->Configure Build Path----->切换到" Libraries "------>Add Libray----->Server Runtime------->选择相应的" Tomcat "版本---->finish----->OK
d,在 index.jsp 中添加超链接:
e,将项目部署到 tomcat 中,并启动 tomcat 服务器;
f,点击链接,如果能够链接到 success.jsp 页面,说明框架整合成功!否则的话,就要继续努力了……
好了,三大框架的整合说到这里就已经结束了,希望对大家能够有所帮助……
- Struts2+Hibernate+Spring整合教程
- struts2+hibernate+spring整合
- struts2 spring hibernate 整合
- Spring struts2 hibernate 整合
- struts2+spring+hibernate整合
- struts2+spring+hibernate整合
- struts2+spring+hibernate整合
- Spring、Struts2、Hibernate整合
- Spring + Hibernate + Struts2整合
- struts2+spring+hibernate整合
- struts2+spring+hibernate整合
- Struts2+Hibernate+Spring整合
- Spring Struts2 Hibernate整合
- 整合struts2+spring+hibernate
- spring struts2 hibernate整合
- 整合Struts2,Spring,Hibernate
- Spring + Struts2 + Hibernate整合
- spring+struts2+hibernate整合
- 13.OC方法和文件编译
- C++中friend的使用(friend function and friend class)
- istream_iterator创建和值的变化
- C++学习笔记之继承
- 动态规划
- Struts2+Hibernate+Spring整合教程
- 14.OC内存管理
- 15.OC面向对象—封装
- java从0单排之<<java就业培训教程>>复习与面试题回顾——02
- 一起学spring--依赖注入---简单粗暴的例子展示
- 【机器学习】机器学习界大牛林达华推荐的书籍
- 16.OC面向对象—继承
- 17.OC面向对象—多态
- 二叉排序树的建立、结点插入及删除