无法使用 Spring 引导呈现 JSP 页面

Unable to render the JSP page using Spring Boot

在我的 SpringBoot 应用程序中,我有 2 个控制器。我正在尝试呈现 JSP 页面但无法这样做。我在 JSP 页面中有 <app-root> 指向 Angular 应用程序。

点击 http://localhost:8081//api/project1/mission/home 时出现以下错误。

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Jan 14 00:15:58 CST 2022
There was an unexpected error (type=Not Found, status=404).

我尝试了很多解决方案,但其中 none 有效。我需要帮助。谢谢

application.yaml

server:
  port: 8081
  servlet:
    context-path: /api/project1/mission
  tomcat:
    max-http-header-size: 72636B

management:
  endpoints:
    web:
      base-path:
      exposure:
        include: health
      path-mapping:
        health: healthcheck
  health:
    solr:
      enabled: false
    redis:
      enabled: false

spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

welcome.jsp


      <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <base href="/api/project1/mission/"/>
    <script type="text/javascript" src="${pageContext.request.contextPath}/dist/polyfills.js" defer></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/dist/runtime.js" defer></script>
 
</head>
<body>

<div id="mission-app-ctnr">
    <app-root></app-root>
</div>

</body>
</html>

第一个控制器


package com.google.mission.controller;

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

import java.util.Map;

@Controller
@RequestMapping(path = "/")
public class FirstController {

    private String message = "Hi";

    @GetMapping
    public String welcome(Map<String, Object> model) {
        model.put("message", this.message);
        System.out.println(this.message);
        return "welcome";
    }

}

AnotherController.js

package com.google.mission.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping(path = "/home")
public class AnotherController {

    private String message = "Hi";

    @GetMapping
    public String serveTemplate(Map<String, Object> model) {
        model.put("message", this.message);
        return "welcome";
    }

}

您的context-path不正确。您可以将其设置为 /(对于根 Web 应用程序,这是默认值)或目录(例如 /myapp),但不能像您那样设置子目录 (/api/project1/mission)。

所以我建议您从 application.yaml 中删除 context-path 设置并将您的控制器映射到 /api/project1/mission/home:

@Controller
@RequestMapping(path = "/api/project1/mission/home")
public class AnotherController {

    private String message = "Hi";

    @GetMapping
    public String serveTemplate(Map<String, Object> model) {
        model.put("message", this.message);
        return "welcome";
    }

}

1.3.5. JSP Limitations

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

  1. With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.

  2. Undertow does not support JSPs.

  3. Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.

将第 2 项简而言之:“.war 存档一起使用”!

要将您的 jar 更改为 war,请按照以下(几个简单的)步骤操作:

  1. 将 pom-packaging|gradle-plugin 更改为“war”。 (https://start.spring.io 包装选项)
  2. 在您的申请中添加 ServletInitializer。 (最好的样本来自:https://start.spring.io/#!packaging=war

添加以下依赖项应该可以解决错误。

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