thymeleaf模板和字符串模板

本文详细介绍了Thymeleaf模板引擎在Spring Boot框架中的应用,包括如何导入依赖、在Controller层使用Model绑定值、在前端页面展示数据及循环列表、拼接超链接等核心操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

thymeleaf模板引擎的运用

1.先导入依赖(spring boot 框架)
2. controller层 (用model绑定值)
public String get(Long id,Model model) {
Student student = iStudentService.get(id);
model.addAttribute(“student”,student);
return “showById”;
}
3. 前端页面接受值并赋值给表单(语法: th:value=" s t u d e n t . n a m e " ) 姓 名 : < i n p u t c l a s s = " b " t y p e = " t e x t " i d = " n a m e " t h : v a l u e = " {student.name}") 姓名: <input class="b" type="text" id="name" th:value=" student.name"<inputclass="b"type="text"id="name"th:value="{student.name}">

前端页面接受值并循环展示列表
<tr th:each="stu : ${student}">
        	<td th:text="${stu.id}"></td>
        	<td th:text="${stu.name}"></td>
        	<td th:text="${stu.age}"></td>
        	<td th:text="${stu.weight}"></td》
	</tr>

4.thymeleaf拼接超链接语法
例:点击按钮跳转页面,同时带走这一行的id值(通过id 执行selectById语句,显示在页面上)

<td><a th:href="@{/toupd(id=${stu.id})}">修改</a></td>       (toupd是方法名,stu是模板定义的对象)
百度的语法:<dd><a th:href="@{/get/{id}(id=${user.id})}">基本资料</a></dd>

字符串模板

语法:String text (两个反单引号)
String text ${expression} string text string text line2 ${expression} string text

多行字符串并且取表达式的值
例:var a=20;
        var b="jquery";
        var str = `My age is $(a) and i love ${c}`;    console.log(str);
    原始方法:var str = “My age is” + a  “and i love ”  + c`;    console.log(str);
### 使用 Thymeleaf 解析 HTML 字符串 为了在 Java Spring 应用程序中使用 Thymeleaf 来解析 HTML 字符串,可以创建一个 `StringTemplateResolver` 并将其配置到 `TemplateEngine` 中。这允许应用程序处理动态生成的模板字符串而不是文件中的静态模板。 下面是一个简单的例子来展示如何实现这一点: #### 配置 Template Resolver Engine ```java import org.springframework.context.annotation.Bean; import org.thymeleaf.TemplateEngine; import org.thymeleaf.templateresolver.StringTemplateResolver; public class ThymeleafConfig { @Bean public StringTemplateResolver templateResolver() { final StringTemplateResolver templateResolver = new StringTemplateResolver(); templateResolver.setOrder(1); // 设置前缀后缀为空白字符,因为我们直接传递完整的HTML作为参数 templateResolver.setPrefix(""); templateResolver.setSuffix(""); templateResolver.setTemplateMode("HTML"); templateResolver.setCharacterEncoding("UTF-8"); templateResolver.setCacheable(false); // 如果希望每次请求都重新编译,则设置为false return templateResolver; } @Bean public TemplateEngine templateEngine(final StringTemplateResolver templateResolver) { final TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; } } ``` #### 处理并渲染 HTML 字符串 一旦有了上述 Bean 的定义,在服务层或其他组件内就可以通过注入 `TemplateEngine` 实例来进行 HTML 字符串的解析工作了: ```java @Service public class HtmlService { private final TemplateEngine templateEngine; @Autowired public HtmlService(TemplateEngine templateEngine){ this.templateEngine = templateEngine; } public String processHtml(String htmlContent, Map<String,Object> variables){ Context context = new Context(Locale.getDefault(),variables); return templateEngine.process(htmlContent,context)[^2]; } } ``` 在这个例子中,`processHtml()` 方法接收两个参数:一个是待解析的 HTML 文本;另一个是要应用到该文本上的变量映射表。这些变量会被替换掉 HTML 中对应的占位符位置,从而完成最终页面内容的组装。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值