本文转载自
Struts 2 - Spring 4 - Hibernate 4 Integration Tutorial Part 1 - XML Configuration
主要内容是结合eclipse,通过maven来整合struts2、spring4和hibernate4
在阅读本文之前最好先看一下预备篇
Spring 4 and Struts 2 Integration Tutorial Part 1: XML Configuration
预备篇中详细讲解了怎么通过eclipse创建一个maven的web application,而且预备篇中的servlet&JSP依赖配置在本文中已经被直接忽略了。
如果对Annotations的配置方式比较感兴趣,可以查看如下的两篇文章
Spring 4 and Struts 2 Integration Tutorial Part 2: Java-Based and Annotations
Struts - Spring - Hibernate Integration Tutorial Part 2 - Java-Based and Annotations
Welcome to the first part of Struts - Spring - Hibernate integration tutorial series! Let imagine you are assigned a legacy project which is based-on Struts 2 framework. Your critical mission is to add Spring and Hibernate to the project and make sure these three guys (Struts, Spring and Hibernate) working together in harmony. And imagine this is the last chance you can impress your boss (he promises you with salary increase or promotion if you can get the project done perfectly). Being excited, you’re looking around for solutions and finally reading this page!
My friend, I want to tell you this: This tutorial is written for you to solve such problems. I will walk you through the steps of developing a sample Java web project which utilizes the three frameworks Struts, Spring and Hibernate. The purpose of the application is to list all products from a MySQL database in a web page (JSP).
Before going into the details, let’s review some key points first. I think these key points are very important as it helps us understand what is really going on with the integration at a high level.
Why Integration of Struts, Spring and Hibernate?
Struts is a web application framework, Spring is an enterprise application framework, and Hibernate is an ORM framework. Hibernate is working at the database layer, whereas both Struts and Spring can work at the web layer. Spring is more powerful than Struts and it can replace Struts. So if you are starting a new project with Spring and Hibernate, don’t use Struts!
This kind of integration is only necessary for legacy projects which were built with Struts and now you have to upgrade them with Spring and Hibernate while still keeping Struts. Therefore, there are very few Struts-Spring-Hibernate applications in practice.
How does the Integration of Struts, Spring and Hibernate work?
In this kind of integration, Struts should intercept all requests coming to the application by working as a dispatcher filter. Spring should act as a dependency injection container which also manages Hibernate session and provides transaction management services.
The interesting point is that Struts’ action classes are managed by Spring. Therefore, action mappings in Struts can refer to a Spring bean. This only can be done by using the Spring Plug-in provided by Struts.
Back to the sample project we are going to develop in the next few minutes, I suggest you use the following technologies:
- Java 8
- Struts Framework 2.3.20
- Spring Framework 4.1.4.RELEASE
- Hibernate Framework 4.3.8.Final
- Tomcat 8.0
- Eclipse Luna (with Maven 3)
- MySQL 5.5
- Apache Commons DBCP
NOTE: in this first part, all the configurations are done by using XML.
Now, let’s create a Dynamic Web project in Eclipse and then convert it to a Maven project (If you don’t know how to do this, follow the section 1 in the tutorial: Spring 4 and Struts 2 Integration Tutorial Part 1: XML Configuration). After the project is created, follow the steps below:
1. Setting Up Database
Execute the following MySQL script to create a database called productsdb with a table called product:
1
2
3
4
5
6
7
8
9
|
CREATE DATABASE `productsdb`; CREATE TABLE `product` ( `product_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`description` varchar(512) NOT NULL,
`price` float NOT NULL,
PRIMARY KEY (`product_id`)
) |
Our application will list all products from the product table. Remember to insert some dummy data for testing the application later.
2. Adding Maven Dependencies
First, declare properties for version numbers of Java, Struts, Spring, Hibernate and MySQL Connector library as follows:
1
2
3
4
5
6
7
|
< properties >
< java-version >1.8</ java-version >
< org.springframework-version >4.1.4.RELEASE</ org.springframework-version >
< org.strutsframework-version >2.3.20</ org.strutsframework-version >
< org.hibernateframework-version >4.3.8.Final</ org.hibernateframework-version >
< org.mysqlconnector-version >5.1.34</ org.mysqlconnector-version >
</ properties >
|
Dependencies for Spring framework:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-context</ artifactId >
< version >${org.springframework-version}</ version >
</ dependency >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-context-support</ artifactId >
< version >${org.springframework-version}</ version >
</ dependency >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-orm</ artifactId >
< version >${org.springframework-version}</ version >
< type >jar</ type >
< scope >compile</ scope >
</ dependency >
|
Recommended Book: Maven: The Definitive Guide
Dependencies for Struts framework:
1
2
3
4
5
6
7
8
9
10
11
|
< dependency >
< groupId >org.apache.struts</ groupId >
< artifactId >struts2-core</ artifactId >
< version >${org.strutsframework-version}</ version >
</ dependency >
< dependency >
< groupId >org.apache.struts</ groupId >
< artifactId >struts2-spring-plugin</ artifactId >
< version >${org.strutsframework-version}</ version >
</ dependency >
|
Dependencies for Hibernate ORM framework:
1
2
3
4
5
|
< dependency >
< groupId >org.hibernate</ groupId >
< artifactId >hibernate-core</ artifactId >
< version >${org.hibernateframework-version}</ version >
</ dependency >
|
Dependency for Apache Commons DBCP (database connection pooling):
1
2
3
4
5
|
< dependency >
< groupId >org.apache.commons</ groupId >
< artifactId >commons-dbcp2</ artifactId >
< version >2.0</ version >
</ dependency >
|
Dependency for MySQL Connector-Java library:
1
2
3
4
5
|
< dependency >
< groupId >mysql</ groupId >
< artifactId >mysql-connector-java</ artifactId >
< version >${org.mysqlconnector-version}</ version >
</ dependency >
|
For the complete content of Maven configuration file, look for the pom.xml file in the attached project.
3. Coding and Mapping Model Class
Create a POJO class (Product.java) to model the product table in the database:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
/** * Copyright CodeJava.net To Present
* All rights reserved.
*/
package net.codejava.framework.model;
public class Product {
private long id;
private String name;
private String description;
private float price;
public long getId() {
return id;
}
public void setId( long id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this .description = description;
}
public float getPrice() {
return price;
}
public void setPrice( float price) {
this .price = price;
}
} |
And here’s its mapping class (Product.hbm.xml):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
< hibernate-mapping package = "net.codejava.framework.model" >
< class name = "Product" table = "PRODUCT" >
< id name = "id" column = "PRODUCT_ID" >
< generator class = "native" />
</ id >
< property name = "name" />
< property name = "description" />
< property name = "price" />
</ class >
</ hibernate-mapping >
|
This is a pretty simple XML mapping file which tells Hibernate how to map the above POJO class with the table in the database.
4. Coding DAO Classes
Because our application has only function - list all products - hence the following DAO interface (ProductDAO.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * Copyright CodeJava.net To Present
* All rights reserved.
*/
package net.codejava.framework.dao;
import java.util.List;
import net.codejava.framework.model.Product;
public interface ProductDAO {
List<Product> list();
} |
And the following code is for the implementation class (ProductDAOImpl.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/** * Copyright CodeJava.net To Present
* All rights reserved.
*/
package net.codejava.framework.dao;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import net.codejava.framework.model.Product;
public class ProductDAOImpl implements ProductDAO {
private SessionFactory sessionFactory;
public ProductDAOImpl(SessionFactory sessionFactory) {
this .sessionFactory = sessionFactory;
}
@Override
@Transactional
public List<Product> list() {
@SuppressWarnings ( "unchecked" )
List<Product> listProduct = (List<Product>)
sessionFactory.getCurrentSession().createCriteria(Product. class )
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
return listProduct;
}
} |
As you can see, the list() method simply returns a list of products from the database through the SessionFactorywhich is injected via constructor by Spring.
5. Coding Struts Action Class
Now, create a class that acts as a Struts action (ListProductAction.java) with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/** * Copyright CodeJava.net To Present
* All rights reserved.
*/
package net.codejava.framework.action;
import java.util.List;
import net.codejava.framework.dao.ProductDAO;
import net.codejava.framework.model.Product;
import com.opensymphony.xwork2.ActionSupport;
public class ListProductAction extends ActionSupport {
private ProductDAO productDAO;
private List<Product> listProduct;
public void setProductDAO(ProductDAO productDAO) {
this .productDAO = productDAO;
}
public String execute() {
listProduct = productDAO.list();
return SUCCESS;
}
public List<Product> getListProduct() {
return listProduct;
}
} |
The action method execute() invokes the ProductDAO to retrieve a list of products which is used by the view (JSP) via the getter method getListProduct(). Spring will inject an instance of ProductDAO via setter method setProductDAO().
Recommended Book: Struts 2 in Action
So far we have done all the Java code parts. Now it’s time to make configurations to wire the pieces of Struts, Spring and Hibernate together.
6. Coding View Page
Create a JSP file called ProductList.jsp under the /WEB-INF/views directory (create the views directory first) with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< title >Product List</ title >
</ head >
< body >
< div align = "center" >
< table width = "80%" border = "1" style = "border-collapse: collapse;" >
< tr >
< th >No</ th >
< th >Product Name</ th >
< th >Description</ th >
< th >Price</ th >
</ tr >
< s:iterator value = "listProduct" status = "stat" >
< tr >
< td >< s:property value = "#stat.count" /></ td >
< td >< s:property value = "name" /></ td >
< td >< s:property value = "description" /></ td >
< td >< s:property value = "price" /></ td >
</ tr >
</ s:iterator >
</ table >
</ div >
</ body >
</ html >
|
This page simply uses Struts tag to iterate over the collection of products and display result in an HTML table.
7. Configuring Web Deployment Descriptor (web.xml)
Configure Struts and Spring in the web.xml file as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id = "WebApp_ID" version = "3.1" >
< display-name >Struts2Spring4Hibernate4XML</ display-name >
< context-param >
< param-name >contextConfigLocation</ param-name >
< param-value >/WEB-INF/spring/appContext.xml</ param-value >
</ context-param >
< listener >
< listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
</ listener >
< filter >
< filter-name >DispatcherFilter</ filter-name >
< filter-class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</ filter-class >
</ filter >
< filter-mapping >
< filter-name >DispatcherFilter</ filter-name >
< url-pattern >/*</ url-pattern >
</ filter-mapping >
</ web-app >
|
Here, Struts is configured as the dispatcher servlet in order to intercept all requests coming to the application, whereas Spring is configured as a context listener which is responsible for managing beans and injecting dependencies.
8. Configuring Hibernate framework
Create an XML file called hibernate.cfg.xml under the source directory with the following content:
1
2
3
4
5
6
7
8
9
10
11
|
<? xml version = '1.0' encoding = 'utf-8' ?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
< hibernate-configuration >
< session-factory >
< property name = "dialect" >org.hibernate.dialect.MySQLDialect</ property >
< property name = "show_sql" >true</ property >
< mapping resource = "net/codejava/framework/model/Product.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
|
In this Hibernate configuration file, we simply specify the database dialect is MySQL and tell Hibernate where to look for the mapping file.
If you are new to Hibernate and want to learn more, read this book: Hibernate Made Easy: Simplified Data Persistence with Hibernate and JPA (Java Persistence API) Annotations
9. Configuring Struts framework
On the Struts side, create an XML file called struts.xml under the source folder and put the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<? 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 >
< package name = "Struts2Spring4Hibernate4Demo" extends = "struts-default" >
< action name = "listProduct" class = "listProductActionBean" >
< result name = "success" >/WEB-INF/views/ProductList.jsp</ result >
</ action >
</ package >
</ struts >
|
Here, we declare an action mapping to handle the URL /listProduct. Note that the action class is now pointing to name of a bean managed by Spring - listProductActionBean.
We also configure mapping for the SUCCESS view that points to the ProductList.jsp page.
10. Configuring Spring framework
On the Spring side, create an XML file called appContext.xml under the /WEB-INF/spring directory (create the springdirectory first). Put the following XML code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<? 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:tx = "http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
< bean id = "listProductActionBean" class = "net.codejava.framework.action.ListProductAction" >
< property name = "productDAO" ref = "productDAO" />
</ bean >
< bean id = "productDAO" class = "net.codejava.framework.dao.ProductDAOImpl" >
< constructor-arg >
< ref bean = "sessionFactory" />
</ constructor-arg >
</ bean >
< bean id = "sessionFactory"
class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
< property name = "dataSource" ref = "dataSource" />
< property name = "configLocation" value = "classpath:hibernate.cfg.xml" />
</ bean >
< bean id = "dataSource" class = "org.apache.commons.dbcp2.BasicDataSource"
destroy-method = "close" >
< property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
< property name = "url" value = "jdbc:mysql://localhost:3306/productsdb" />
< property name = "username" value = "root" />
< property name = "password" value = "P@ssw0rd" />
</ bean >
< tx:annotation-driven />
< bean id = "transactionManager"
class = "org.springframework.orm.hibernate4.HibernateTransactionManager" >
< property name = "sessionFactory" ref = "sessionFactory" />
</ bean >
</ beans >
|
From top to bottom, we declare the listProductActionBean which is the Struts action. Then productDAO bean is declared and injected to the listProductActionBean.
The productDAO requires a SessionFactory so we declare a SessionFactory bean next. In turn, the SessionFactoryrequires a DataSource so next we declare a BasicDataSource bean which simply contains parameters for database connection.
Finally we declare a TransactionManager on top of the SessionFactory in order to facilitate transaction management services for the productDAO bean.
11. Final Project Structure
Hurray! We have done all the heavy stuff of Java code and XML configuration. Looking back, we have the following project structure:
Refer to this in case you did something wrong.
12. Testing the Application
Now, it’s time to enjoy our hard work done so far. Deploy the application on Tomcat server and type the following URL into your browser to access the application:
http://localhost:8080/Struts2Spring4Hibernate4XML/listProduct
And here’s our sweet result:
相关推荐
Struts2-Spring-Plugin-2.2.1.1.jar是Apache Struts 2框架的一个重要插件,主要用于整合Struts 2与Spring框架。这个插件在开发基于SSH(Struts 2、Spring、Hibernate)的企业级应用时扮演着关键角色。SSH是一个流行...
【标题】"Spring-Struts2-Hibernate-Maven整合教程" 在Java Web开发领域,Spring、Struts2、Hibernate这三大框架的组合,通常被称为SSH(Spring-Struts2-Hibernate),是构建企业级应用的常见选择。Maven作为项目...
s2sh代码-maven+spring4+hibernate4+struts2-登录注册的小demo(只是给讲了下如何使用该框架),参考博客:http://blog.csdn.net/lxfHaHaHa/article/details/79366330
2. `3struts-spring-hibernate.txt`:这可能是一个关于如何将Struts2、Spring3和Hibernate4整合的文本教程,内容可能涵盖Action的配置、Spring的ApplicationContext配置、Hibernate的SessionFactory配置,以及它们...
标题 "spring-struts2-maven" 指的是一个整合了Spring、Struts2和Maven的项目。这个项目是为了解决在Eclipse环境中如何有效地管理依赖、构建和部署包含这三个关键组件的Web应用程序。下面我们将深入探讨这些技术及其...
Struts2、Spring和Hibernate是Java开发中三大主流框架,它们的整合应用是企业级Java Web项目中的常见实践。这个源码包"struts2.3.7-spring3.1.0-hibernate4.0.0 整合源码"提供了一个完整的示例,展示了如何将这三个...
这是一个基于Java技术栈的经典组合,包括Maven 3.3.9、Spring 4.3.10、Struts2 2.3.34、Hibernate 4.3.10以及EasyUI 1.5.3的整合教程。这个项目可能是为了帮助初学者或者开发者了解如何将这些流行的技术框架集成到一...
总之,“maven整合spring+hibernate+struts2”是一种常见的Java Web开发架构,它通过Maven的依赖管理,Spring的全面控制,Hibernate的对象关系映射,以及Struts2的视图展现,为复杂的企业级应用提供了坚实的基础。...
Struts2-Spring-Hibernate整合框架详解 在Java Web开发领域,Struts2、Spring和Hibernate是三个非常重要的开源框架,它们分别负责MVC模式中的Action层、业务逻辑层和服务数据访问层。将这三个框架整合在一起,可以...
"用maven整合struts+spring+hibernate"这个主题,就是关于如何将这三大核心框架——Struts、Spring和Hibernate——集成到一个项目中的实践教程。Struts提供了MVC(Model-View-Controller)架构,Spring是全面的后端...
Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们各自解决应用程序的不同问题,而将这三者整合在一起可以构建高效、灵活的企业级应用。Struts2作为MVC(Model-View-Controller)框架,负责处理用户请求...
struts2.2.3-spring3.0.6-hibernate3.6.6 二、Demo功能(导入sql可直接运行) 0.数据库文件doc/db.sql 1.引入Spring全注解 2.抽取业务/持久层通用代码(UniversalService/UniversalDao),避免重复编写 比如有实体:...
《精通J2EE--Eclipse、Struts、Hibernate及Spring整合应用案例》是针对Java企业级开发的一个深度学习资源,涵盖了四个核心的技术领域:Eclipse集成开发环境、Struts MVC框架、Hibernate对象关系映射工具以及Spring...
struts2.2.3-spring3.0.6-hibernate3.6.6 二、Demo功能(导入sql可直接运行) 1.数据库文件doc/db.sql 2.全注解,实现零配置 3.对Service/Dao层做了封装: 实现了对任何通用CRUD(增删改查)操作,不用写任何Service/...
在“Struts2+Spring3+Hibernate4+Maven+EasyUI整合入门视频002”中,可能涵盖了以下内容: 1. Maven项目配置:讲解如何创建Maven项目,配置POM.xml文件,引入所需的Struts2、Spring3、Hibernate4和EasyUI的依赖。 2...
**Spring-Struts-Hibernate ...通过这个整合,开发者可以享受到Spring、Struts2和Hibernate各自的优势,并通过Maven进行高效的项目管理。这种架构对于大型、复杂的Java Web应用来说,提供了良好的可维护性和扩展性。
Maven搭建spring_struts2_Hibernate项目源码,包括数据库,博客地址:http://blog.csdn.net/fengshizty/article/details/43794911
Struts2、Spring3、Hibernate4、Maven和EasyUI是Java Web开发中常见的开源框架和技术,它们在构建高效、可维护的企业级应用程序方面发挥着关键作用。本篇将详细介绍这些技术及其整合入门的知识点。 **Struts2** 是...
Struts2、Spring3、Hibernate4、Maven和EasyUI是Java Web开发中常见的开源框架和技术,它们在构建高效、可维护的企业级应用中发挥着关键作用。本教程源码结合了这些技术,旨在帮助开发者理解和掌握它们的整合与应用...
【轻量级Java_EE企业应用实战_Struts2_Spring3_Hibernate整合开发】这个主题主要涉及的是Java EE领域中的三大框架——Struts2、Spring3和Hibernate的集成应用,这是构建现代Java后端系统的一种常见方式。这些框架...