首页 > 解决方案 > 如何在 Thymeleaf 链接标签中转义“@”字符?

问题描述

这是我的链接:

<link th:href="@{/lib/@fortawesome/fontawesome-free/css/all.min.css}"
      href="../../static/lib/@fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet">

该文件夹以“@”符号开头。(我的 IDE IntelliJ Idea 对此提出了错误。)

更新:

我也试过这个,但它似乎不起作用:

<link th:with="linkString = '/lib/@fortawesome/fontawesome-free/css/all.min.css'" th:href="@{linkString}"
      href="../static/lib/@fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet">

在这种情况下,开发控制台显示:

index:1 Refused to apply style from 'http://localhost:8080/linkString' because its MIME type ('application/json') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

如果我更改文件夹名称并删除“@”,问题就会消失并且 CSS 会出现在页面上。

标签: intellij-ideathymeleaf

解决方案


尝试将您的链接地址保存在string变量中并将其传递到您的页面上。并在 html 中尝试如下所示。

 <link th:href="@{'/' + ${page}}" rel="stylesheet">

希望这有效!


推荐阅读