详解SpringBoot 添加对JSP的支持(附常见坑点)

2025-05-29 0 99

序言:

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资源目录。如图:

详解SpringBoot 添加对JSP的支持(附常见坑点)

2. 在pom.xml 添加相关依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85
<?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支持

?

1

2

3

4

5

6

7

8

9

10

11

12
@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 配置如下:

?

1

2

3

4

5
spring:

mvc:

view:

prefix: /web-inf/views/ # read from web resources dir

suffix: .jsp

5. 编写jsp页面

在 web-inf/views 目录下新建一个jsp文件

?

1

2

3

4

5

6

7

8

9
<%@ page contenttype="text/html;charset=utf-8" language="java" %>

<!doctype html>

<html>

<body>

<marquee><p style="font-size: 100px">hello jsp !!</p>

![](${pagecontext.servletcontext.contextpath}/doge.gif)

</marquee>

</body>

</html>

6.启动项目

启动方式1:在ide启动webjspapplication,然后打开项目地址。

启动方式2:部署到外置tomcat,启动完成后,打开项目地址。这里需要注意的是,使用外置tomcat部署的时候,需要将嵌入式容器调整为provided级别。(防止冲突)

?

1

2

3

4

5

6

7

8

9

10
<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>

7.单元测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
@runwith(springrunner.class)

@springboottest(webenvironment = webenvironment.random_port)

@dirtiescontext

public class webjspapplicationtest {

@autowired

private testresttemplate resttemplate;

@test

public void testjspwithel() throws exception {

responseentity<string> entity = resttemplate.getforentity("/", string.class);

assertthat(entity.getstatuscode()).isequalto(httpstatus.ok);

assertthat(entity.getbody()).contains("hello jsp");

}

}

全文至此,有疑问的小伙伴可在评论下方进行交流。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://www.jianshu.com/p/de939365c472?utm_source=tuicool&utm_medium=referral

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解SpringBoot 添加对JSP的支持(附常见坑点) https://www.kuaiidc.com/114375.html

相关文章

发表评论
暂无评论