当前位置:网站首页>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
- The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
- [buuctf.reverse] 152-154
- openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
- ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
- Scenario based technology architecture process based on tidb - Theory
- 判断变量是否为数组
- Sqllab 1-6 exercise
- 04_ Use of solrj7.3 of solr7.3
- OSI and tcp/ip protocol cluster
猜你喜欢

家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级

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

What category does the Internet of things application technology major belong to

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

How to introduce devsecops into enterprises?

Redis如何实现多可用区?

世界环境日 | 周大福用心服务推动减碳环保

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

非技术部门,如何参与 DevOps?

Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
随机推荐
Some ideas about Apache mesos
汇编语言 assembly language
Getting started with rce
TiFlash 面向编译器的自动向量化加速
明峰医疗冲刺科创板:年营收3.5亿元 拟募资6.24亿
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
故障分析 | MySQL 耗尽主机内存一例分析
瑞能实业IPO被终止:年营收4.47亿 曾拟募资3.76亿
清大科越冲刺科创板:年营收2亿 拟募资7.5亿
动态规划
Use the word "new" to attract curious people
Discussion on memset assignment
LeetCode_ 67 (binary sum)
R语言dplyr包select函数、group_by函数、mutate函数、cumsum函数计算dataframe分组数据中指定数值变量的累加值、并生成累加数据列
国富氢能冲刺科创板:拟募资20亿 应收账款3.6亿超营收
03_Solr之dataimport
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
Why do mechanical engineers I know complain about low wages?
Sharing the 12 most commonly used regular expressions can solve most of your problems
upload (1-6)