【问题标题】:Javascript not loading on internal thymeleaf linkJavascript 未加载到内部 thymeleaf 链接上
【发布时间】:2023-03-12 16:16:01
【问题描述】:

当我打开一个新表单时,所有的 js 都可以工作,在编辑时它无法加载所有的内部 js。请在所有数据和路由都正确连接的假设下工作。

我有一个链接可以打开一个包含现有对象的表单:

<a th:if="${foo.typeName} == a" href="Edit"
       th:href="@{/aFoo/{id}(id=${foo.id})}">view</a>

控制器方法:

新对象:

@RequestMapping(value="/aFoo", method=RequestMethod.GET)
    public String newAFooForm(Model model) {
        FooViewModel fooViewModel = new FooViewModel(Type.A);
        model.addAttribute("fooViewModel", fooViewModel);
        return "fooForm";
    }

加载现有对象:

@RequestMapping(value="/aFoo/{id}", method=RequestMethod.GET)
    public String editAFooForm(@PathVariable("id") Long id, Model model) {
        FooViewModel fooViewModel = assignFooViewModel(id);
        model.addAttribute("fooViewModel", fooViewModel);
        return "fooForm";
    }

表单 html/th 代码无关紧要,但要描述我的 js 引用:

layout.html 是两种表单的容器。它有 js 导入:

<script th:src="@{js/jquery-1.11.3.min.js}"></script>
<script th:src="@{js/bootstrap.min.js}"></script>
<script th:src="@{js/parsley.min.js}"></script>

以及包含页面上的 js 模板:

<div layout:fragment="script"></div>

fooForm.html 有额外的 js 导入,然后是大量的 js 代码,它开始了:

<div layout:fragment="script">
    <script th:src="@{js/typeahead.bundle.min.js}"></script>
    <script th:src="@{js/handlebars.js}"></script>
    <script>
    ....
    </script>
</div>

我的第一个想法是我以某种方式编写了错误的链接,因此它丢失了引用 js 的上下文。有什么想法吗?谢谢。

【问题讨论】:

  • 发布为 script 标签生成的 HTML 代码。

标签: javascript spring-boot thymeleaf


【解决方案1】:

js 导入的格式应该是:

<script href="/js/handlebars.js" th:src="@{/js/handlebars.js}"></script>

【讨论】:

  • 并非如此。 th:src 就足够了。另请注意href 不是script 标签的属性。您在这里缺少其他东西。
猜你喜欢
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 2021-04-26
  • 1970-01-01
  • 2020-09-19
  • 2019-08-07
  • 2019-10-05
  • 2018-12-06
相关资源
最近更新 更多