eclipse环境下基于已构建struts2项目整合spring+hibernate
本文是基于已构建的struts2项目基础上整合
spring+hibernate,若读者还不熟悉struts2项目,请先阅读
eclipse环境下基于tomcat-7.0.82构建struts2项目
实现步骤:
第一步:引入spring所需jar包,如下图所示:

第二步:导入hibernate所需jar包,如下图中被选中jar文件:

第三步:导入struts-spring整合包,暂且就这么称呼吧

第四步:导入MySQL驱动包:

第五步:所有准备工作就绪后,接下来创建spring与hibernate配置文件,命名为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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" 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/task
http://www.springframework.org/schema/task/spring-task-3.1.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
"> </beans>
这是spring配置文件命名空间的引入,命名空间具体含义请参考前辈资料
6.2.3 Spring 2.5配置文件详解(1)
第六步:在web.xml文件中配置对spring的监听,配置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SSHDemo</display-name> <!-- spring的监听器配置开始 -->
<!-- spring监听器的作用:提供实例(IOC) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 将请求路径交由struts过滤 -->
<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>
这是在struts2项目基础配置上增加对spring的监听。
第七步:项目下构建业务层接口(TestServiceI)、实现类(TestServiceImpl)以及数据库操作层接口(TestDaoI)、实现类(TestDaoImpl)
整个项目结构如下图所示:

其中dao层实现类中我们需要注入一个session工厂(工厂注入请留意本文后续),接着打开session工厂用于操作数据库,代码如下所示:
package wjt.com.test.dao.impl; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory; import wjt.com.test.dao.TestDaoI; public class TestDaoImpl implements TestDaoI{ private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} private Session getCurrentSession() {
try {
return sessionFactory.getCurrentSession();
} catch (HibernateException e) {
return sessionFactory.openSession();
}
} @Override
public void testDaoMethod() {
System.out.println("dao层测试方法...");
if(getCurrentSession()!=null) {
System.out.println("session工厂注入成功!");
} } }
其中service层实现类中我们暂时写一个空方法进行测试dao层接口对象的注入成功与否并调用dao层方法,代码如下:
package wjt.com.test.service.impl; import wjt.com.test.dao.TestDaoI;
import wjt.com.test.service.TestServiceI; public class TestServiceImpl implements TestServiceI{ private TestDaoI testDao; public void setTestDao(TestDaoI testDao) {
this.testDao = testDao;
} @Override
public void testServiceMethod() {
System.out.println("service层测试方法...");
testDao.testDaoMethod();
} }
其中Action层我们在原来struts2项目的基础上注入service层接口并调用其方法,代码如下:
package wjt.com.test.action;
import com.opensymphony.xwork2.ActionSupport;
import wjt.com.test.service.TestServiceI;
public class TestAction extends ActionSupport{
private TestServiceI testService;
public void setTestService(TestServiceI testService) {
this.testService = testService;
}
public String execute() throws Exception {
System.out.println("struts==========================");
testService.testServiceMethod();
return "success";
}
}
整个s2sh项目中的结构布局如上述第七步所示,用于视图映射的类我们封装在action包下,用于实现业务逻辑的我们封装在service包下,用于访问数据库对数据进行操作的我们封装在dao包下。
第八步:配置spring+hibernate的配置文件
截止到现在整个s2sh项目的准备工作已经准备完毕,接下来我们我们配置hibernate的数据源、session工厂,配置spring的核心之一控制反转IOC或称为依赖注入DI。
配置如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" 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/task
http://www.springframework.org/schema/task/spring-task-3.1.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
"> <!-- 数据源配置 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/wjt_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true" />
<property name="username" value="root" />
<property name="password" value="wujingtao" />
</bean> <!-- 配置hibernate session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property> </bean> <!-- 注入对象 采用属性注入方式--> <bean id="testDao" class="wjt.com.test.dao.impl.TestDaoImpl" scope="prototype">
<property name="sessionFactory" ref="sessionFactory"/><!-- 引用上方的hibernate工厂 -->
</bean> <bean id="testService" class="wjt.com.test.service.impl.TestServiceImpl" scope="prototype">
<property name="testDao" ref="testDao"/>
</bean> <bean id="testAction" class="wjt.com.test.action.TestAction" scope="prototype">
<property name="testService" ref="testService"/>
</bean> </beans>
其中IOC配置采用属性注入的方式,大家可以根据个人喜好选择spring注入的几种方式,我个人偏爱基于注解的属性注入方式,注解方式注入会在后续的文章中给出。
第九步:将struts2运行时的对象交由spring创建,即所谓的应用IOC
在struts.xml配置文件中添加如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"><!-- 这里要和struts2版本号统一 --> <struts> <!-- 告知Struts2运行时使用Spring来创建对象 这行代码是新加的-->
<constant name="struts.objectFactory" value="spring" /> <package name="default" extends="struts-default">
<action name="login" class="wjt.com.test.action.TestAction">
<result name="success">index.jsp</result>
</action>
</package> </struts>
测试项目:
在tomcat下部署并启动项目
浏览器地址栏输入:http://localhost:8080/SSHDemo/login
浏览器显示如下所示:

