当前位置:网站首页>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.效果
边栏推荐
- R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to each vari
- Mysql database installation tutorial under Linux
- 金融壹賬通香港上市:市值63億港元 葉望春稱守正篤實,久久為功
- 用“新”字来吸引好奇的人群
- 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
- 世界环境日 | 周大福用心服务推动减碳环保
- Leetcode array question brushing notes
- 分享 12 个最常用的正则表达式,能解决你大部分问题
- 国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
- 日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
猜你喜欢
如何深入理解“有限状态机”的设计思想?
Why do mechanical engineers I know complain about low wages?
清大科越冲刺科创板:年营收2亿 拟募资7.5亿
Postman简介、安装、入门使用方法详细攻略!
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
TDengine 社区问题双周精选 | 第三期
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
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
Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
Sorter evolution of ticdc 6.0 principle
随机推荐
04_ Use of solrj7.3 of solr7.3
The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
Implementation process of WSDL and soap calls under PHP5
世界环境日 | 周大福用心服务推动减碳环保
Sorter evolution of ticdc 6.0 principle
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
The forked VM terminated without saying properly goodbye
How to introduce devsecops into enterprises?
鸿蒙第四次培训
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
3W原则[通俗易懂]
做自媒体视频二次剪辑,怎样剪辑不算侵权
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
Thymeleaf 常用函数
神经网络物联网未来现状和趋势及看法
-Web direction attack and defense world
Current situation, trend and view of neural network Internet of things in the future
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
How to make a second clip of our media video without infringement
What is the ranking of GF futures? Is it safe and reliable to open an account for GF futures online?