当前位置:网站首页>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
边栏推荐
- Crud of MySQL
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
- MongDB学习笔记
- Two policemen were shot dead in a "safety accident" in Philadelphia, USA
- NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
- Un week - end heureux
- Long list optimized virtual scrolling
- 想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
- 【招聘岗位】基础设施软件开发人员
- [12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
猜你喜欢
市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
Pointer operation - C language
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
Super wow fast row, you are worth learning!
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
微帧科技荣获全球云计算大会“云鼎奖”!
Crud of MySQL
Mysql---- function
[JVM] operation instruction
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
随机推荐
【jvm】运算指令
APR protocol and defense
百亿按摩仪蓝海,难出巨头
想进阿里必须啃透的12道MySQL面试题
JMeter performance test: serveragent resource monitoring
I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
Shanghai under layoffs
FR练习题目---简单题
Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
Explain Vue's plan to clean up keepalive cache in time
P1451 求细胞数量/1329:【例8.2】细胞
开挖财上的证券账户可以吗?安全吗?
How to open an account of qiniu securities? Is it safe to open an account?
计算中间件 Apache Linkis参数解读
在Pytorch中使用Tensorboard可视化训练过程
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
PostgreSQL 13 installation
Total amount analysis accounting method and potential method - allocation analysis
两个BI开发,3000多张报表?如何做的到?