thymeleaf -模板生成的页面内容格式

界面textarea中文本编辑好,传递给Thymeleaf模板生成页面,回车,空行,空格等格式改变。
解决方式:
1.在Thymeleaf替换到格式转义字符为 、<br/>等。
2.在1处理完后展示的空白任然不一致,原因可能为界面与Thymeleaf对应部分的font-family不一致,调整一致即可。

thymeleaf -邮件中的页面保持原来格式
思路使用replace与转义工具类替换回车、空格的转义字符为对应的html标签。

<div class="text" th:utext="${#strings.unescapeJava(#strings.replace(#strings.escapeJava(content),"","<br/>"))}">
</div> content为传入要显示的字符串。 th:utext表示会解析内容中的html内容。 escapeJava/unescapeJava转义与反转义。

thymeleaf—内容格式

使用如下:
<span th:utext="${#strings.unescapeJava(#strings.replace(#strings.escapeJava(textContent),"\n","<br>"))}"></span>
说明:
1.th:utext,表示不已纯文本解析。
2.#strings.unescapeJava与#strings.escapeJava,解码与编码。
3.#strings.replace将"\n"替换为<br>完成换行。
4.textContent为模板变量值。

 

参考链接:https://www.cnblogs.com/Zender/p/8709031.html
参考链接:https://kb.kutu66.com/spring/post_12856118
参考链接:https://www.cnblogs.com/xiaohu1218/p/9634126.html

posted @ 2021-02-19 22:39  对月当歌  阅读(805)  评论(0)    收藏  举报