如何在 thymeleaf html 中包含静态文件夹中保存的 js 文件

tin*_*tin 2 spring spring-mvc thymeleaf spring-boot

我正在使用Spring Tool Suite构建 Spring Boot 应用程序并使用thymeleaf作为模板引擎。

文件夹结构如下:

src/main/resources/static  
src/main/resources/templates
Run Code Online (Sandbox Code Playgroud)

HTML文件保存在src/main/resources/templates ,javacript文件保存在src/main/resources/static

为了将 javascript 文件包含在我的 html 中,我添加了以下行:

<script type="text/javascript" th:src="@{../static/js/policyCreations.js}"></script>
Run Code Online (Sandbox Code Playgroud)

但我在 chrome 控制台中收到以下错误:

获取http://localhost:8082/static/js/policyCreations.js网::ERR_ABORTED 404

谁能帮我解决这个问题

Rah*_*ain 5

根据官方文档:

Spring Boot 将自动添加位于以下任意目录中的静态 Web 资源:

  • /META-INF/资源/
  • /资源/
  • /静止的/
  • /民众/

因此,无需指定静态文件夹名称,即可直接引用位于静态文件夹中的所有文件。

喜欢:

<script type="text/javascript" th:src="@{/js/policyCreations.js}"></script>
Run Code Online (Sandbox Code Playgroud)