首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Spring Boot中使用Thymeleaf将值传递给HTML模板的变量

在Spring Boot中使用Thymeleaf将值传递给HTML模板的变量,可以按照以下步骤进行操作:

  1. 首先,在Spring Boot项目的pom.xml文件中添加Thymeleaf的依赖:
代码语言:txt
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 在Spring Boot的配置文件(application.properties或application.yml)中配置Thymeleaf相关的属性,例如:
代码语言:txt
复制
# 设置Thymeleaf模板文件的位置,默认为classpath:/templates/
spring.thymeleaf.prefix=classpath:/templates/

# 设置Thymeleaf模板文件的后缀,默认为.html
spring.thymeleaf.suffix=.html

# 开启Thymeleaf的缓存,默认为true
spring.thymeleaf.cache=true
  1. 创建一个Controller类,用于处理请求和返回数据。在方法中使用Model对象将数据传递给HTML模板的变量。例如:
代码语言:txt
复制
@Controller
public class MyController {

    @GetMapping("/hello")
    public String hello(Model model) {
        String message = "Hello, Thymeleaf!";
        model.addAttribute("message", message);
        return "hello";
    }
}
  1. 创建一个HTML模板文件(例如hello.html),使用Thymeleaf的语法将变量值展示在页面上。例如:
代码语言:txt
复制
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>

在上述代码中,th:text="${message}"表示将message变量的值显示在<h1>标签中。

  1. 运行Spring Boot应用程序,并访问对应的URL(例如http://localhost:8080/hello),即可看到HTML页面中显示了传递的变量值。

推荐的腾讯云相关产品:腾讯云服务器(CVM)、腾讯云数据库MySQL版、腾讯云对象存储(COS)等。

腾讯云产品介绍链接地址:

  • 腾讯云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 腾讯云数据库MySQL版:https://cloud.tencent.com/product/cdb_mysql
  • 腾讯云对象存储(COS):https://cloud.tencent.com/product/cos
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • SpringBoot当中如何整合动态html模板:Thymeleaf

    4.整合动态html模板:Thymeleaf: 光是静态html还不足够,必须html还能显示动态成分,这时我们考虑使用thymeleaf,就能完全达到springmvc的水平了,官方推荐thymeleaf。继续在上一部分的项目中,在src/main目录下,添加resources/templates/result.html:(参考目录下:bootThymeleaf) 例4.1: 1)首先在pom.xml中添加: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 注意:即使导了上面的包,也没有办法访问到resources根目录下的html。至于templates目录下的html,直接或sendRedirect都不能访问。唯有用下面的方法访问。 package com.SpringbootMaven; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @SpringBootApplication public class App { @RequestMapping("/hello") public String myhome(HttpServletResponse res,HttpSession session) throws IOException { System.out.println("spring boot springMvc 马克-to-win!"); session.setAttribute("user","马克-to-win 马克java社区创始人"); return "result"; /*下列不能再用了,在Thymeleaf框架中,sendRedirect不能跳到templates目录里的html*/ // res.sendRedirect("result.html"); } public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } } index.html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> index1 test SpringMvc </body> </html>

    00

    thymeleaf的常见问题汇总

    thymeleaf的常见问题汇总 1.thymeleaf th:href 多个参数传递格式 th:href="@{/Controller/update(param1=1,param2=${person.id})}"。就是使用逗号隔开多个参数!!! thymeleaf的th:each常见用法 一.th:eath迭代集合用法:

    03
    领券
    是否选中 编号