我新建了一個springboot專案,專案的目錄結構如下: 在此處輸入圖片描述 yml配置如下:
server:
port: 8783
servlet:
context-path: /spring-demo
spring:
scan : com.example
application:
name: spring-demo
mvc:
view:
suffix: .html
prefix: /
web:
resources:
static-path-pattern: /**
static-locations: classpath:/templates/,classpath:/static/
thymeleaf:
cache: false
suffix: .html
IndexController 如下
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index")
public String index(){
System.out.println("index");
return "/index";
}
}
索引.html
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="icon" href="/static/favicon.ico">
<title>private-report</title>
</head>
<body><noscript><strong>We're sorry but private-report doesn't work properly without JavaScript enabled. Please enable
it to continue.</strong></noscript>
<div id="app"></div>
<link href="/static/js/chunk-vendors.365b8cfb.js" rel="preload" as="script">
</body>
</html>
當我啟動spring-demo服務,在瀏覽器中輸入http://localhost:8783/spring-demo/index時,頁面報錯 enter image description here
可以訪問index介面,但是無法訪問js檔案,報404錯誤。
嘗試了很多方法都無法訪問js檔案,spring boot版本是2.5.5.
uj5u.com熱心網友回復:
我發現了你的錯誤。您根據需要使用spring-demo未應用于相對路徑的根背景關系路徑
你必須把它改成
<link href="/spring-demo/static/js/chunk-vendors.365b8cfb.js" rel="preload" as="script">
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/395196.html