eclipse输出显示如下:(因为我们在各层有打印信息)

到这里完整的s2sh项目构建完毕并通过测试。
本人是刚毕业的小白,写文章的目的是全当给自己做笔记了,博文有不合理的地方还请读者指出!
eclipse环境下基于已构建struts2项目整合spring+hibernate的更多相关文章
- 基于已构建S2SH项目配置全注解方式简化配置文件
如果还不熟悉s2sh项目搭建的朋友可以先阅读 eclipse环境下基于tomcat-7.0.82构建struts2项目 eclipse环境下基于已构建struts2项目整合spring+hiberna ...
- eclipse环境下基于tomcat-7.0.82构建struts2项目
开山第一篇,毕业4个月目前接触最多的框架还是s2sh框架.... 具备完整的开发环境下,在eclipse下启动tomcat出现如下所示画面表示环境构建成功. 第一步:创建web项目,截图如下 此页面只 ...
- 详细的图文教程来实现 eclipse环境下如何配置tomcat,并且把项目部署到Tomcat服务器上
很多初学,尤其自学JavaWeb的朋友首次在eclipse下配置tomcat时,总会有种难下手的感觉,在此,通过图文解说的方法,最直观的向大家演示一遍该配置过程. 第一部分:eclipse环境下如何配 ...
- .NET环境下基于RBAC的访问控制
.NET环境下基于RBAC的访问控制 Access Control of Application Based on RBAC model in .NET Environment 摘 要:本文从目前信息 ...
- eclipse环境下如何配置tomcat
eclipse环境下如何配置tomcat 很多初学,尤其自学JavaWeb的朋友首次在eclipse下配置tomcat时,总会有种难下手的感觉,在此,通过图文解说的方法,最直观的向大家演示一遍该配置过 ...
- Jenkins:基于linux构建ivy项目
Jenkins:基于linux构建ivy项目 (二) 基于以上<Jenkins:VMware虚拟机Linux系统的详细安装和使用教程(一)>的配置再进行对ivy项目构建: 启动tomcat ...
- CentOS 环境下基于 Nginx uwsgi 搭建 Django 站点
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,CentOS 环境下基于 Nginx uwsgi 搭建 Django 站点 以下 ...
- java(一)IntelliJ和eclipse环境下的Hello World
1. IntelliJ环境下的Hello World 1. 启动IntelliJ IDE,选择File->New->Project 选择Java如果没有出现Project SDK,则选择N ...
- 【应用笔记】【AN004】VB环境下基于RS-485的4-20mA电流采集
版本:第一版作者:周新稳 杨帅 日期:20160226 =========================== 本资料高清PDF 下载: http://pan.baidu.com/s/1c1uuhLQ ...
随机推荐
- JavaScript函数之作用域 / 作用链域 / 预解析
关于作用域和作用链域的问题,很多文章讲的都很详细,本文属于摘录自己觉得对自己有价值的部分,留由后用,仅供参考,需要查看详细信息请点击我给出的原文链接查看原文件 做一个有爱的搬运工~~ -------- ...
- WAMPServer配置修改及问题汇总
备忘录 软件版本wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b -------------------------------------- ...
- Junit基本使用
Junit基本用法 1.创建Junit Test Case 2.基本使用(以oracle数据库操作为例) package com.csit.adminsystem1.tests; import sta ...
- 【ASP.NET MVC 学习笔记】- 03 Razor语法
本文参考:http://www.cnblogs.com/willick/p/3224144.html 1.Razor语句以@开头. 2.每个View都有自己的Model属性,可通过@Model调用.语 ...
- angularJS的环境搭建--初学
一 \在这里简单介绍一下Angular-cli的特性: Angular-cli可以快速搭建框架,创建module,service,class,directive等: 有webpack的功能,可以实现 ...
- LeetCode 277. Find the Celebrity (找到明星)$
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...
- LeetCode 18. 4Sum (四数之和)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- JPA之常用 基本注解
1.常用基本注解 @Entity @Table @Basic @Column @GeneratedValue @Id 2.特殊注解 @Transient @Temporal 用 table 来生成主键 ...
- CentOS设置系统时间、硬件时间、以及定时校对时间
CentOS设置系统时间和时区 一.设置时区 方法一:使用setup工具 setup 选择Timezone configuration 选择Asia/Shanghai 空格键勾选上System clo ...
- Linux库函数制作(静态库、动态库)
Linux库函数制作(静态库.动态库) 静态库与动态库 链接方式 链接分为两种:静态链接.动态链接 静态链接: 由链接器在链接时将库的内容加入到可执行程序中 静态链接的特点是: 优点: 对运行环境的依 ...