当前位置:网站首页>Thymeleaf 使用后台自定义工具类处理文本
Thymeleaf 使用后台自定义工具类处理文本
2022-07-05 14:10:00 【fengyehongWorld】
1.自定义工具类
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;
/* 给自定义工具类取一个放入Spring容器中的别名; 如果不取的话,默认为类名小写htmlUtil */
@Component("htmlHandleUtil")
public class HtmlUtil {
/** * 数字添加逗号分隔符 * * @param num 数值 * @return 添加逗号之后的数字 */
public static String formatNumber(Number num) {
return NumberUtils.format(num, 1, NumberPointType.COMMA, java.util.Locale.JAPAN);
}
/** * 将换行符转换为 <br />标签 * * @param content 转换前 * @return 转换后 */
public static String replaceLinebreak(String content) {
return StringUtils.replace(StringUtils.escapeXml(content), System.getProperty("line.separator"), "<br />");
}
}
2.后台向前台返回的数据
ZTestEntity zTestEntity = new ZTestEntity();
// 数字未添加千位分隔符
zTestEntity.setId("100000");
// 文本中含有换行符
zTestEntity.setAddress("宇宙 \r\n 银行系 \r\n 地球 \r\n 中国");
@GetMapping("/init")
public ModelAndView init() {
ZTestEntity entity = service.init();
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test");
// 后台的数据传给前台
modelAndView.addObject("entity", entity);
return modelAndView;
}
3.前台使用后台自定义的工具类处理
<!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}">
<!-- 其中htmlHandleUtil为后台工具类的bean名称 -->
<div>[[*{@htmlHandleUtil.formatNumber(id)}]]</div>
<hr>
<!--未使用工具类处理携带换行符的文本-->
<div>[[*{address}]]</div>
<hr>
<!-- 使用th:utext确保后台的html换行<br>标签不被解析为纯文本 -->
<div th:utext="*{@htmlHandleUtil.replaceLinebreak(address)}"></div>
</div>
</body>
</html>
🥳4.效果

边栏推荐
- 魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
- 分享 12 个最常用的正则表达式,能解决你大部分问题
- SSH免密码登录详解
- 故障分析 | MySQL 耗尽主机内存一例分析
- How to make a second clip of our media video without infringement
- 关于Apache Mesos的一些想法
- Use the word "new" to attract curious people
- Financial one account Hong Kong listed: market value of 6.3 billion HK $Ye wangchun said to be Keeping true and true, long - term work
- Sorter evolution of ticdc 6.0 principle
- C - Divisors of the Divisors of An Integer Gym - 102040C
猜你喜欢

TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析

如何深入理解“有限状态机”的设计思想?

治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东

Postman简介、安装、入门使用方法详细攻略!

What are the advantages and characteristics of SAS interface

国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收

The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million

区间 - 左闭右开

如何将 DevSecOps 引入企业?

Oneconnect listed in Hong Kong: with a market value of HK $6.3 billion, ye Wangchun said that he was honest and trustworthy, and long-term success
随机推荐
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
Simple process of penetration test
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
POI set the data format of the column (valid)
After the microservice project is deployed, static resources and files uploaded to upload cannot be accessed. Solution
关于memset赋值的探讨
动态规划
用“新”字来吸引好奇的人群
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
享你所想。智创未来
Tdengine biweekly selection of community issues | phase III
3W原则[通俗易懂]
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
Detailed explanation of SSH password free login
R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
Scenario based technology architecture process based on tidb - Theory