java - Thymeleaf#strings.replace 为正则表达式和 :fragment

标签 java thymeleaf

我已经保留了带有字段文本的实体。里面的文字我有部分要替换。 myEntity.text:

some text
twoImages[12_v13.PNG, 10_v6.PNG]
text text text
twoImages[12_v13.PNG, 10_v6.PNG]
<h1>And HTML</h1>

所以在 view 中我想使用类似 th:utext 的东西。但是用 th:fragment 替换图像 block (每个片段约 15 行)。

输出应该是这样的:

some text
<img .. 12_v13.PNG />
<br />
<img .. 12_v13.PNG />
<additional html />
text text text
<img .. 12_v13.PNG />
<br />
<img .. 12_v13.PNG />
<additional html />
<h1>And HTML</h1>

如何用 Thymeleaf 实现这一点?

最佳答案

如果字段的整个文本是单个字符串,您将必须使用正则表达式匹配器或某些生成的解析器来解析它。假设 [12_v13.PNG, 10_v6.PNG] 必须被解析,例如和 \\[([^,]),\\s([^\\]]*)] 第一组将给出 12_v13.PNG,第二组将给出 10_v6.PNG。

您需要在 img 标签的 src 属性中提供每个图像的路径。 您可以通过以下方式实现结果:

<img th:src="@{/images/test.png}" /> 

这意味着/images 文件夹位于项目的 webapp 文件夹内。

<img th:src="@{/resources/images/Picture.png}" />

输出为:

<img src="/resources/image/Picture.png" />

当您点击 http://localhost:8080/myapp/resources/images/Picture.png 时在您的浏览器中,您应该能够访问该图像以使上述语法起作用。

此链接很有用:Standard URL Syntax

请看一下我的 thymeleaf 演示项目:demo project (包含模板的文件夹将打开)您将找到有关项目结构的数据和 thymeleaf 模板的示例。

关于java - Thymeleaf#strings.replace 为正则表达式和 :fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45618539/

相关文章:

java - JNI : getting method id for private method in superclass

java - 是否有用于 Java 字符串的 left() 函数?

java - 泛型还是对象转换?

css - Spring MVC 无法加载 CSS,出现错误 405

java - Spring Boot Thymeleaf 3 外部 javascript 模板配置

include - 动态 Thymeleaf 包括

java - Jersey 2.22 : What is the default connect timeout for Client?

java - 简单 Java API - 错误 : Unrecognized field, 未标记为可忽略

html - Thymeleaf:附加到 html 元素/避免重复迭代

java - Spring Boot项目空指针异常