SpringBoot (.jsp)文件被下载而不是被渲染(显示)出来

我正在使用SpringBoot创建一个显示HTML页面的应用程序,但浏览器没有显示,而是开始下载。以下是这些代码。

文件:
/SpringBoot1_FirstWebApp/src/main/java/com/example/demo/com/example/demo/SpringBoot1FirstWebAppApplication.java

package com.example.demo.com.example.demo;


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

@SpringBootApplication
public class SpringBoot1FirstWebAppApplication {

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

}

文件:
/SpringBoot1_FirstWebApp/src/main/java/com/example/demo/com/example/demo/SpringController.java

package com.example.demo.com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SpringController {

    
    @RequestMapping("/Courses")
    public String cs() {
        return "Course.jsp";
    }
}

文件: /SpringBoot1_FirstWebApp/src/main/WebApp/Course.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
HI
</body>
</html>

文件: /SpringBoot1_FirstWebApp/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.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringBoot1_FirstWebApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot1_FirstWebApp</name>
    <description>Demo project</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</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-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jsp-api -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
            <version>9.0.48</version>
        </dependency>
        
        
    </dependencies>

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

</project>

StackOverflow:java - SpringBoot (.jsp) file gets downloaded instead of being displayed - Stack Overflow

你缺少 JSP 编译所需的 JAR。

你可以用下面的一个依赖性来添加它。

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

或者

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>${tomcat.version}</version>
    </dependency>

变量 "tomcat.version "是由上级POM定义的。