javascript - 如何将 Thymeleaf 链接到外部 js 文件?

标签 javascript thymeleaf

我有一个在 Spring Boot 应用程序中提供服务的页面,但它通过 Thymeleaf 渲染 View 。我最初用 HTML 编写它,并将其连接到外部 JavaScript 文件,以便实现向数据库提交数据的功能。我无法让它工作,我正在 Thymeleaf 中完成它。但是,我不断收到 404。

这是我正在使用的文档:

http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html

这是我在实际页面中的代码:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <script type="text/javascript" th:src="@{/js/main.js}"></script>
  <title>Stereoscopics</title>
</head>

这是我得到的错误:

Request URL:http://localhost:8080/js/main.js Request Method:GET Status Code:404 Not Found Remote Address:[::1]:8080 Referrer Policy:no-referrer-when-downgrade

这是我尝试过的

  1. 最初我用 HTML 构建了这个页面。仅供记录,它曾经有效,但我转向使用 Thymeleaf,现在它可以了。它曾经看起来像这样:
<script type="text/javascript" src="js/main.js"></script>
  • 我检查了我的目录结构。 javascript 文件保存在:

    C:...\src\main\resources\static\js

  • 我查阅了文档。

  • 我做错了什么?该页面需要与javascript连接,以便用户可以向数据库提交数据。

    最佳答案

    已修复。

    出于某种原因,我必须手动进入 Eclipse 并刷新包含静态内容的包。如果您不这样做,它将不会接受更改。

    关于javascript - 如何将 Thymeleaf 链接到外部 js 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45651356/

    相关文章:

    Javascript循环+对象问题: How to get required item in object with loop

    javascript - 我想重定向到一个网站,然后点击它上面的按钮

    javascript - 调整图像大小以保持在视口(viewport)内

    javascript - jQueryclearInterval 不工作

    javascript - 我在 Thymeleaf 中有下拉 bool 选择框和 3 个选项。 True False 和 null。得到e

    java - thymeleaf 中的嵌套对象

    Spring Boot Thymeleaf 布局方言不起作用

    javascript - 如何单击下拉菜单并选择选项?

    spring - 使用 Spring Boot 与 thymeleaf 模板一起提供静态内容

    java - Thymeleaf请求参数空值问题