当前位置:网站首页>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
边栏推荐
猜你喜欢
Security analysis of Web Architecture
Niuke: intercepting missiles
Fr exercise topic --- comprehensive question
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Pointer operation - C language
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
【NVMe2.0b 14-9】NVMe SR-IOV
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
【NVMe2.0b 14-9】NVMe SR-IOV
百亿按摩仪蓝海,难出巨头
随机推荐
【华为机试真题详解】字符统计及重排
Using tensorboard to visualize the training process in pytoch
Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
easyOCR 字符识别
Penetration testing methodology
想进阿里必须啃透的12道MySQL面试题
mysql8.0JSON_ Instructions for using contains
CPU设计实战-第四章实践任务二用阻塞技术解决相关引发的冲突
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
CODING DevSecOps 助力金融企业跑出数字加速度
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
Shanghai under layoffs
【C 题集】of Ⅷ
你童年的快乐,都是被它承包了
STM32+BH1750光敏传感器获取光照强度
R 熵权法计算权重及综合得分
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)