賞金將在 6 天后到期。此問題的答案有資格獲得 50聲望賞金。 Paritosh M想引起更多關注這個問題:
我正在尋找解決方案。謝謝
在我的 SpringBoot 應用程式中,我有 2 個控制器。我正在嘗試呈現 JSP 頁面但無法這樣做。我<app-root>在指向 Angular 應用程式的 JSP 頁面中有。
擊中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).
我嘗試了許多解決方案,但沒有一個有效。我需要幫助。謝謝
應用程式.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
歡迎.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";
}
}
另一個控制器.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";
}
}
uj5u.com熱心網友回復:
你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";
}
}
uj5u.com熱心網友回復:
1.3.5。JSP 限制
在運行使用嵌入式 servlet 容器(并打包為可執行存檔)的 Spring Boot 應用程式時,JSP 支持存在一些限制。
使用 Jetty 和 Tomcat,如果您使用戰爭包裝,它應該可以作業。當使用 java -jar 啟動時,可執行的戰爭將起作用,并且也可以部署到任何標準容器。使用可執行 jar 時不支持 JSP。
Undertow 不支持 JSP。
創建自定義 error.jsp 頁面不會覆寫錯誤處理的默認視圖。應改為使用自定義錯誤頁面。
簡而言之,第 2 項:“它僅適用于.war檔案”!
要將您的 jar 更改為戰爭,請按照以下(很少,簡單)步驟操作:
- 將 pom-packaging|gradle-plugin 更改為“war”。(https://start.spring.io打包選項)
- 添加
ServletInitializer到您的應用程式。(也是來自: https ://start.spring.io/#!packaging=war的最佳示例)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/414601.html
標籤:
