解析文本并执行文本内表达式

解析文本并执行文本内表达式

本文介绍了Thymeleaf 解析文本并执行文本内表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含链接的文本字符串,例如 .我观察到,一旦 thymeleaf 的命名空间 th: 限定了位于文本/字符串中的 hrefsrc 属性,则绝对路径无法正确解析.

I have text string, that contains links, for example, like <a th:href="'someLink'">Download</a> .

I need to process that text and replace th:href="'someLink'" with correct links to show text with <a href="someLink">Download</a>.

The text with links is stored in variable textThatContainsLinks.

My code to show text is <div th:utext="${textThatContainsLinks}">. I also tried to use preprocessing like <div th:utext="${__textThatContainsLinks__}">.

Currently this code shows links not as I expected, but non-preprocessed, ie, output is <a th:href="'someLink'">Download</a> now.

How to pre-process expressions in text, before showing it?

Thank you very much!

解决方案

Take the context path and directly attach it to the relative path of a pure html5 attribute e.g <a href="/contextPath/relative/path">LINK</a>, <img src="/contextPath/relative/path/image.jpg" width="50" height="50" alt="logo"/>.Notice how simple the accessibility to the resource is: /contextPath/relativePath, so the most important path there is the relative path. This is similar to Thymeleaf was unable to render <img> tag when sent from database table. I observed that once thymeleaf's namespace th: qualifies a href or src attribute that resides inside a text/String the absolute path is not properly resolved.

这篇关于Thymeleaf 解析文本并执行文本内表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:41