spring-eleaf找不到js/css

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

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

  1. package com.visualizer.web;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class AlgorithmVisualizerApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(AlgorithmVisualizerApplication.class, args);
  8. }
  9. }
  1. package com.visualizer.web.config;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  5. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7. @Configuration
  8. @ComponentScan
  9. @EnableWebMvc
  10. public class WebConfig implements WebMvcConfigurer {
  11. @Override
  12. public void addViewControllers(ViewControllerRegistry registry) {
  13. registry.addViewController("/").setViewName("test");
  14. }
  15. }

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

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org"
  3. xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <link th:href="@{/css/testLayout.css}" rel="stylesheet" />
  7. <script type="text/javascript" th:src="@{/js/testJS.js}"></script>
  8. </head>
  9. <body onload=test()>
  10. <h1>TEST</h1>
  11. </body>
  12. </html>

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

  1. BODY {
  2. color: green;
  3. }

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

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

构建.gradle

  1. plugins {
  2. id 'org.springframework.boot' version '2.5.0-SNAPSHOT'
  3. id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.visualizer.web'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '16'
  9. repositories {
  10. mavenCentral()
  11. maven { url 'https://repo.spring.io/milestone' }
  12. maven { url 'https://repo.spring.io/snapshot' }
  13. }
  14. dependencies {
  15. implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
  16. implementation 'org.springframework.boot:spring-boot-starter-web'
  17. testImplementation 'org.springframework.boot:spring-boot-starter-test'
  18. developmentOnly 'org.springframework.boot:spring-boot-devtools'
  19. }
  20. test {
  21. useJUnitPlatform()
  22. }

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

fykwrbwg

fykwrbwg1#

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

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

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

  1. @Controller
  2. public class TestController {
  3. @GetMapping("/test")
  4. public String testPage() {
  5. return "test"; // this will search for a Thymeleaf template at src/main/resources/templates called test.html
  6. }
  7. }
展开查看全部

相关问题