我正在尝试在我的 Spring 引导应用程序中添加一个 jsp 页面。我的问题是,每次我尝试访问该页面时,我都会遇到以下问题:

I'm trying to add a jsp page in my Spring Boot Aplication. My problem is that every time I try to go to that page I have this:

白标错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。

4 月 26 日星期二 16:10:15IRDT 2022 出现意外错误(类型=未找到,状态=404)。 JSP 找不到文件 [/WEB-INF/jsp/home.jsp]

我已将前缀和后缀添加到我的 application.properties:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

这是我的控制器class:

@Controller
@RequestMapping("/")
public class HomeController {
@GetMapping("/")
public String index(){
    return "home";
}
}

我在控制器中有 1 个警告 class :

Cannot resolve MVC view 'home'

my project folders picture

我的申请class:

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

和home.jsp:

<h1> hello world from jsp</h1>

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.13</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>largesize.shop.app</groupId>
<artifactId>OnlineShop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OnlineShop</name>
<description>Online shopping web application</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </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>
</dependencies>

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

您似乎缺少名为 home 的模板。您需要创建此模板并将其存储在 \src\main\resources\templates 下 该模板是一个 html 文件,您要将来自 spring 的响应返回到该文件。它应该是这样的:

project-directory
->src
    ->main
        ->java->com->...etc
        ->resources
            ->static
            ->templates
                home.html
                other_file.html
            application.properties
      
    ->test

也停止使用特定版本。使用这个:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

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

还回滚到 TomCat 9,因为不支持 10。

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

您可以从 The MVN Repository

中获取这些依赖项

更改以下内容:

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

至:

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

当我打开项目时,我打开了包含该项目的文件夹,该文件夹比主项目文件夹高一卷。