I did some projects based on Struts+Spring+Hibernate when I was in college, but the previous projects were developed using MyEclipse or Eclipse tools, and it was very troublesome to import dependent packages from outside. I wanted to renovate some SSH projects written by Zeng Jin, so I used IDEA+Maven to build and integrate them. It is mainly used for playing. I heard that Struts2 has a lot of loopholes. There have been several loophole accidents. I don’t know how to fix them. As for this technology, just take a look at it.
1. Create a Maven project
(1)GroupId:org.example
(2)ArtifactId:j2ee-struts2-spring4-hibernate4
(3)Project name:j2ee-struts2-spring4-hibernate4
(4)Project location:D:\idea_workspace\j2ee-struts2-spring4-hibernate4
2. Introduce SSH dependency library
(1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>j2ee-struts2-spring4-hibernate4</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- 在Maven编译时指定读取文件的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Spring 版本号 -->
<spring.version>4.0.3.RELEASE</spring.version>
<!-- Struts2 版本号 -->
<struts2.version>2.5.26</struts2.version>
<!-- Hibernate 版本号 -->
<hibernate.version>4.3.5.Final</hibernate.version>
<!-- Mybatis 版本号 -->
<mybatis.version>3.1.1</mybatis.version>
</properties>
<dependencies>
<!-- ^ Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- / Spring -->
<!-- ^ Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- / Struts2 -->
<!-- ^ Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- / Hibernate -->
<!-- ^ MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- / MyBatis -->
<!-- ^ J2EE -->
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- / J2EE -->
<!-- c3p0 数据源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
</dependencies>
<!-- 配置Maven的构建环境 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. Design directory structure
(1) Create relevant modules and design a good basic project structure
4. Create a new web.xml configuration file
(1) Create a new web.xml file in the j2ee-struts2-spring4-hibernate4/src/main/webapp/WEB_INF directory
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name>我爱你</display-name>
<welcome-file-list>
<!-- 若欢迎页不填写 index.jsp/welcome.jsp 等,则到 struts.xml 找默认的 action,若填写了 index.jsp/welcome.jsp 等,则优先级比 struts.xml 中默认的 action 还高 -->
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml,classpath:spring/**/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<!-- struts2 版本为 2.3.16.1 -->
<!-- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> -->
<!-- struts2 版本为 2.5.26 -->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<!-- 此处可以填写自定义的 struts-*.xml 配置文件,如:struts/struts-hi.xml,若不写亦可在 struts.xml 文件中使用 include 标签导入 -->
<param-value>platform-web.xml,struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(2) Remember to add web.xml file for Web in Project Structure
5. Create a new welcome.jsp welcome page
(1) Create a new web.xml file in the j2ee-struts2-spring4-hibernate4/src/main/webapp directory
<%--
Created by IntelliJ IDEA.
User: 帅龍之龍
Date: 2023/7/25
Time: 15:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div style="width: 100%; height: 100%; display: grid; align-items: center; text-align: center">
<span style="color: #303133; font-size: 14px;">你好,世界! ...</span>
</div>
</body>
</html>
6. Create four new jsp test pages
(1) In the j2ee-struts2-spring4-hibernate4/src/main/webapp/view/jsp directory, create success.jsp, error.jsp, login.jsp, and home.jsp in order for testing
(2) success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: 帅龍之龍
Date: 2023/7/25
Time: 15:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div style="width: 100%; height: 100%; display: grid; align-items: center; text-align: center">
<p>
<span style="color: #303133; font-size: 14px;">你好,${request.username}</span>
</p>
</div>
</body>
</html>
(2)error.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: 帅龍之龍
Date: 2023/7/25
Time: 15:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language