springboot中使用jsp

项目结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
├─src
│ ├─main
│ │ ├─java
│ │ │ └─cn
│ │ │ └─idea360
│ │ │ └─jsp
│ │ │ └─JspApplication.java
│ │ │ └─controller
│ │ │ └─IndexController.java
│ │ ├─resources
│ │ │ ├─META-INF
│ │ │ └─static
│ │ └─webapp
│ │ └─WEB-INF
│ │ └─jsp
│ │ └─index.jsp

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

<groupId>cn.idea360</groupId>
<artifactId>jsp</artifactId>
<version>0.0.1</version>

<name>jsp</name>
<description>springboot-jsp示例</description>

<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.12</spring-boot.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

application.yaml

1
2
3
4
5
6
7
8
9
10
11
server:
port: 8080

spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
logging:
level:
web: DEBUG

index.jsp

1
2
3
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Hi JSP. 现在时间是 ${now}

java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cn.idea360.jsp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.util.Date;


@Controller
public class IndexController {

// http://127.0.0.1:8080
@RequestMapping("/")
public ModelAndView hello(Model m) {
ModelAndView hello = new ModelAndView("index");
hello.addObject("now", DateFormat.getDateTimeInstance().format(new Date()));
return hello;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
package cn.idea360.jsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JspApplication {

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

}