SpringBoot入门:SpringBoot添加JSP支持

步骤:
(1) 创建Maven web project;
(2) 在pom.xml文件添加依赖;
(3) 配置application.properties支持jsp
(4) 编写测试Controller
(5) 编写JSP页面
(6) 编写启动类App.java

1、使用IDEA创建Maven工程

2、添加依赖:

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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<properties>
<java.version>1.7</java.version>
</properties>

<dependencies>
<!-- spring-boot-starter-web: MVC,AOP的依赖包.... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <version></version> 由于我们在上面指定了 parent(spring boot) -->
</dependency>

<!-- servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- tomcat 的支持. -->
<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>

</dependencies>

3、配置application.properties:

1
2
3
4
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp

yml的写法:

1
2
3
4
5
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp

4、编写测试的controller: JspController

1
2
3
4
5
6
7
8
9
@Controller
public class JspController {

@RequestMapping("helloJsp")
public String hello() {
return "hello" ;
}

}

5、编写hello.jsp页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>hello</title>
</head>
<body>
<!--nav-end-->
<div style="text-align: center;color: red;font-size: 30px">
Hello Spring Boot !
</div>
</body>
</html>

6、编写启动类App.java:

1
2
3
4
5
6
7
@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class) ;
}
}

运行App.java, 访问地址: http://127.0.0.1:8080/helloJsp

运行结果:


SpringBoot入门:SpringBoot添加JSP支持
https://river106.cn/posts/2f43bee1.html
作者
river106
发布于
2018年9月16日
许可协议