在我的SpringBoot应用程序中,我有两个控制器。我正在尝试呈现JSP页面,但无法这样做。我在JSP页面中有<app-root>
,它指向角应用程序。
在点击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).
我尝试过许多解决方案,但都没有奏效。我需要帮助。谢谢
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>
FirstController
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";
}
}
发布于 2022-01-21 22:01:50
添加下面的依赖项可以解决错误。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
发布于 2022-01-18 09:14:11
1.3.5.JSP限制 当运行使用嵌入式servlet容器(并打包为可执行存档)的Spring应用程序时,JSP支持有一些限制。
简单地说:“它只适用于.war
档案”!
若要将您的jar更改为war,请执行以下步骤(很少,简单):
ServletInitializer
。(也是来自:https://start.spring.io/#!packaging=war的最佳示例)发布于 2022-01-17 16:41:58
您的context-path
不正确。您可以将其设置为/
(用于根webapp,这是默认值)或目录(例如,/myapp
),但不像您所做的(/api/project1/mission
)那样设置一个子目录。
因此,我建议您从context-path
中删除application.yaml设置,并将控制器映射到/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";
}
}
https://stackoverflow.com/questions/70706695
复制相似问题