thymeleaf html转义,Thymeleaf字符串转义

本文章将介绍Thymeleaf标准表达式语法中的概念。

学习如何在Thymeleaf模板中显示转义值。

已将HTML代码片段设置为上下文模型,并将其作为变量名为html的字符串。在第一个div中显示HTML转义字符串,在第二个div中显示未转义字符串。

如果要上机实践,请参考:。这里不再重复创建项目的过程,这里将只介绍如何使用Thymeleaf标准表达式和标签。

这里创建一个Maven Web项目: thymeleaf-tutorials ,其目录结构如下所示 –

e7c3b0b8ceac7fc3c59a0a670cef8f25.png

控制器类的实现:MyController.java –

package com.yiibai.spring.controller; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import com.yiibai.spring.bean.Product; @Controller public class MyController { @GetMapping("/") public String index(Model model) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Product product = new Product("花生油", 129, sdf.parse("2018-02-18")); model.addAttribute("product", product); return "index"; } @GetMapping("/escape") public String escape(Model model) throws ParseException { String html = "Welcome to our fantastic grocery store!"; model.addAttribute("html", html); return "escape"; } }

模板文件的实现:/webapp/WEB-INFO/views/escape.html –

SpringMVC5+Thymeleaf示例

Spring MVC5 + Thymeleaf 字符串转义示例

Some escaped text
Some unescaped text

运行上面项目,在浏览器中显示效果如下 –

bc54bd7cd2413c5b013ee70a81e4667f.png

¥ 我要打赏   纠错/补充 收藏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值