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

边栏推荐
- Lepton 无损压缩原理及性能分析
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the labs parameter to customize the X axis label text (customize X axis labels)
- 【学习笔记】图的连通性与回路
- 家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
- VC development of non MFC program memory leak tracking code
- R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
- Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
- 明峰医疗冲刺科创板:年营收3.5亿元 拟募资6.24亿
- 清大科越冲刺科创板:年营收2亿 拟募资7.5亿
- 强联通分量
猜你喜欢

Sorter evolution of ticdc 6.0 principle

SAS接口有什么优势特点

CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕

周大福践行「百周年承诺」,真诚服务推动绿色环保

分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少

What is the future development trend of neural network Internet of things

Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products

快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力

物联网应用技术专业是属于什么类

乌卡时代下,企业供应链管理体系的应对策略
随机推荐
Simple process of penetration test
Postman简介、安装、入门使用方法详细攻略!
鸿蒙第四次培训
poi设置列的数据格式(有效)
04_solr7.3之solrJ7.3的使用
WebRTC的学习(二)
Introduction, installation, introduction and detailed introduction to postman!
Tiflash compiler oriented automatic vectorization acceleration
无密码身份验证如何保障用户隐私安全?
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
明峰医疗冲刺科创板:年营收3.5亿元 拟募资6.24亿
怎么叫一手一机的功能方式
做自媒體視頻二次剪輯,怎樣剪輯不算侵權
神经网络物联网未来发展趋势怎么样
Hongmeng fourth training
R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
Linux下mysql数据库安装教程
让秒杀狂欢更从容:大促背后的数据库(下篇)
Some ideas about Apache mesos
MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)