Springboot3.0+spring6.0+JDK17,如何配置JSP并打包成WAR包?
- 内容介绍
- 文章标签
- 相关推荐
本文共计4267个文字,预计阅读时间需要18分钟。
由于某些特殊情况,公司产品需要升级,但又不想花费大量时间修改前端代码(原本的代码前后端并未分离)。因此,尽管Spring和Spring Boot都已升级到最新版本,但仍需支持JSP。
由于某些缘故,公司的产品需要升级,但并不希望花费大量时间重写前端代码(原来的就不是前后分离的)。所以虽然spring和springboot都升级为最新的版本,但是依然还是需要支持jsp,并继续用打包为war。
本文中的例子百分百可以执行。
一、概述升级的理由:
- java1.8已经用得太久了,就快不是长期优先支持的。毕竟1.8已经用了快10年了,该抛弃了
- java17,对于性能的提升是明显的,至少有关资料是这么说的,其次是被优先支持的
- 有关三方技术也渐渐转向jdk17了,例如VSCODE,KAFKA。eclise,sts也不再优先支持1.8
- 团队需要升级技术,不能老是在1.8中打转
- spring5.x已经用太久了,而且有不少缺点;原来的诸多配置需要多个xml文件,有点小繁琐。
- 其它理由
- 老版本的产品依然继续支持
保持war包理由:
- 每次更新的时候,可能就是替换几个class和前端页面,所以没有必要每次拷贝几百M的可执行代码
- 可以更容易调整tomcat的配置,把tomcat的配置从代码中脱离出来。
保持JSP的理由:
- 暂时不想花费大量时间更新前端页面,前端代码量比较大
- jsp没有什么大毛病,虽然spring并不是很推荐它。但spring最新版本,以及tomcat最新版本依然支持着。
- jsp技术,某种意义上,还是比较节约成本的。
本文涉及到几个知识点:
- servlet
- 动态web项目
工具和环境:
- 操作系统:windows 11 家庭版
- ide:使用sts即可。本人使用的是Version: 4.14.1.RELEASE版本。
- 容器:tomcat使用最新的10.0.22
- jdk-17
- jquery-3.6.0
本文的例子就是为了升级做准备的,看是否可以比较容易地实现。
大体步骤如下:
- 使用sts,选择"spring starter project ",之后选择有关组件和选项。其中打包的选项选择"war",java选择17。注意,java8依然可以支持
- 修改pom.xml-支持jsp和war包
- 修改启动代码(不修改也可以)
如果不想使用sts,仅仅是想看看有关配置,可以通过start.spring.io/在线生成示例。
以下几个图分别是目录结构图,项目配置信息图。
2.1.1目录结构图修改后的启动代码(原来例子是两个类,现在合并为一个,不合并也可以):
package com.hc.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class SpringbootJspApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringbootJspApplication.class); } public static void main(String[] args) { SpringApplication.run(SpringbootJspApplication.class, args); } }
这里最关键的是 SpringBootServletInitializer 。
2.1.2项目配置信息-部署这里的关键是 src/main/webapp目录会映射到/,这个src/main/webapp就是用于存放前端代码的。当然,你要愿意修改为webContent,myhtml之类都可以,只要做好部署映射即可。
其它build path,java compiler略过。
2.1.3项目配置信息-project facets注意:”dynamic web module"不要选择太老的,选择5.0即可。
这个"dynamic web mouule 5.0“意味着可以支持:
2.1.4项目配置信息-配置web上下文首先打开"servers"视图,然后添加tomcat10,之后就可以设置上下文:
点击“edit”可以设置上下文。根据项目配置不同和sts的配置不同,有的时候,也可以在项目设置页面中直接设置上下文。
2.2修改pom.xml这里是关键,以下是本例的pom.xml的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hc.demo</groupId>
<artifactId>springjsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springjsp</name>
<description>Demo project for Spring boot and jsp and war</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 以下1~4是为了支持jsp -->
<!-- 1添加servlet依赖模块 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>10.0.22</version>
</dependency>
<!-- 2添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--3添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- 4使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 配置war包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>src/main/resources/**</warSourceExcludes>
<warName>springjsp</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
主要是两个地方要修改:
- 配置jsp
- 配置war插件
以上两个修改,分类如以下两个地方:
2.2.1配置jsp<!-- 以下1~4是为了支持jsp -->
<!-- 1添加servlet依赖模块 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>10.0.22</version>
</dependency>
<!-- 2添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--3添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- 4使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>compile</scope>
</dependency>
此外,因为这里引入了"spring-boot-starter-tomcat",所以需要把它从“spring-boot-starter-web”移除掉,具体见完整pom.xml。
2.2.2配置war插件<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warSourceExcludes>src/main/resources/**</warSourceExcludes> <warName>springjsp</warName> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build>
具体过程略,一些列出过滤器代码:
package com.hc.demo.config.filter; import java.io.IOException; import org.springframework.web.filter.OncePerRequestFilter; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.repo.spring.io/milestone/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom (5.3 kB at 1.5 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom (9.9 kB at 1.8 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/apache/maven/maven-parent/35/maven-parent-35.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/apache/maven/maven-parent/35/maven-parent-35.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/apache/maven/maven-parent/35/maven-parent-35.pom (45 kB at 4.2 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/apache/apache/25/apache-25.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/apache/apache/25/apache-25.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/apache/apache/25/apache-25.pom (21 kB at 5.5 kB/s) [INFO] [INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ springjsp --- [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar (169 kB at 7.0 kB/s) [INFO] Deleting E:\codes-java\git\learning\gitee\springjsp\target [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ springjsp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ springjsp --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 7 source files to E:\codes-java\git\learning\gitee\springjsp\target\classes [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ springjsp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ springjsp --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 7 source files to E:\codes-java\git\learning\gitee\springjsp\target\classes [INFO] [INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ springjsp --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ springjsp --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ springjsp --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-war-plugin:3.3.2:war (default-war) @ springjsp --- [INFO] Packaging webapp [INFO] Assembling webapp [springjsp] in [E:\codes-java\git\learning\gitee\springjsp\target\springjsp-0.0.1-SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [E:\codes-java\git\learning\gitee\springjsp\src\main\webapp] [INFO] Building war: E:\codes-java\git\learning\gitee\springjsp\target\springjsp.war [INFO] [INFO] --- spring-boot-maven-plugin:3.0.0-SNAPSHOT:repackage (repackage) @ springjsp --- [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/maven-metadata.xml [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/maven-metadata.xml (2.4 kB at 1.5 kB/s) [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/spring-boot-buildpack-platform-3.0.0-20220626.095848-608.pom [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/spring-boot-buildpack-platform-3.0.0-20220626.095848-608.pom (3.4 kB at 2.5 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.pom (0 B at 0 B/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/net/java/dev/jna/jna/5.7.0/jna-5.7.0.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.pom (0 B at 0 B/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.pom (0 B at 0 B/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.pom (0 B at 0 B/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/antlr/antlr4-master/4.7.2/antlr4-master-4.7.2.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/antlr/antlr4-master/4.7.2/antlr4-master-4.7.2.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/antlr/antlr4-master/4.7.2/antlr4-master-4.7.2.pom (0 B at 0 B/s) [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/maven-metadata.xml [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/maven-metadata.xml (2.4 kB at 1.4 kB/s) [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/spring-boot-loader-tools-3.0.0-20220626.095848-608.pom [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/spring-boot-loader-tools-3.0.0-20220626.095848-608.pom (2.3 kB at 1.4 kB/s) [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/spring-boot-buildpack-platform-3.0.0-20220626.095848-608.jar [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/spring-boot-loader-tools-3.0.0-20220626.095848-608.jar [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/spring-boot-loader-tools-3.0.0-20220626.095848-608.jar (247 kB at 39 kB/s) [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/spring-boot-buildpack-platform-3.0.0-20220626.095848-608.jar (240 kB at 35 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.jar [INFO] Downloading from spring-milestones: repo.spring.io/milestone/net/java/dev/jna/jna/5.7.0/jna-5.7.0.jar [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar (0 B at 0 B/s) [INFO] Downloaded from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.jar (0 B at 0 B/s) [INFO] Downloaded from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.jar (0 B at 0 B/s) [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar (0 B at 0 B/s) [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar (268 kB at 38 kB/s) [INFO] Replacing main artifact with repackaged archive [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:31 min [INFO] Finished at: 2022-07-04T22:06:59+08:00 [INFO] ------------------------------------------------------------------------
五、测试 5.1安装和配置tomcat10安装过程略。
主要是设置JAVA_HOME和日志编码。
5.1.1设置JAVA_HOME打开TOMCAT10的bin\catalina.bat,在”setlocal"下插入一句:
set JAVA_HOME=D:\soft\develop-tool\java\jdk-17.0.3.1
5.1.2设置日志编码打开tomcat10\conf\logging.properties,把所有的“UTF-8”修改为“GBK"即可。
5.2部署war包把前面打包的E:\codes-java\git\learning\gitee\springjsp\target\springjsp.war复制到tomcat10\webapps下。
5.3运行war包在cmd下,运行tomcat10下的startup.bat,当然之前,需要先打开Mysql。
控制台输出如下:
看起来很正常,默认8080已经打开。
切换到浏览器,输入:localhost:8080/springjsp/
结果如下图:
运行 正常。
六、小结整个过程很顺利,麻烦的地方仅仅是配置pom.xml和在wms中配置静态资源。
和以前版本比起来,的确方便了不少。例如不要配置web.xml(实际上web module>=3.1即可),这样可以节省servlet,filter之类的配置。
本文共计4267个文字,预计阅读时间需要18分钟。
由于某些特殊情况,公司产品需要升级,但又不想花费大量时间修改前端代码(原本的代码前后端并未分离)。因此,尽管Spring和Spring Boot都已升级到最新版本,但仍需支持JSP。
由于某些缘故,公司的产品需要升级,但并不希望花费大量时间重写前端代码(原来的就不是前后分离的)。所以虽然spring和springboot都升级为最新的版本,但是依然还是需要支持jsp,并继续用打包为war。
本文中的例子百分百可以执行。
一、概述升级的理由:
- java1.8已经用得太久了,就快不是长期优先支持的。毕竟1.8已经用了快10年了,该抛弃了
- java17,对于性能的提升是明显的,至少有关资料是这么说的,其次是被优先支持的
- 有关三方技术也渐渐转向jdk17了,例如VSCODE,KAFKA。eclise,sts也不再优先支持1.8
- 团队需要升级技术,不能老是在1.8中打转
- spring5.x已经用太久了,而且有不少缺点;原来的诸多配置需要多个xml文件,有点小繁琐。
- 其它理由
- 老版本的产品依然继续支持
保持war包理由:
- 每次更新的时候,可能就是替换几个class和前端页面,所以没有必要每次拷贝几百M的可执行代码
- 可以更容易调整tomcat的配置,把tomcat的配置从代码中脱离出来。
保持JSP的理由:
- 暂时不想花费大量时间更新前端页面,前端代码量比较大
- jsp没有什么大毛病,虽然spring并不是很推荐它。但spring最新版本,以及tomcat最新版本依然支持着。
- jsp技术,某种意义上,还是比较节约成本的。
本文涉及到几个知识点:
- servlet
- 动态web项目
工具和环境:
- 操作系统:windows 11 家庭版
- ide:使用sts即可。本人使用的是Version: 4.14.1.RELEASE版本。
- 容器:tomcat使用最新的10.0.22
- jdk-17
- jquery-3.6.0
本文的例子就是为了升级做准备的,看是否可以比较容易地实现。
大体步骤如下:
- 使用sts,选择"spring starter project ",之后选择有关组件和选项。其中打包的选项选择"war",java选择17。注意,java8依然可以支持
- 修改pom.xml-支持jsp和war包
- 修改启动代码(不修改也可以)
如果不想使用sts,仅仅是想看看有关配置,可以通过start.spring.io/在线生成示例。
以下几个图分别是目录结构图,项目配置信息图。
2.1.1目录结构图修改后的启动代码(原来例子是两个类,现在合并为一个,不合并也可以):
package com.hc.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class SpringbootJspApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringbootJspApplication.class); } public static void main(String[] args) { SpringApplication.run(SpringbootJspApplication.class, args); } }
这里最关键的是 SpringBootServletInitializer 。
2.1.2项目配置信息-部署这里的关键是 src/main/webapp目录会映射到/,这个src/main/webapp就是用于存放前端代码的。当然,你要愿意修改为webContent,myhtml之类都可以,只要做好部署映射即可。
其它build path,java compiler略过。
2.1.3项目配置信息-project facets注意:”dynamic web module"不要选择太老的,选择5.0即可。
这个"dynamic web mouule 5.0“意味着可以支持:
2.1.4项目配置信息-配置web上下文首先打开"servers"视图,然后添加tomcat10,之后就可以设置上下文:
点击“edit”可以设置上下文。根据项目配置不同和sts的配置不同,有的时候,也可以在项目设置页面中直接设置上下文。
2.2修改pom.xml这里是关键,以下是本例的pom.xml的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hc.demo</groupId>
<artifactId>springjsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springjsp</name>
<description>Demo project for Spring boot and jsp and war</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 以下1~4是为了支持jsp -->
<!-- 1添加servlet依赖模块 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>10.0.22</version>
</dependency>
<!-- 2添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--3添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- 4使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 配置war包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>src/main/resources/**</warSourceExcludes>
<warName>springjsp</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
主要是两个地方要修改:
- 配置jsp
- 配置war插件
以上两个修改,分类如以下两个地方:
2.2.1配置jsp<!-- 以下1~4是为了支持jsp -->
<!-- 1添加servlet依赖模块 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>10.0.22</version>
</dependency>
<!-- 2添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--3添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- 4使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>compile</scope>
</dependency>
此外,因为这里引入了"spring-boot-starter-tomcat",所以需要把它从“spring-boot-starter-web”移除掉,具体见完整pom.xml。
2.2.2配置war插件<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <warSourceExcludes>src/main/resources/**</warSourceExcludes> <warName>springjsp</warName> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build>
具体过程略,一些列出过滤器代码:
package com.hc.demo.config.filter; import java.io.IOException; import org.springframework.web.filter.OncePerRequestFilter; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.repo.spring.io/milestone/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/3.2.0/maven-clean-plugin-3.2.0.pom (5.3 kB at 1.5 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom (9.9 kB at 1.8 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/apache/maven/maven-parent/35/maven-parent-35.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/apache/maven/maven-parent/35/maven-parent-35.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/apache/maven/maven-parent/35/maven-parent-35.pom (45 kB at 4.2 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/apache/apache/25/apache-25.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/apache/apache/25/apache-25.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/apache/apache/25/apache-25.pom (21 kB at 5.5 kB/s) [INFO] [INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ springjsp --- [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar (169 kB at 7.0 kB/s) [INFO] Deleting E:\codes-java\git\learning\gitee\springjsp\target [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ springjsp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ springjsp --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 7 source files to E:\codes-java\git\learning\gitee\springjsp\target\classes [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ springjsp --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ springjsp --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 7 source files to E:\codes-java\git\learning\gitee\springjsp\target\classes [INFO] [INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ springjsp --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ springjsp --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ springjsp --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-war-plugin:3.3.2:war (default-war) @ springjsp --- [INFO] Packaging webapp [INFO] Assembling webapp [springjsp] in [E:\codes-java\git\learning\gitee\springjsp\target\springjsp-0.0.1-SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [E:\codes-java\git\learning\gitee\springjsp\src\main\webapp] [INFO] Building war: E:\codes-java\git\learning\gitee\springjsp\target\springjsp.war [INFO] [INFO] --- spring-boot-maven-plugin:3.0.0-SNAPSHOT:repackage (repackage) @ springjsp --- [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/maven-metadata.xml [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/maven-metadata.xml (2.4 kB at 1.5 kB/s) [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/spring-boot-buildpack-platform-3.0.0-20220626.095848-608.pom [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/spring-boot-buildpack-platform-3.0.0-20220626.095848-608.pom (3.4 kB at 2.5 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.pom (0 B at 0 B/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/net/java/dev/jna/jna/5.7.0/jna-5.7.0.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.pom (0 B at 0 B/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.pom (0 B at 0 B/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.pom (0 B at 0 B/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/antlr/antlr4-master/4.7.2/antlr4-master-4.7.2.pom [INFO] Downloading from central: repo.maven.apache.org/maven2/org/antlr/antlr4-master/4.7.2/antlr4-master-4.7.2.pom [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/antlr/antlr4-master/4.7.2/antlr4-master-4.7.2.pom (0 B at 0 B/s) [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/maven-metadata.xml [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/maven-metadata.xml (2.4 kB at 1.4 kB/s) [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/spring-boot-loader-tools-3.0.0-20220626.095848-608.pom [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/spring-boot-loader-tools-3.0.0-20220626.095848-608.pom (2.3 kB at 1.4 kB/s) [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/spring-boot-buildpack-platform-3.0.0-20220626.095848-608.jar [INFO] Downloading from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/spring-boot-loader-tools-3.0.0-20220626.095848-608.jar [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/spring-boot-loader-tools-3.0.0-20220626.095848-608.jar (247 kB at 39 kB/s) [INFO] Downloaded from spring-snapshots: repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/spring-boot-buildpack-platform-3.0.0-20220626.095848-608.jar (240 kB at 35 kB/s) [INFO] Downloading from spring-milestones: repo.spring.io/milestone/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.jar [INFO] Downloading from spring-milestones: repo.spring.io/milestone/net/java/dev/jna/jna/5.7.0/jna-5.7.0.jar [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar [INFO] Downloading from spring-milestones: repo.spring.io/milestone/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar [INFO] Downloading from central: repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar (0 B at 0 B/s) [INFO] Downloaded from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.jar (0 B at 0 B/s) [INFO] Downloaded from central: repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.jar (0 B at 0 B/s) [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar (0 B at 0 B/s) [INFO] Downloaded from central: repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar (268 kB at 38 kB/s) [INFO] Replacing main artifact with repackaged archive [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:31 min [INFO] Finished at: 2022-07-04T22:06:59+08:00 [INFO] ------------------------------------------------------------------------
五、测试 5.1安装和配置tomcat10安装过程略。
主要是设置JAVA_HOME和日志编码。
5.1.1设置JAVA_HOME打开TOMCAT10的bin\catalina.bat,在”setlocal"下插入一句:
set JAVA_HOME=D:\soft\develop-tool\java\jdk-17.0.3.1
5.1.2设置日志编码打开tomcat10\conf\logging.properties,把所有的“UTF-8”修改为“GBK"即可。
5.2部署war包把前面打包的E:\codes-java\git\learning\gitee\springjsp\target\springjsp.war复制到tomcat10\webapps下。
5.3运行war包在cmd下,运行tomcat10下的startup.bat,当然之前,需要先打开Mysql。
控制台输出如下:
看起来很正常,默认8080已经打开。
切换到浏览器,输入:localhost:8080/springjsp/
结果如下图:
运行 正常。
六、小结整个过程很顺利,麻烦的地方仅仅是配置pom.xml和在wms中配置静态资源。
和以前版本比起来,的确方便了不少。例如不要配置web.xml(实际上web module>=3.1即可),这样可以节省servlet,filter之类的配置。

