簡體   English   中英

如何從 thymeleaf javascript 內聯訪問 static 資源

[英]How to access static resources from thymeleaf javascript inline

我有一個 spring 引導項目,我需要從 javascript 代碼訪問位於我的static/images目錄中的.png圖像文件。 我正在使用 thymeleaf。 我正在使用 html 文件中的th:inline="javascript" ,如下所示:

<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    ...
       src: "<location of the image file>"
    ...
    /*]]>*/
    </script>

png 文件位於src/main/resources/static/images目錄中,javascript 程序位於src/main/resources/templates目錄中的 html 文件中。 我知道我可以使用絕對路徑來使用它,但是當它在生產中使用 go 時就不一樣了,所以我必須再次更改它。 Thymeleaf has th:src to load static file from a context-based path, but it works only from html tags, is there anything similar for javascript?

您可以在內聯表達式中使用與常規 Thymeleaf 表達式中相同的標准 URL 語法。

<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
let src = /*[[@{/path/to/image.png}]]*/ null;
/*]]>*/
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM