原文: https://howtodoinjava.com/struts2/spring-4-struts-2-hibernate-integration-tutorial/

以前,我已经介绍了 Spring3 + Hibernate 集成示例和 Struts2 helloworld 示例。 在本教程中,我将讨论将 spring 框架与 Struts 和 Hibernate 结合使用时要记住的所有重要点。 另外,请注意,本教程使用了其他次要但很重要的概念,例如日志记录,TLD 的使用和事务以及已集成到本教程中。 因此,请记住还要检查其详细信息。

在本教程中,我将使用简单的功能(例如全部获取,添加和删除)来构建员工管理屏幕。 它看起来像这样:

Spring4   Struts2   Hibernate 集成教程 - 图1

我将按照以下步骤讨论集成过程:

  1. 1) Integration Overview
  2. 2) Spring + Struts Integration
  3. 3) Spring + Hibernate Integration
  4. 4) Other Integrated Functionalities
  5. a) Log4j
  6. b) TLDs
  7. c) Transactions
  8. 5) Important Points to Keep Remember
  9. 6) Database Schema Used in Tutorial
  10. 7) 下载源码

1)概述

在进入集成细节之前,让我们先确定一下为什么我们需要此集成本身。 像 Struts 一样,Spring 也可以充当 MVC 实现。 两种框架都有其优缺点,仍然有很多人会同意 Spring 更好,并且提供了更广泛的功能。 对我来说,只有两种情况,您需要本教程中提供的信息:

i)您有一个用 Struts 编写的旧应用,并且想要使用 spring 来提高应用的功能很多倍。

ii)您确实想根据自己的原因来学习它。

否则,我不知道为什么有人会在 Spring 选择支柱。 如果您知道其他一些好的理由,请与我们所有人分享。 那挺棒的。

继续,在本教程中,我将委托从 Struts 到 Spring 进行动作管理。 进行委派的原因是,通过 Spring 上下文实例化Action类时,它可以使用 spring 在其自己的 MVC 实现中为其提供的Controller类的所有其他功能。 因此,您将获得所有 spring 功能以及 struts Action类,以具有包括ActionForm概念在内的控制器逻辑。

2)Spring + Struts 集成

这是核心逻辑,从在web.xml中注册ContextLoaderListenerStrutsPrepareAndExecuteFilter开始。 ContextLoaderListener带有初始化参数contextConfigLocation,并负责设置和启动 Spring WebApplicationContext。 现在,struts 将在与 Spring 相关的服务中特别是在依赖项注入 and dependency injection (DI) patterns in spring framework and related interview questions”)中利用此上下文。

