项目结构
1.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.it</groupId>
<artifactId>009-springboot-jsp</artifactId>
<version>1.0.0</version>
<name>009-springboot-jsp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--处理jsp的依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<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>
</dependencies>
<build>
<!--指定jsp编译后的存放目录-->
<resources>
<resource>
<!--jsp原来的目录-->
<directory>src/main/webapp</directory>
<!--指定编译后的存放目录-->
<targetPath>META-INF/resources</targetPath>
<!--指定处理的目录和文件-->
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.资源配置文件application.properties,配置视图解析器和连接端口号
#配置端口号
server.port=9090
server.servlet.context-path=/myboot
#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
3.创建controller包下的JspController类
package com.it.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class JspController {
@RequestMapping("/myjsp")
public String doJsp(HttpServletRequest request){
request.setAttribute("data","SpringBoot使用jsp");
//视图的逻辑名称
return "index";
}
}
4.编写index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>jsp文件</title>
</head>
<body>
<h3>
使用jsp,显示Controller中的数据:${data}
</h3>
</body>
</html>
5.启动Application类
查看Tomcat服务器是否启动成功,以及它的端口号和路径
6.测试项目
通过正确的访问地址路径可以正确的在页面显示出各种需要的数据