【问题标题】:Thymeleaf parse text and execute in-text expressionsThymeleaf 解析文本并执行文本表达式
【发布时间】:2017-09-15 11:16:42
【问题描述】:

我有包含链接的文本字符串,例如 <a th:href="'someLink'">Download</a>

我需要处理该文本并将th:href="'someLink'" 替换为正确的链接以显示带有<a href="someLink">Download</a> 的文本。

带有链接的文本存储在变量textThatContainsLinks中。

我显示文本的代码是<div th:utext="${textThatContainsLinks}">。我还尝试使用 <div th:utext="${__textThatContainsLinks__}"> 之类的预处理。

目前这段代码显示的链接不是我预期的,而是未经预处理的,即现在​​的输出是<a th:href="'someLink'">Download</a>

在显示之前如何预处理文本中的表达式?

非常感谢!

【问题讨论】:

  • 为什么不能写下载
  • 写在哪里?在包含 HTML 的变量中,包含 th: 表达式?
  • 好的,我想我现在明白了你想要做什么。 textThatContainsLinks 是此类锚元素的数组吗?
  • 带有链接的文本存储在变量 textThatContainsLinks 中。即,这是包含链接的 HTML 变量,可能与 <a th:href="'someLink'">Download</a> 相同

标签: spring thymeleaf


【解决方案1】:

获取上下文路径并将其直接附加到纯 html5 属性的相对路径,例如 <a href="/contextPath/relative/path">LINK</a><img src="/contextPath/relative/path/image.jpg" width="50" height="50" alt="logo"/>。 注意资源的可访问性是多么简单:/contextPath/relativePath,所以最重要的路径是相对路径。这类似于Thymeleaf was unable to render <img> tag when sent from database table。我观察到,一旦 thymeleaf 的命名空间 th: 符合位于文本/字符串内的 hrefsrc 属性,则绝对路径无法正确解析。

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多