StrutsPrepareAndExecuteFilter在类路径中查找struts.xml文件,并配置 strut 的特定内容,例如动作映射,全局转发和其他在struts.xml文件中定义的内容。

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://www.oracle.com/technetwork/java/index.html; id="WebApp_ID" version="2.5">
  6. <display-name>Spring+Struts+Hibernate Integration Example</display-name>
  7. <welcome-file-list>
  8. <welcome-file>/WEB-INF/index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- Specify the spring context information location;
  11. Default location is applicationContext.xml file in classpath
  12. -->
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>classpath:beans.xml</param-value>
  16. </context-param>
  17. <!-- Bootstrap listener to start up and shut down Spring's root WebApplicationContext. -->
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. <!-- Handles both the preparation and execution phases of the Struts dispatching process. -->
  22. <filter>
  23. <filter-name>struts2</filter-name>
  24. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  25. <init-param>
  26. <param-name>debug</param-name>
  27. <param-value>0</param-value>
  28. </init-param>
  29. <init-param>
  30. <param-name>detail</param-name>
  31. <param-value>0</param-value>
  32. </init-param>
  33. </filter>
  34. <filter-mapping>
  35. <filter-name>struts2</filter-name>
  36. <url-pattern>/*</url-pattern>
  37. </filter-mapping>
  38. <!-- Struts Tag Library Descriptors -->
  39. <jsp-config>
  40. <taglib>
  41. <taglib-uri>/tags/struts-bean</taglib-uri>
  42. <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  43. </taglib>
  44. <taglib>
  45. <taglib-uri>/tags/struts-html</taglib-uri>
  46. <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  47. </taglib>
  48. <taglib>
  49. <taglib-uri>/tags/struts-logic</taglib-uri>
  50. <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  51. </taglib>
  52. <taglib>
  53. <taglib-uri>/tags/struts-nested</taglib-uri>
  54. <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  55. </taglib>
  56. </jsp-config>
  57. </web-app>

第二步,将在struts.xml文件中创建操作映射,如下所示:

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <!-- devMode is helpful when you want some extra logs for debugging -->
  7. <constant name="struts.devMode" value="false" />
  8. <!-- Global message resource;
  9. Otherwise you will have seperate message resource for each Action
  10. -->
  11. <constant name="struts.custom.i18n.resources" value="messages" />
  12. <!--
  13. This is important if you are planning to have slashes in Action URLs
  14. e.g. In this demo, employee is deleted using URL /delete/10
  15. This this is set to false; then struts will try to find mapping for
  16. URL "/10" instaed of "/delete/10"
  17. -->
  18. <constant name="struts.enable.SlashesInActionNames" value="true"/>
  19. <!-- Normal Action mappings are defined here -->
  20. <package name="default" namespace="" extends="struts-default">
  21. <!-- Two things to Notice:
  22. 1) class is set to 'editEmployeeAction' which is bean defined by Spring context
  23. 2) We have given the method to be called here as well;
  24. -->
  25. <action name="list" class="editEmployeeAction" method="listEmployees">
  26. <result>/view/editEmployeeList.jsp</result>
  27. </action>
  28. <action name="add" class="editEmployeeAction" method="addEmployee">
  29. <result type="redirect">/list</result>
  30. </action>
  31. <action name="delete/*" class="editEmployeeAction" method="deleteEmployee">
  32. <param name="employee.id">{1}</param>
  33. <result type="redirect">/list</result>
  34. </action>
  35. <action name="*" class="editEmployeeAction" method="listEmployees">
  36. <result>/view/editEmployeeList.jsp</result>
  37. </action>
  38. </package>
  39. </struts>

在单独的 strut 应用程序中,我们将在“class”属性中具有完整的Action类及其包装信息。 在这里,我们将类名命名为editEmployeeAction。 它在哪里定义? 我们将要求 Spring 为我们查找。

Spring 上下文文件beans.xml是典型的 Spring 单独上下文文件,具有 Web 应用运行所需的所有内容,其中包括 struts 正在寻找的 bean 定义editEmployeeAction

beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:jee="http://www.springframework.org/schema/jee"
  7. xmlns:lang="http://www.springframework.org/schema/lang"
  8. xmlns:p="http://www.springframework.org/schema/p"
  9. xmlns:tx="http://www.springframework.org/schema/tx"
  10. xmlns:util="http://www.springframework.org/schema/util"
  11. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  12. http://www.springframework.org/schema/aop/ http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
  14. http://www.springframework.org/schema/jee/ http://www.springframework.org/schema/jee/spring-jee.xsd
  15. http://www.springframework.org/schema/lang/ http://www.springframework.org/schema/lang/spring-lang.xsd
  16. http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx.xsd
  17. http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd">
  18. <!-- This bean has been referred fron struts.xml file; So type it correctly; -->
  19. <!-- Make scope prototype; This is really important. -->
  20. <bean name="editEmployeeAction" class="com.howtodoinjava.controller.EditEmployeeAction" scope="prototype">
  21. <property name="employeeManager">
  22. <ref bean="employeeManager"/>
  23. </property>
  24. </bean>
  25. <!-- These beans are injected automatically by spring context -->
  26. <bean id="employeeDAO" class="com.howtodoinjava.dao.EmployeeDaoImpl">
  27. <property name="sessionFactory" ref="sessionFactory"/>
  28. </bean>
  29. <bean id="employeeManager" class="com.howtodoinjava.service.EmployeeManagerImpl">
  30. <property name="employeeDAO" ref="employeeDAO"/>
  31. </bean>
  32. <!-- Configure jdbc.properties -->
  33. <bean id="propertyConfigurer"
  34. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
  35. p:location="/WEB-INF/jdbc.properties" />
  36. <!-- Data Source configuration -->
  37. <bean id="dataSource"
  38. class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
  39. p:driverClassName="${jdbc.driverClassName}"
  40. p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
  41. p:password="${jdbc.password}" />
  42. <!-- Configure hibernate session factory -->
  43. <bean id="sessionFactory"
  44. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  45. <property name="dataSource" ref="dataSource" />
  46. <property name="configLocation">
  47. <value>classpath:hibernate.cfg.xml</value>
  48. </property>
  49. <property name="configurationClass">
  50. <value>org.hibernate.cfg.AnnotationConfiguration</value>
  51. </property>
  52. <property name="hibernateProperties">
  53. <props>
  54. <prop key="hibernate.dialect">${jdbc.dialect}</prop>
  55. <prop key="hibernate.show_sql">true</prop>
  56. </props>
  57. </property>
  58. </bean>
  59. <!-- Run SQL queries in transactions -->
  60. <tx:annotation-driven />
  61. <bean id="transactionManager"
  62. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  63. <property name="sessionFactory" ref="sessionFactory" />
  64. </bean>
  65. </beans>

这是我们要做的将 Struts 与 spring 框架集成在一起的所有步骤。 现在,您的动作类如下所示:

EditEmployeeAction.java

  1. package com.howtodoinjava.controller;
  2. import java.util.List;
  3. import org.apache.log4j.Logger;
  4. import com.howtodoinjava.entity.EmployeeEntity;
  5. import com.howtodoinjava.service.EmployeeManager;
  6. import com.opensymphony.xwork2.ActionSupport;
  7. import com.opensymphony.xwork2.Preparable;
  8. public class EditEmployeeAction extends ActionSupport implements Preparable
  9. {
  10. private static final long serialVersionUID = 1L;
  11. //Logger configured using log4j
  12. private static final Logger logger = Logger.getLogger(EditEmployeeAction.class);
  13. //List of employees; Setter and Getter are below
  14. private List<EmployeeEntity> employees;
  15. //Employee object to be added; Setter and Getter are below
  16. private EmployeeEntity employee;
  17. //Employee manager injected by spring context; This is cool !!
  18. private EmployeeManager employeeManager;
  19. //This method return list of employees in database
  20. public String listEmployees() {
  21. logger.info("listEmployees method called");
  22. employees = employeeManager.getAllEmployees();
  23. return SUCCESS;
  24. }
  25. //This method will be called when a employee object is added
  26. public String addEmployee() {
  27. logger.info("addEmployee method called");
  28. employeeManager.addEmployee(employee);
  29. return SUCCESS;
  30. }
  31. //Deletes a employee by it's id passed in path parameter
  32. public String deleteEmployee() {
  33. logger.info("deleteEmployee method called");
  34. employeeManager.deleteEmployee(employee.getId());
  35. return SUCCESS;
  36. }
  37. //This method will be called before any of Action method is invoked;
  38. //So some pre-processing if required.
  39. @Override
  40. public void prepare() throws Exception {
  41. employee = null;
  42. }
  43. //Getters and Setters hidden
  44. }

Spring 还将 DAO 引用注入到Manager类。

EmployeeManagerImpl.java

  1. package com.howtodoinjava.service;
  2. import java.util.List;
  3. import org.springframework.transaction.annotation.Transactional;
  4. import com.howtodoinjava.dao.EmployeeDAO;
  5. import com.howtodoinjava.entity.EmployeeEntity;
  6. public class EmployeeManagerImpl implements EmployeeManager
  7. {
  8. //Employee dao injected by Spring context
  9. private EmployeeDAO employeeDAO;
  10. //This method will be called when a employee object is added
  11. @Override
  12. @Transactional
  13. public void addEmployee(EmployeeEntity employee) {
  14. employeeDAO.addEmployee(employee);
  15. }
  16. //This method return list of employees in database
  17. @Override
  18. @Transactional
  19. public List<EmployeeEntity> getAllEmployees() {
  20. return employeeDAO.getAllEmployees();
  21. }
  22. //Deletes a employee by it's id
  23. @Override
  24. @Transactional
  25. public void deleteEmployee(Integer employeeId) {
  26. employeeDAO.deleteEmployee(employeeId);
  27. }
  28. //This setter will be used by Spring context to inject the dao's instance
  29. public void setEmployeeDAO(EmployeeDAO employeeDAO) {
  30. this.employeeDAO = employeeDAO;
  31. }
  32. }

3)Spring + Hibernate 集成

现在我们必须将 Hibernate 集成到应用中。 最好的地方是利用 Spring 的强大功能进行集成,以充分利用依赖注入来与不同的 ORM 一起使用。

上面beans.xml文件中已经给出了 Spring 所需的 Hibernate 依赖关系。 您将需要的其他文件是:

hibernate.cfg.xml

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <mapping class="com.howtodoinjava.entity.EmployeeEntity" />
  8. </session-factory>
  9. </hibernate-configuration>

jdbc.properties

  1. jdbc.driverClassName=com.mysql.jdbc.Driver
  2. jdbc.dialect=org.hibernate.dialect.MySQLDialect
  3. jdbc.databaseurl=jdbc:mysql://127.0.0.1:3306/test
  4. jdbc.username=root
  5. jdbc.password=password

EmployeeDaoImpl.java

  1. package com.howtodoinjava.dao;
  2. import java.util.List;
  3. import org.hibernate.SessionFactory;
  4. import org.springframework.stereotype.Repository;
  5. import com.howtodoinjava.entity.EmployeeEntity;
  6. @Repository
  7. public class EmployeeDaoImpl implements EmployeeDAO
  8. {
  9. //Session factory injected by spring context
  10. private SessionFactory sessionFactory;
  11. //This method will be called when a employee object is added
  12. @Override
  13. public void addEmployee(EmployeeEntity employee) {
  14. this.sessionFactory.getCurrentSession().save(employee);
  15. }
  16. //This method return list of employees in database
  17. @SuppressWarnings("unchecked")
  18. @Override
  19. public List<EmployeeEntity> getAllEmployees() {
  20. return this.sessionFactory.getCurrentSession().createQuery("from EmployeeEntity").list();
  21. }
  22. //Deletes a employee by it's id
  23. @Override
  24. public void deleteEmployee(Integer employeeId) {
  25. EmployeeEntity employee = (EmployeeEntity) sessionFactory.getCurrentSession()
  26. .load(EmployeeEntity.class, employeeId);
  27. if (null != employee) {
  28. this.sessionFactory.getCurrentSession().delete(employee);
  29. }
  30. }
  31. //This setter will be used by Spring context to inject the sessionFactory instance
  32. public void setSessionFactory(SessionFactory sessionFactory) {
  33. this.sessionFactory = sessionFactory;
  34. }
  35. }

供您参考,EmployeeEntity类如下所示:

EmployeeEntity.java

  1. package com.howtodoinjava.entity;
  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.Id;
  6. import javax.persistence.Table;
  7. @Entity
  8. @Table(name="EMPLOYEE")
  9. public class EmployeeEntity {
  10. @Id
  11. @Column(name="ID")
  12. @GeneratedValue
  13. private Integer id;
  14. @Column(name="FIRSTNAME")
  15. private String firstname;
  16. @Column(name="LASTNAME")
  17. private String lastname;
  18. @Column(name="EMAIL")
  19. private String email;
  20. @Column(name="TELEPHONE")
  21. private String telephone;
  22. //Setters and Getters
  23. }

4)其他集成功能

除了 struts + spring + hibernate,我们还使用以下组件来构建应用。

a)Log4j

Spring 通过扫描类路径中的 log4j 自动配置日志记录。 我们使用pom.xml文件添加了 log4j 依赖项。 现在,您只需要在类路径中放置一个log4j.xmllog4j.properties文件。

log4j.properties

  1. log4j.rootLogger=info, stdout, R
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  5. log4j.appender.R=org.apache.log4j.RollingFileAppender
  6. log4j.appender.R.File=c:/log/demo.log
  7. log4j.appender.R.MaxFileSize=100KB
  8. log4j.appender.R.MaxBackupIndex=1
  9. log4j.appender.R.layout=org.apache.log4j.PatternLayout
  10. log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

b)顶级域

如果您查看web.xml文件,我们在其中包含了一些 TLD。 我们可以随时在视图层中使用它们,如下所示:

editEmployeeList.jsp

  1. <%@ taglib prefix="s" uri="/struts-tags"%>
  2. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <html>
  4. <head>
  5. <title>Spring-4 + Struts-3 + Hibernate Integration Demo</title>
  6. <style>
  7. table.list
  8. {
  9. border-collapse:collapse;
  10. width: 40%;
  11. }
  12. table.list, table.list td, table.list th
  13. {
  14. border:1px solid gray;
  15. padding: 5px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <h2>Spring-4 + Struts-3 + Hibernate Integration Demo</h2>
  21. <s:form method="post" action="add">
  22. <table>
  23. <tr>
  24. <td><s:textfield key="label.firstname" name="employee.firstname"/></td>
  25. </tr>
  26. <tr>
  27. <td><s:textfield key="label.lastname" name="employee.lastname"/></td>
  28. </tr>
  29. <tr>
  30. <td><s:textfield key="label.email" name="employee.email"/></td>
  31. </tr>
  32. <tr>
  33. <td><s:textfield key="label.telephone" name="employee.telephone"/></td>
  34. </tr>
  35. <tr>
  36. <td>
  37. <s:submit key="label.add"></s:submit>
  38. </td>
  39. </tr>
  40. </table>
  41. </s:form>
  42. <h3>Employees</h3>
  43. <c:if test="${!empty employees}">
  44. <table class="list">
  45. <tr>
  46. <th align="left">Name</th>
  47. <th align="left">Email</th>
  48. <th align="left">Telephone</th>
  49. <th align="left">Actions</th>
  50. </tr>
  51. <c:forEach items="${employees}" var="emp">
  52. <tr>
  53. <td>${emp.lastname}, ${emp.firstname} </td>
  54. <td>${emp.email}</td>
  55. <td>${emp.telephone}</td>
  56. <td><a href="delete/${emp.id}">delete</a></td>
  57. </tr>
  58. </c:forEach>
  59. </table>
  60. </c:if>
  61. </body>
  62. </html>

c)事务

EmployeeManagerImpl.java在诸如getAllEmployees()deleteEmployee()之类的方法中使用注解@Transactional。 这实际上是在单个事务中运行在此方法下执行的所有数据库查询。 像这样在beans.xml上下文文件中声明了事务依赖项。

  1. <!-- Run SQL queries in transactions -->
  2. <tx:annotation-driven />
  3. <bean id="transactionManager"
  4. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  5. <property name="sessionFactory" ref="sessionFactory" />
  6. </bean>

5)要记住的要点

a)如果运行时无法找到lib文件夹中存在的类,则将来自项目依赖项的 jar 文件添加到项目部署程序集中。

b)使bean.xml中定义的Action类的作用域为“原型”。 Spring 提供的 bean 的默认范围是单例,并且必须为每个请求创建新的 Struts Action类,因为它包含特定于用户会话的数据。 为了容纳两者,请将Action类 bean 标记为原型。

c)在运行此应用之前,请不要忘记设置数据库。 如果安装不正确,将导致您出现一些异常。

d)另外,请在项目运行时依赖项中也包括struts2-spring-plugin

6)教程中使用的数据库架构

下表已在 MySQL 中的名为“test”的数据库中创建。

  1. CREATE TABLE EMPLOYEE
  2. (
  3. ID INT PRIMARY KEY AUTO_INCREMENT,
  4. FIRSTNAME VARCHAR(30),
  5. LASTNAME VARCHAR(30),
  6. TELEPHONE VARCHAR(15),
  7. EMAIL VARCHAR(30),
  8. CREATED TIMESTAMP DEFAULT NOW()
  9. );

如果您打算自己构建应用,则在下面给定的直接在此应用中使用的结构将为您提供帮助。

Spring4   Struts2   Hibernate 集成教程 - 图2

Spring Struts Hibernate 集成的目录结构

此项目中使用的pom.xml文件具有所有项目相关性(有些额外),如下所示:

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.howtodoinjava.app</groupId>
  5. <artifactId>Spring4Struts2HibernateIntegration</artifactId>
  6. <packaging>war</packaging>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>Spring4Struts2HibernateIntegration Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- JBoss repository for Hibernate -->
  11. <repositories>
  12. <repository>
  13. <id>JBoss repository</id>
  14. <url>http://repository.jboss.org/nexus/content/groups/public/</url>
  15. </repository>
  16. </repositories>
  17. <properties>
  18. <org.springframework.version>4.0.3.RELEASE</org.springframework.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>junit</groupId>
  23. <artifactId>junit</artifactId>
  24. <version>4.11</version>
  25. <scope>test</scope>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.apache.struts</groupId>
  29. <artifactId>struts2-core</artifactId>
  30. <version>2.3.16.2</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.apache.struts</groupId>
  34. <artifactId>struts2-spring-plugin</artifactId>
  35. <version>2.3.16.2</version>
  36. </dependency>
  37. <!--
  38. Core utilities used by other modules.
  39. Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*)
  40. -->
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-core</artifactId>
  44. <version>${org.springframework.version}</version>
  45. <scope>runtime</scope>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework</groupId>
  49. <artifactId>spring-web</artifactId>
  50. <version>${org.springframework.version}</version>
  51. <scope>runtime</scope>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework</groupId>
  55. <artifactId>spring-webmvc</artifactId>
  56. <version>${org.springframework.version}</version>
  57. <scope>runtime</scope>
  58. </dependency>
  59. <!--
  60. Bean Factory and JavaBeans utilities (depends on spring-core)
  61. Define this if you use Spring Bean APIs (org.springframework.beans.*)
  62. -->
  63. <dependency>
  64. <groupId>org.springframework</groupId>
  65. <artifactId>spring-beans</artifactId>
  66. <version>${org.springframework.version}</version>
  67. </dependency>
  68. <!--
  69. Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans)
  70. This is the central artifact for Spring's Dependency Injection Container and is generally always defined
  71. -->
  72. <dependency>
  73. <groupId>org.springframework</groupId>
  74. <artifactId>spring-context</artifactId>
  75. <version>${org.springframework.version}</version>
  76. </dependency>
  77. <!--
  78. Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans)
  79. Define this if you use Spring AOP APIs (org.springframework.aop.*)
  80. -->
  81. <dependency>
  82. <groupId>org.springframework</groupId>
  83. <artifactId>spring-aop</artifactId>
  84. <version>${org.springframework.version}</version>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.aspectj</groupId>
  88. <artifactId>aspectjtools</artifactId>
  89. <version>1.6.2</version>
  90. </dependency>
  91. <dependency>
  92. <groupId>cglib</groupId>
  93. <artifactId>cglib</artifactId>
  94. <version>3.1</version>
  95. </dependency>
  96. <!--
  97. Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context)
  98. Define this if you use Spring Transactions or DAO Exception Hierarchy
  99. (org.springframework.transaction.*/org.springframework.dao.*)
  100. -->
  101. <dependency>
  102. <groupId>org.springframework</groupId>
  103. <artifactId>spring-tx</artifactId>
  104. <version>${org.springframework.version}</version>
  105. </dependency>
  106. <!--
  107. JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx)
  108. Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*)
  109. -->
  110. <dependency>
  111. <groupId>org.springframework</groupId>
  112. <artifactId>spring-jdbc</artifactId>
  113. <version>${org.springframework.version}</version>
  114. </dependency>
  115. <!--
  116. Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis.
  117. (depends on spring-core, spring-beans, spring-context, spring-tx)
  118. Define this if you need ORM (org.springframework.orm.*)
  119. -->
  120. <dependency>
  121. <groupId>org.springframework</groupId>
  122. <artifactId>spring-orm</artifactId>
  123. <version>${org.springframework.version}</version>
  124. </dependency>
  125. <dependency>
  126. <groupId>log4j</groupId>
  127. <artifactId>log4j</artifactId>
  128. <version>1.2.15</version>
  129. <exclusions>
  130. <exclusion>
  131. <groupId>javax.mail</groupId>
  132. <artifactId>mail</artifactId>
  133. </exclusion>
  134. <exclusion>
  135. <groupId>javax.jms</groupId>
  136. <artifactId>jms</artifactId>
  137. </exclusion>
  138. <exclusion>
  139. <groupId>com.sun.jdmk</groupId>
  140. <artifactId>jmxtools</artifactId>
  141. </exclusion>
  142. <exclusion>
  143. <groupId>com.sun.jmx</groupId>
  144. <artifactId>jmxri</artifactId>
  145. </exclusion>
  146. </exclusions>
  147. <scope>runtime</scope>
  148. </dependency>
  149. <dependency>
  150. <groupId>org.hibernate</groupId>
  151. <artifactId>hibernate-core</artifactId>
  152. <version>3.6.3.Final</version>
  153. </dependency>
  154. <dependency>
  155. <groupId>javassist</groupId>
  156. <artifactId>javassist</artifactId>
  157. <version>3.12.1.GA</version>
  158. </dependency>
  159. <dependency>
  160. <groupId>javax.servlet</groupId>
  161. <artifactId>jstl</artifactId>
  162. <version>1.2</version>
  163. <scope>runtime</scope>
  164. </dependency>
  165. <dependency>
  166. <groupId>taglibs</groupId>
  167. <artifactId>standard</artifactId>
  168. <version>1.1.2</version>
  169. <scope>runtime</scope>
  170. </dependency>
  171. <dependency>
  172. <groupId>commons-dbcp</groupId>
  173. <artifactId>commons-dbcp</artifactId>
  174. <version>1.4</version>
  175. </dependency>
  176. <dependency>
  177. <groupId>mysql</groupId>
  178. <artifactId>mysql-connector-java</artifactId>
  179. <version>5.1.9</version>
  180. </dependency>
  181. </dependencies>
  182. <build>
  183. <finalName>Spring3Struts2HibernateIntegration</finalName>
  184. <plugins>
  185. <plugin>
  186. <artifactId>maven-compiler-plugin</artifactId>
  187. <version>2.3.2</version>
  188. <configuration>
  189. <source>1.6</source>
  190. <target>1.6</target>
  191. </configuration>
  192. </plugin>
  193. </plugins>
  194. </build>
  195. </project>

7)下载源代码

下载以上示例的源代码或获取.war文件。

下载源码

下载.war文件

这是 Spring4 + Struts2 + hibernate 集成教程的全部内容。 让我知道您的想法和疑问。

祝您学习愉快!