Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Thymeleaf和Spring Boot,如何构造assets文件夹以在运行时和设计时查看html_Java_Spring Boot_Thymeleaf - Fatal编程技术网

Java Thymeleaf和Spring Boot,如何构造assets文件夹以在运行时和设计时查看html

Java Thymeleaf和Spring Boot,如何构造assets文件夹以在运行时和设计时查看html,java,spring-boot,thymeleaf,Java,Spring Boot,Thymeleaf,我有一个SpringBoot2项目,使用Thymeleaf模板引擎。 文件夹结构为: main\ resources\ static\ assets\ css\ my.css js\ templates\ index.html (1) 如果我在index.html中将my.css引用为../static/assets/css/my.css,我可以在浏览器中直接查看index.html(file:/

我有一个SpringBoot2项目,使用Thymeleaf模板引擎。 文件夹结构为:

main\
  resources\
    static\
      assets\
        css\
          my.css
        js\

    templates\
      index.html
(1) 如果我在index.html中将my.css引用为../static/assets/css/my.css,我可以在浏览器中直接查看index.html(file:///path/to/main/resources/templates/index.html)但是如果我在JetBrains IDEA中运行该项目,并将其浏览为,浏览器控制台会告诉我找不到my.css

(2) 如果我在index.html中将my.css引用为assets/css/my.css,当我直接在浏览器中查看index.html时(file:///path/to/main/resources/templates/index.html)浏览器告诉我找不到my.css,但是如果我在JetBrains IDEA中运行项目,并将其浏览为,浏览器视图将变为OK


因为Thymeleaf网站说它对设计时间、运行时间以及与程序员和设计师的合作都很友好,那么有人能告诉我如何构建我的静态资源和html模板文件夹关系来实现这个目标吗?首先非常感谢

在Spring boot中,静态文件从位置
src/main/resources/static
提供,并且在应用程序URL的根目录下可用。 例如,如果您有
src/main/resources/static/assets/css/my.css
,那么当您运行应用程序时,它在
http://localhost:8080/assets/css/my.css

因此,您可以将其包含在
index.html
中,如下所示:

<link rel="stylesheet" href="../static/assets/css/my.css" 
    th:href="@{/assets/css/my.css}" />


这样,当您在浏览器中打开
index.html
时,它将使用
href
检测CSS,当您通过服务器启动它时(即运行应用程序并在浏览器中打开它),Thymeleaf将显示
th:href
。所以在这两种情况下都有效。

谢谢,就这样!