当前位置:网站首页>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.效果
边栏推荐
- Leetcode array question brushing notes
- 01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
- Laravel - model (new model and use model)
- 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
- 判断变量是否为数组
- Time to calculate cron expression based on cronsequencegenerator
- R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
- VC开发非MFC程序内存泄漏跟踪代码
- 03_ Dataimport of Solr
- 快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
猜你喜欢
治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东
Current situation, trend and view of neural network Internet of things in the future
乌卡时代下,企业供应链管理体系的应对策略
Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
明峰医疗冲刺科创板:年营收3.5亿元 拟募资6.24亿
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多租户解决方案,构建全产业链数字化营销竞争力
物联网应用技术专业是属于什么类
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
随机推荐
What is the ranking of GF futures? Is it safe and reliable to open an account for GF futures online?
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
鸿蒙第四次培训
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
C语言中限定符的作用
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
让秒杀狂欢更从容:大促背后的数据库(下篇)
强联通分量
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
世界环境日 | 周大福用心服务推动减碳环保
Discussion on memset assignment
SAS接口有什么优势特点
poi设置列的数据格式(有效)
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
Time to calculate cron expression based on cronsequencegenerator
3W原则[通俗易懂]
Webrtc learning (II)
不相交集
Implementation process of WSDL and soap calls under PHP5