当前位置:网站首页>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

边栏推荐
- Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
- [detailed explanation of Huawei machine test] happy weekend
- C language -- structure and function
- 729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
- FR练习题目---综合题
- Longest common subsequence dynamic programming
- Fr exercise topic --- comprehensive question
- 想进阿里必须啃透的12道MySQL面试题
- Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
- 超级哇塞的快排,你值得学会!
猜你喜欢

Run faster with go: use golang to serve machine learning

【NVMe2.0b 14-9】NVMe SR-IOV

【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !

Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development

当代人的水焦虑:好水究竟在哪里?

Section - left closed right open

MongDB学习笔记

CPU design related notes

Topology可视化绘图引擎

Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
随机推荐
你童年的快乐,都是被它承包了
在Pytorch中使用Tensorboard可视化训练过程
Shanghai under layoffs
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
Interpretation of Apache linkage parameters in computing middleware
两个BI开发,3000多张报表?如何做的到?
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
B站做短视频,学抖音死,学YouTube生?
30岁汇源,要换新主人了
Run faster with go: use golang to serve machine learning
想进阿里必须啃透的12道MySQL面试题
How to choose the appropriate certificate brand when applying for code signing certificate?
Disjoint Set
Machine learning notes - gray wolf optimization
Type declaration of all DOM elements in TS
【招聘岗位】软件工程师(全栈)- 公共安全方向
[detailed explanation of Huawei machine test] happy weekend
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数