大学时做过一些基于Struts+Spring+Hibernate的项目,但是以前的项目是使用MyEclipse或者Eclipse工具来开发的,而且还是外部引入依赖包,甚是麻烦。想着翻新一下曾今写的一些SSH项目,于是使用IDEA+Maven来搭建整合一下。主要就是拿来玩的,听说Struts2挺多漏洞的,有过几次漏洞事故,不知修复得怎么样,至于这个技术,大家就简单看看就好了。
一、创建一个Maven项目
(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
二、引入SSH依赖库
(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>
三、设计目录结构
(1)创建好相关模块,设计出一个良好的项目基本结构
四、新建一个 web.xml 配置文件
(1)在 j2ee-struts2-spring4-hibernate4/src/main/webapp/WEB_INF 目录中新建 web.xml 文件
<?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)记得在 Project Structure 中为 Web 添加 web.xml 文件
五、新建一个 welcome.jsp 欢迎页面
(1)在 j2ee-struts2-spring4-hibernate4/src/main/webapp 目录中新建 web.xml 文件
<%--
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>
六、新建四个 jsp 测试页面
(1)在 j2ee-struts2-spring4-hibernate4/src/main/webapp/view/jsp 目录中依次创建 success.jsp、error.jsp、login.jsp、home.jsp 作为测试使用
(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&