【问题标题】:What's the difference between static and templates folder under resources in a springboot & thymeleaf project?springboot & thymeleaf 项目中资源下的静态文件夹和模板文件夹有什么区别?
【发布时间】:2017-06-06 10:31:56
【问题描述】:

我是 thymeleaf 和 springboot 的新手。

我在 IntelliJ IDEA 中使用“New Project”->“Spring Initalizr”创建了一个带有 thymeleaf 的 springboot 项目。

然后我得到一个结构如下所示的项目。

demo
  |+-src/main
  |    +-java
  |    +-resources
  |         static
  |         templates
  |         application.properties
  |-pom.xml

问题是我应该在资源下的静态和模板中放置什么?他们之间有什么区别?

我猜应该将具有 thymeleaf 属性的 .html 文件放在模板中。静态html、css和js怎么样?

谢谢。

【问题讨论】:

    标签: intellij-idea spring-boot thymeleaf


    【解决方案1】:

    templates 文件夹是放置所有 thymeleaf 模板的地方。它是一个默认目录(默认情况下 spring 会在里面寻找任何模板)。

    static 文件夹用于提供 web 静态内容,所有 css、js、html 等(也是 spring 的默认文件夹搜索) http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content

    【讨论】:

    • 不幸的是,在使用 th:src="@{/images/logos/mylogo.png}" 之类的东西时,IntelliJ 似乎没有为 Thymeleaf 模板的静态文件夹中的路径/文件提供代码补全。当您运行该应用程序时它会正常工作,但在处理模板时,它不会像标准 HTML src= 标记那样完成代码。
    • @SiriusBits 您可以在工件jetbrains.com/help/idea/web-application-static-content.html 中使用 Web 描述符,我将两者都添加为“/”并且 IntelliJ 能够为我提供代码完成。
    【解决方案2】:

    这些文件夹是组织文件的一种方式。这些statictemplates 文件夹具有特殊含义,因为它知道在开发人员中组织您的前端文件(例如cssjshtml 等)已成为共识。据我了解,我们应该放置像cssjsimages 这样的文件,这些文件支持您的网页,这些网页不会在您的应用程序运行时发生变化。例如:您的设计不会改变,这在您的css 文件中完成。在templates 我们应该放置html 文件。如果同时添加 html 和其他文件,则很难组织文件。

    【讨论】:

    • 没有thymeleaf属性的html文件应该放在哪里?它们在运行时不会改变。
    • 仍将它们添加到templates 文件夹中,其中包括您的Web 应用程序的结构和布局。
    • 我应该在哪里放置一个带有 index.html 的角度应用程序?在 /static /public 或 /templates 中?
    猜你喜欢
    • 2015-06-13
    • 2012-09-26
    • 1970-01-01
    • 2011-07-02
    • 2011-04-21
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多