详解SpringBoot 添加对JSP的支持(附常见坑点)
序言:
SpringBoot默认不支持JSP,如果想在项目中使用,需要进行相关初始化工作。为了方便大家更好的开发,本案例可直接作为JSP开发的脚手架工程 SpringBoot+War+JSP .
常见问题:
1.修改JSP需重启才能生效:
在生产环境中,SpringBoot重新编译JSP可能会导致较大的性能损失,并且很难追查到问题根源,所以在最新的版本中,官方已经默认关闭此功能,详见JspServlet类的初始化参数。那么,如何解决这个问题呢?推荐两个解决办法:1.使用devtools 2. 添加配置(server.servlet.jsp.init-parameters.development=true)
2.各种404:
1.必须导入嵌入式容器和JASPER解析器 2.必须创建webapp目录
正文:SpringBoot 添加对JSP的支持
1. 搭建脚手架
首先使用 Spring Initializr构建工程,其中源码和静态资源目录默认生成,这里只需手工添加Web资源目录。如图:
2. 在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>com.hehe</groupId>
<artifactId>springboot-web-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version> <!--打包格式:SpringBoot使用JSP时需打包为war类型 -->
<packaging>war</packaging> <!--继承父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M4</version>
<relativePath/>
</parent> <!--依赖管理 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <!--指定远程仓库(含插件) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories> <!--构建插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
3. 启动类添加Servlet支持
@SpringBootApplication
public class SpringbootWarJspApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootWarJspApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootWarJspApplication.class, args);
}
}
4. 添加MVC映射
application.yml 配置如下:
spring:
mvc:
view:
prefix: /WEB-INF/views/ # Read From Web Resources Dir
suffix: .jsp
5. 编写JSP页面
在 WEB-INF/views 目录下新建一个JSP文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<body>
<marquee><p style="font-size: 100px">Hello JSP !!</p>

</marquee>
</body>
</html>
6.启动项目
启动方式1:在IDE启动WebJspApplication,然后打开项目地址。
启动方式2:部署到外置Tomcat,启动完成后,打开项目地址。这里需要注意的是,使用外置Tomcat部署的时候,需要将嵌入式容器调整为provided级别。(防止冲突)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
详解SpringBoot 添加对JSP的支持(附常见坑点)的更多相关文章
- SpringBoot添加对jsp的支持
1.在pom.xml添加如下内容: <dependency> <groupId>org.apache.tomcat.embed</groupId> <arti ...
- 详解Springboot中自定义SpringMVC配置
详解Springboot中自定义SpringMVC配置 WebMvcConfigurer接口 这个接口可以自定义拦截器,例如跨域设置.类型转化器等等.可以说此接口为开发者提前想到了很多拦截层面的需 ...
- 详解SpringBoot(2.3)应用制作Docker镜像(官方方案)
关于<SpringBoot-2.3容器化技术>系列 <SpringBoot-2.3容器化技术>系列,旨在和大家一起学习实践2.3版本带来的最新容器化技术,让咱们的Java应用更 ...
- 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)
转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...
- [转]application.properties详解 --springBoot配置文件
本文转载:http://blog.csdn.net/lpfsuperman/article/details/78287265###; # spring boot application.propert ...
- application.properties详解 --springBoot配置文件【转载】
# spring boot application.properties配置的各个属性详解 # 该示例文件作为标准提供.(官方文档 翻译过来的) # 还是花了些功夫翻译,各位如果转发,请留下本文地址, ...
- application.properties详解 --springBoot配置文件
本文转载:http://blog.csdn.net/lpfsuperman/article/details/78287265###; # spring boot application.propert ...
- SpringBoot添加对Log4j2的支持
1.在添加对Log4j2的支持前,需要先把SpringBoot默认使用的Logback日志框架排除,修改pom.xml文件: <dependency> <groupId>org ...
- SpringBoot添加对Mybatis的支持
1.修改maven配置文件pom.xml,添加对mybatis的支持: <dependency> <groupId>org.mybatis.spring.boot</gr ...
随机推荐
- 移动端UI自动化Appium测试——DesiredCapabilities参数配置及含义
一.DesiredCapabilities的作用: 负责启动服务端时的参数设置,启动session的时候是必须提供的. Desired Capabilities本质上是key value的对象,它告诉 ...
- SharePoint2013升级SP1后,运行配置向导报错:未注册sharepoint服务
SharePoint Server 2013 升级SP1后,需要重新运行配置向导,但是运行过程中报错:未注册sharepoint服务. 日志详细错误信息: 已引发类型为 Microsoft.Share ...
- P1433 吃奶酪
题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...
- iOS Programming Localization 本地化
iOS Programming Localization 本地化 Internationalization is making sure your native cultural informatio ...
- DBUtils使用技巧
BbUtils(一) 结果集概览:http://www.cnblogs.com/myit/p/4269165.html DbUtils(二) 结果集实例:http://www.cnblogs.com/ ...
- Swift3命名空间的实现
最近在看一些Swift开源库的时候,发现了一些优秀的开源库都使用了命名空间,例如Kingfisher这个开源库中,就针对UIImage,UIImageView,UIButton做了命名空间的扩展.通过 ...
- HTML head meta标签详细
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...
- redis新特性
摘自<redis 4.xcookbook> 从实例重启同步] 故障切换同步] 4.0之前从实例主键过期bug redis4新特性 Memory Command Lazy Free PSYN ...
- 花括号的使用 printf %${width}s , 否则会 去找 $widths
花括号的使用 printf %${width}s , 否则会 去找 $widths 1 #! /usr/bin/perl 2 use strict; 3 use warnings; 4 ...
- jquery 应用
http://www.jq22.com/ gwj6396668@163.com