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

边栏推荐
- What are the advantages and characteristics of SAS interface
- Google EventBus 使用详解
- PHP5下WSDL,SOAP调用实现过程
- 动态规划
- 汇编语言 assembly language
- 基于 TiDB 场景式技术架构过程 - 理论篇
- 做自媒體視頻二次剪輯,怎樣剪輯不算侵權
- R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
- 关于memset赋值的探讨
- 04_ Use of solrj7.3 of solr7.3
猜你喜欢

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

Tiflash compiler oriented automatic vectorization acceleration

Scenario based technology architecture process based on tidb - Theory

IP packet header analysis and static routing

LeetCode_ 2 (add two numbers)

Simple process of penetration test

tidb-dm报警DM_sync_process_exists_with_error排查

网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理

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

Detailed explanation of IP address and preparation of DOS basic commands and batch processing
随机推荐
Webrtc learning (II)
mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
SSH免密码登录详解
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
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)
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
矩阵链乘 - 动态规划实例
R语言dplyr包select函数、group_by函数、mutate函数、cumsum函数计算dataframe分组数据中指定数值变量的累加值、并生成累加数据列
OSI and tcp/ip protocol cluster
Sorter evolution of ticdc 6.0 principle
故障分析 | MySQL 耗尽主机内存一例分析
Why do mechanical engineers I know complain about low wages?
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
03_ Dataimport of Solr
Introduction, installation, introduction and detailed introduction to postman!
强联通分量
Discussion on memset assignment
一网打尽异步神器CompletableFuture