如何将代码 thymeleaf 放入外部 javascript 文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27176640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:04:39  来源:igfitidea点击:

How to put code thymeleaf in an external javascript file?

javascriptthymeleaf

提问by Premier

I have an external javascript file which is declared in my html file with the following tag:

我有一个外部 javascript 文件,它在我的 html 文件中使用以下标签声明:

<script type="text/javascript" th:inline="javascript" th:src="@{/js/gp-aprobarDocumento.js}"></script>

and in gp-aprobarDocumento.jsthe code shown below:

并在gp-aprobarDocumento.js下面显示的代码中:

ventanaAprobacion = function(td) 
{
  /*<![CDATA[*/
    idEntregable = $(td).attr("data-row-id");
    idVersion = $(td).attr("data-row-version");
    alert("la siguiente viene con el texto dle properties");
    alert(/*[[${link.menu.page-certificacion-qa-bandeja-entrada}]]*/);
    $(function() {
        $("#dialog-aprobar-documento").dialog("open");
    });
  /*]]>*/
}

Thus when executed the function the window alert is shown empty.

因此,当执行该功能时,窗口警报显示为空。

Does somebody know how to put thymeleaf expression in a external javascript?

有人知道如何将 thymeleaf 表达式放入外部 javascript 中吗?

回答by Manu Zi

I think what you want to do it's not possible, I have a similar question (here:How do you access a model attribute with javascript variable)

我认为你想做什么是不可能的,我有一个类似的问题(这里:你如何使用 javascript 变量访问模型属性

but in your case you can do something like the this:

但在您的情况下,您可以执行以下操作:

in html:

在 html 中:

<script type="text/javascript" th:inline="javascript" >
    var alertVariable = ${link.menu.page-certificacion-qa-bandeja-entrada};
</script>

and in the javascript the following:

并在 javascript 中:

ventanaAprobacion = function(td) 
{
    ...
    alert(alertVariable);
    ...
}

I know that's not really what you want but I have the same problem and I don't think there is any solution.

我知道这不是你真正想要的,但我有同样的问题,我认为没有任何解决方案。

回答by Pat

Via DOM:

通过 DOM:

https://datatables.net/examples/data_sources/js_array.html

https://datatables.net/examples/data_sources/js_array.html

If you're looking to create a JS variable from a Thymeleaf object you can add said object to the DOM. I recently did a project where I returned query results to a Java object of type List<> and added that object to the DOM through my Spring Controller.

如果您想从 Thymeleaf 对象创建一个 JS 变量,您可以将所述对象添加到 DOM。我最近做了一个项目,我将查询结果返回到 List<> 类型的 Java 对象,并通过 Spring Controller 将该对象添加到 DOM。

  //Deliver Results Array to the DOM
  model.addAttribute("myResult", myResult);

After this object is added to your Thymleaf template model you can access it in your HTML as

将此对象添加到您的 Thymleaf 模板模型后,您可以在 HTML 中访问它

  th:text="${myResult}"

You can also reference it in your Javascript by simply referencing the name of the model object from the DOM. I haven't been able to get the variable to populate in the seperate JS file without making it global in scope from the HTML file with:

您还可以通过简单地从 DOM 中引用模型对象的名称,在您的 Javascript 中引用它。我无法让变量填充到单独的 JS 文件中,而没有使其在 HTML 文件中的全局范围内使用:

<script th:inline="javascript">
    var myResult = [[${myResult}]];
</script>

My JS file looks like this

我的 JS 文件看起来像这样

  $(function(){

  //Get Thymeleaf DOM Object
  console.log(myResult);
  });

Returning Result from DOM

从 DOM 返回结果

Th object in question must be reference-able from within the DOM. You may have better performance with AJAX and creating a controller that returns the data to the client over HTTP. Seems like thymeleaf 3 has other solutions as well : https://github.com/thymeleaf/thymeleaf/issues/395

所讨论的对象必须是可从 DOM 中引用的。使用 AJAX 并创建一个通过 HTTP 将数据返回给客户端的控制器,您可能会获得更好的性能。似乎 thymeleaf 3 也有其他解决方案:https: //github.com/thymeleaf/thymeleaf/issues/395

Hope this helps!

希望这可以帮助!

回答by jpllosa

This worked for me. In my index.html:

这对我有用。在我的index.html

<script th:inline="javascript">
    /* variable used by index.js */
    var referenceId = [[${referenceId}]];
</script>
<script type="text/javascript" th:src="@{/js/index.js}">
</script>

then in my index.js:

然后在我的index.js

function doSomething() {        
    $.ajax({
        type: 'GET',
        url: '/api/' + referenceId ,
        contentType: 'application/json',
        beforeSend: beforeSend
    })
}

Hope this helps.

希望这可以帮助。