Springboot3.0+spring6.0+JDK17+配置jsp和打war包
由于某些缘故,公司的产品需要升级,但并不希望花费大量时间重写前端代码(原来的就不是前后分离的)。所以虽然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包
- 修改启动代码(不修改也可以)
2.1使用spring starter project创建工程
如果不想使用sts,仅仅是想看看有关配置,可以通过https://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="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 https://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>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://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>
三、编写测试代码
3.1编写过滤器
具体过程略,一些列出过滤器代码:
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.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; public class MyFilter extends OncePerRequestFilter { @Override public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpServletRequest request2 = (HttpServletRequest) request; String orgin = request2.getHeader("origin"); HttpServletResponse res = (HttpServletResponse) response; res.addHeader("Access-Control-Allow-Origin", orgin); res.addHeader("Access-Control-Allow-Methods", "*"); res.addHeader("Access-Control-Allow-Headers", "content-type"); String url=request.getRequestURI().toString(); System.out.println(url); FilterRequest req = new FilterRequest(request); filterChain.doFilter(req, response); } }
注:
1.红色部分代码是为了便于观察过滤器是否生效。
2.在jdk17中,“servlet”的顶级包名以“jakarta"开头,这是一个很大的区别。
3.2编写一个接口
具体过程略,先列出控制器代码:
package com.hc.demo.student.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.hc.demo.student.service.StudentService; @Controller @RequestMapping(value = "/student") public class StudentController { @Autowired private StudentService studentService; @RequestMapping(value = "list") @ResponseBody public Object list() { return studentService.queryAll(); } }
3.3编写一个jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jsp测试</title> </head> <style> html, body { width: 100%; height: 100%; } .jsp-example { display: flex; color: red; font-size: 32px; height: 20%; margin-top: 10%; margin-bottom:4px; line-height: 50%; text-align: center; background-color: yellow; flex-direction: column-reverse; flex-wrap: wrap; align-content: center; justify-content: center; } #studentTable{ border-width: 4px; border-style: solid; border-color: lightgray; width: 400px; margin: 0 auto; } tr,td,th{ border-width: 1px; border-style: solid; border-color: lightgray; text-align:center; } </style> <body> <div class="jsp-example" id="dDivExample"></div> <div id="dDivStudent"> <table id="studentTable"> <thead> <tr> <th>姓名</th> <th>性别</th> </tr> </thead> <tbody id="studentTableBody"> </tbody> </table> </div> </body> <script src="/springjsp/plugin/jquery/jquery-3.6.0.min.js"></script> <script> $(function () { document.getElementById("dDivExample").innerText="这是一个springboot3.0+jdk17+jsp+war的测试"; showStudent(); }); function showStudent(){ $.ajax({ url: '/springjsp/student/list', type: 'POST', dataType: 'json', async: true, success: function (rs, status, xhr) { let htmlText=""; for(let i=0,len=rs.length;i<len;i++){ htmlText+="<tr><td>"+rs[i].NAME+"</td><td>"+rs[i].sex+"</td></tr>\n" } let _body=document.getElementById("studentTableBody"); _body.innerHTML=htmlText; }, error: function (rs) { alert("失败!"); } }); } </script> </html>
3.4配置wms
package com.hc.demo.config; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import com.hc.demo.config.filter.MyFilter; @Configuration public class MyWMS extends WebMvcConfigurationSupport { @Override protected void addInterceptors(InterceptorRegistry registry) { } @Override public void addViewControllers(ViewControllerRegistry registry) { // 为根增加默认的 映射,指向index.html registry.addViewController("/").setViewName("index"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/static/") .addResourceLocations("classpath:/temploates/"); registry.addResourceHandler("/plugin/**").addResourceLocations("/plugin/"); super.addResourceHandlers(registry); } /** * 注册过滤器 * @return */ @Bean public FilterRegistrationBean<MyFilter> registerAuthFilter() { FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>(); MyFilter filter=new MyFilter(); registration.setFilter(filter); registration.addUrlPatterns("/*"); registration.setName("authFilter"); registration.setOrder(1); //值越小,Filter越靠前。 return registration; } }
上文红色部分是需要关注的地方,分别设置根url,静态资源和过滤器。
注意:这是springboot3.0+spring6.0和以往的一个大区别:以前的版本过滤器设置需要新建一个web.xml,并在web.xml中配置过滤器。但是现在不需要了,这个无疑非常方便,非常合理。
当然这主要是因为 5.0的web moudule可以避免配置web.xml(这个有点烦人)。
四、打包
新建debug Configurations,如下图:
”jre“部分,”VM arguments"设置参数"-Dmaven.test.skip=true",避免无谓的测试。
其它设置大概如下:
- goals- clean compile package,这是清理、编译,最后打包
- jdk-17
- 字符集-utf-8
- jrebel 关闭
之后,点击“debug”,则运行如下:
[INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.hc.demo:springjsp >------------------------ [INFO] Building springjsp 0.0.1-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] Downloading from spring-milestones: https://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: https://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: https://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: https://repo.spring.io/milestone/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/35/maven-plugins-35.pom [INFO] Downloaded from central: https://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: https://repo.spring.io/milestone/org/apache/maven/maven-parent/35/maven-parent-35.pom [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/35/maven-parent-35.pom [INFO] Downloaded from central: https://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: https://repo.spring.io/milestone/org/apache/apache/25/apache-25.pom [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/apache/25/apache-25.pom [INFO] Downloaded from central: https://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: https://repo.spring.io/milestone/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar [INFO] Downloaded from central: https://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: https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-buildpack-platform/3.0.0-SNAPSHOT/maven-metadata.xml [INFO] Downloaded from spring-snapshots: https://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: https://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: https://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: https://repo.spring.io/milestone/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.pom [INFO] Downloading from central: https://repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.pom [INFO] Downloaded from central: https://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: https://repo.spring.io/milestone/net/java/dev/jna/jna/5.7.0/jna-5.7.0.pom [INFO] Downloading from central: https://repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.pom [INFO] Downloaded from central: https://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: https://repo.spring.io/milestone/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.pom [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.pom [INFO] Downloaded from central: https://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: https://repo.spring.io/milestone/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.pom [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.pom [INFO] Downloaded from central: https://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: https://repo.spring.io/milestone/org/antlr/antlr4-master/4.7.2/antlr4-master-4.7.2.pom [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/antlr/antlr4-master/4.7.2/antlr4-master-4.7.2.pom [INFO] Downloaded from central: https://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: https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-loader-tools/3.0.0-SNAPSHOT/maven-metadata.xml [INFO] Downloaded from spring-snapshots: https://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: https://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: https://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: https://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: https://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: https://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: https://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: https://repo.spring.io/milestone/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.jar [INFO] Downloading from spring-milestones: https://repo.spring.io/milestone/net/java/dev/jna/jna/5.7.0/jna-5.7.0.jar [INFO] Downloading from spring-milestones: https://repo.spring.io/milestone/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar [INFO] Downloading from spring-milestones: https://repo.spring.io/milestone/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar [INFO] Downloading from spring-milestones: https://repo.spring.io/milestone/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar [INFO] Downloading from central: https://repo.maven.apache.org/maven2/net/java/dev/jna/jna-platform/5.7.0/jna-platform-5.7.0.jar [INFO] Downloading from central: https://repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.7.0/jna-5.7.0.jar [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/tomlj/tomlj/1.0.0/tomlj-1.0.0.jar [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/antlr/antlr4-runtime/4.7.2/antlr4-runtime-4.7.2.jar [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar [INFO] Downloaded from central: https://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: https://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: https://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: https://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: https://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已经打开。
切换到浏览器,输入:http://localhost:8080/springjsp/
结果如下图:
运行 正常。
六、小结
整个过程很顺利,麻烦的地方仅仅是配置pom.xml和在wms中配置静态资源。
和以前版本比起来,的确方便了不少。例如不要配置web.xml(实际上web module>=3.1即可),这样可以节省servlet,filter之类的配置。