spring-eleaf找不到js/css

wsxa1bj1  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(361)

我尝试使用spring工具套件和eclipse用spring构建一个简单的web应用程序。作为模板,我使用的是thymeleaf,但由于一些奇怪的原因,我无法在html页面中加载js或css等静态文件。
更新一个最小的示例:
src/main/java下的文件夹

package com.visualizer.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AlgorithmVisualizerApplication {

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

}
package com.visualizer.web.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("test");
    }

}

我的html src/main/resources/templates/test.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link th:href="@{/css/testLayout.css}" rel="stylesheet" />
        <script type="text/javascript" th:src="@{/js/testJS.js}"></script>
    </head>

    <body onload=test()>
        <h1>TEST</h1>
    </body>
</html>

我的css在src/main/resources/static/css/testlayout.css找到

BODY {
    color: green;
}

我的js位于src/main/resources/static/js/testjs.js

function test() {
    console.log("test successfull");
}

构建.gradle

plugins {
    id 'org.springframework.boot' version '2.5.0-SNAPSHOT'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.visualizer.web'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '16'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
    maven { url 'https://repo.spring.io/snapshot' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
}

test {
    useJUnitPlatform()
}

我的test.html可以加载,但是css和js不能加载。

fykwrbwg

fykwrbwg1#

例如,如果您的应用程序正在端口8080上的localhost上运行,那么请检查http://localhost:8080/css/layout.css工作。
另外,请务必在此处使用前导斜杠:

<link th:href="@{/css/layout.css}" rel="stylesheet" />
<script type="text/javascript" th:src="@{/js/layoutCreator.js}"></script>

更新:
根据您最新的问题信息,我可以这样说:
拆下 WebConfig 类,并创建一个控制器类。例如:

@Controller
public class TestController {

  @GetMapping("/test")
  public String testPage() {
    return "test"; // this will search for a Thymeleaf template at src/main/resources/templates called test.html
  }
}

相关问题