当前位置:网站首页>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
边栏推荐
- Two policemen were shot dead in a "safety accident" in Philadelphia, USA
- 在Pytorch中使用Tensorboard可视化训练过程
- CODING DevSecOps 助力金融企业跑出数字加速度
- GPS原始坐标转百度地图坐标(纯C代码)
- 长列表优化虚拟滚动
- 我这边同时采集多个oracle表,采集一会以后,会报oracle的oga内存超出,大家有没有遇到的?
- Interview shock 62: what are the precautions for group by?
- maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
- 【华为机试真题详解】字符统计及重排
- Mongdb learning notes
猜你喜欢
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
Topology visual drawing engine
实现一个博客系统----使用模板引擎技术
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Section - left closed right open
一键更改多个文件名字
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
社区团购撤城“后遗症”
leetcode:881. 救生艇
Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
随机推荐
Niuke: intercepting missiles
Structure - C language
easyOCR 字符識別
Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
启牛证券账户怎么开通,开户安全吗?
漫画:程序员不是修电脑的!
裁员下的上海
Install and configure Jenkins
用 Go 跑的更快:使用 Golang 为机器学习服务
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
Topology visual drawing engine
[recruitment position] infrastructure software developer
Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
注意!软件供应链安全挑战持续升级
手写promise与async await
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
【jvm】运算指令
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
useMemo,memo,useRef等相关hooks详解
【NVMe2.0b 14-9】NVMe SR-IOV