您的位置:首页 > 教育 > 培训 > 第二节:如何使用thymeleaf渲染html(自学Spring boot 3.x的第一天)

第二节:如何使用thymeleaf渲染html(自学Spring boot 3.x的第一天)

2024/10/5 13:55:41 来源:https://blog.csdn.net/qq_21004057/article/details/140042125  浏览:    关键词:第二节:如何使用thymeleaf渲染html(自学Spring boot 3.x的第一天)

大家好,我是网创有方,今天来学习如何使用thymeleaf渲染html。该模板运用不广泛,所以本节内容了解既可。

第一步:创建html文件。

在模板templates目录下创建一个html文件。

编写代码如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="www.thymeleaf.org"/>
<head><meta charset="UTF-8"><title>thymeleaf演示</title>
</head>
<body>
<p th:text="${description}"/>thymeleaf
</body>
</html>

第二步:编写动态渲染属性值。

并返回渲染的html文件。

package cn.wcyf.wcai;import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@SpringBootApplicationpublic class WcaiApplication {public static void main(String[] args) {SpringApplication.run(WcaiApplication.class, args);}@Controllerpublic static  class HelloController{@GetMapping("/test")public String test(HttpServletRequest request){//设置请求的属性request.setAttribute("description","你好啊 ");return "index";}}}

第三步:点击运行

接下来访问localhost:test

参考:《深入浅出spring-boot 3.x》

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com