当前位置:网站首页>Thymeleaf uses background custom tool classes to process text
Thymeleaf uses background custom tool classes to process text
2022-07-05 14:58:00 【fengyehongWorld】
1. Custom tool class
package com.example.jmw.utils;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.NumberPointType;
import org.thymeleaf.util.NumberUtils;
import org.thymeleaf.util.StringUtils;
/* Take a custom tool class and put it in Spring Alias in container ; If you don't take it , Default to lowercase class name htmlUtil */
@Component("htmlHandleUtil")
public class HtmlUtil {
/** * Add a comma separator to the number * * @param num The number * @return Add the number after the comma */
public static String formatNumber(Number num) {
return NumberUtils.format(num, 1, NumberPointType.COMMA, java.util.Locale.JAPAN);
}
/** * Convert line breaks to <br /> label * * @param content Before conversion * @return After the transformation */
public static String replaceLinebreak(String content) {
return StringUtils.replace(StringUtils.escapeXml(content), System.getProperty("line.separator"), "<br />");
}
}
2. The data returned from the background to the front
ZTestEntity zTestEntity = new ZTestEntity();
// The number has no thousand separator
zTestEntity.setId("100000");
// The text contains line breaks
zTestEntity.setAddress(" The universe \r\n Banking department \r\n The earth \r\n China ");
@GetMapping("/init")
public ModelAndView init() {
ZTestEntity entity = service.init();
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test");
// The data from the background is transferred to the foreground
modelAndView.addObject("entity", entity);
return modelAndView;
}
3. The foreground uses the tool class customized by the background to process
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:object="${entity}">
<!-- among htmlHandleUtil For background tools bean name -->
<div>[[*{@htmlHandleUtil.formatNumber(id)}]]</div>
<hr>
<!-- No tool class is used to deal with text with line breaks -->
<div>[[*{address}]]</div>
<hr>
<!-- Use th:utext Ensure that the html Line break <br> Tags are not parsed as plain text -->
<div th:utext="*{@htmlHandleUtil.replaceLinebreak(address)}"></div>
</div>
</body>
</html>
🥳4. effect

边栏推荐
- Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
- 729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
- Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
- Two Bi development, more than 3000 reports? How to do it?
- [detailed explanation of Huawei machine test] character statistics and rearrangement
- 12 MySQL interview questions that you must chew through to enter Alibaba
- CPU design related notes
- Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
- 1330:【例8.3】最少步数
- [12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
猜你喜欢

qt creater断点调试程序详解

Machine learning notes - gray wolf optimization

Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de

P1451 求细胞数量/1329:【例8.2】细胞

Mongdb learning notes
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

Under the crisis of enterprise development, is digital transformation the future savior of enterprises

Interpretation of Apache linkage parameters in computing middleware

Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology

Niuke: intercepting missiles
随机推荐
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
百亿按摩仪蓝海,难出巨头
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
当代人的水焦虑:好水究竟在哪里?
Implement a blog system -- using template engine technology
leetcode:881. lifeboat
Change multiple file names with one click
PostgreSQL 13 installation
想进阿里必须啃透的12道MySQL面试题
30岁汇源,要换新主人了
Interview shock 62: what are the precautions for group by?
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
【jvm】运算指令
Postgresql 13 安装
qt creater断点调试程序详解
Longest common subsequence dynamic programming
Jmeter性能测试:ServerAgent资源监控
Brief introduction of machine learning framework
超级哇塞的快排,你值得学会!
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems