Spring+jsp老项目升级为Spring Boot
前言
Spring4.x时代产生了Spring Boot,Spring Boot是Spring框架的扩展,它消除了Spring应用程序所需的复杂配置,开发人员只需要专注业务开发,不在受复杂配置的折磨了,大大提高了开发效率。闲话不多说了,现在敲重点:如何将Spring+jsp项目最小化升级至Spring Boot?
构建SpringBoot项目
1.项目目录结构
2.Maven依赖
最小化Spring Boot项目配置,并添加JSP依赖jar。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 项目依赖jar配置 -->
<dependencies>
<!-- Web项目需要 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 跳转jsp页面必须的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 用于编译JSP-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
3.配置application.properties
添加对jsp访问路径和映射支持,此处要注意jsp页面不能放在WEB-INF/目录
,因为放在WEB-INF/目录下是无法直接访问的,需要通过控制器解释,也就是需要通过访问后台。
server.port=8080
server.error.path=/error
server.tomcat.uri-encoding=UTF-8
server.servlet.context-path=/study
#jsp access support
spring.mvc.view.prefix=/webapp/
spring.mvc.view.suffix=.jsp
4.编写并配置默认访问页面index.jsp
4.1创建src/main/webapp/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>hello world</title>
<base href="<%=basePath%>"/>
<meta http-equiv="refresh" content="0;url=<%= basePath%>home/home.jsp">
</head>
</html>
4.2添加另外一个jsp页面
用于测试从index.jsp跳转测试src/main/webapp/home/home.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<base href="${basePath}"/>
<title>Hello World首页</title>
</head>
<body>
<h1>hello world 首页</h1>
<p>欢迎访问 Springboot jsp页面!
</body>
</html>
4.3添加配置文件加载默认页面
WebMvcConfig.java 配置注册默认视图
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// 设置访问默认页面
registry.addViewController("/").setViewName("forward:/index.jsp");
//设置过滤优先级最高
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
5.配置一个JspServlet拦截处理所有的Jsp页面
import javax.servlet.annotation.WebServlet;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@WebServlet(urlPatterns = "*.jsp",name = "JspServlet")
public class JspServlet extends org.apache.jasper.servlet.JspServlet {
}
6.运行测试
启动StudyApplication
,右键-》Run As-》Java Application。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.1.RELEASE)
2020-04-25 13:18:48.210 INFO 5720 --- [ main] com.kcy.study.StudyApplication : Starting StudyApplication on DESKTOP-K3S7MDU with PID 5720 (started by kechangyan in C:\本地磁盘E\geoonline2020\study)
2020-04-25 13:18:48.214 INFO 5720 --- [ main] com.kcy.study.StudyApplication : No active profile set, falling back to default profiles: default
2020-04-25 13:18:49.925 INFO 5720 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-04-25 13:18:49.965 INFO 5720 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-25 13:18:49.965 INFO 5720 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13
2020-04-25 13:18:49.977 INFO 5720 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_161\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\本地磁盘D\Program Files\Xftp 6\C:\本地磁盘D\Program Files\nodejs;C:\Windows\System32;C:\本地磁盘D\Program Files\geoglobe7.0.0.18251\support\native;C:\本地磁盘D\Program Files\gradle-5.6\bin;C:\本地磁盘D\Program Files\tree-1.5.2.2-bin\bin;C:\本地磁盘D\Program Files\apache-maven-3.2.5\bin;C:\Users\kechangyan\AppData\Local\Programs\Python\Python35\Scripts\;C:\Users\kechangyan\AppData\Local\Programs\Python\Python35\;C:\Users\kechangyan\AppData\Local\Microsoft\WindowsApps;C:\Users\kechangyan\AppData\Roaming\npm;C:\本地磁盘D\Program Files\apache-maven-3.2.5\bin;;C:\本地磁盘D\Program Files\Fiddler;.]
2020-04-25 13:18:50.339 INFO 5720 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2020-04-25 13:18:50.350 INFO 5720 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/study] : Initializing Spring embedded WebApplicationContext
2020-04-25 13:18:50.350 INFO 5720 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2090 ms
2020-04-25 13:18:50.584 INFO 5720 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-25 13:18:50.889 INFO 5720 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/study'
2020-04-25 13:18:50.892 INFO 5720 --- [ main] com.kcy.study.StudyApplication : Started StudyApplication in 3.052 seconds (JVM running for 3.813)
6.1访问http://localhost:8080/study
注:JspServet思想源自CSDN博主,不过链接地址忘了,作为小白在此再向这位大佬致敬。以上方式已经在项目上应用并成功实现将Spring+Jsp老项目转换成Spring Boot项目,如果有不正确地方请多指正,谢谢!