thymeleaf 使用示例


thymeleaf 使用示例

 

 

**********************

示例

 

**************

pojo  层

 

Person

@Data
public class Person {

    private Integer id;
    private String name;
    private Integer age;
}

 

**************

controller 层

 

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(ModelAndView mv){
        Person person=new Person();
        person.setId(1);
        person.setName("瓜田李下");
        person.setAge(20);
        mv.addObject("person",person);

        List<Person> list=new ArrayList<>();
        for (int i=0;i<5;i++){
            Person p=new Person();
            p.setId(i);
            p.setName("海贼王 "+i);
            p.setAge(20+i);

            list.add(p);
        }
        mv.addObject("list",list);

        mv.setViewName("hello");
        return mv;
    }
}

 

 

**************

前端页面

 

hello.html

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:align="center" th:if="${person} != null" style="color: greenyellow">
    <span th:text="${person.getId()}"></span> ==> 
    <span th:text="${person.getName()}"></span> ==> 
    <span th:text="${person.getAge()}"></span>
</div><br>

<div th:align="center" th:if="not ${#lists.isEmpty(list)}" style="color: orange">
    <div th:each="person:${list}">
        <span th:text="${person.getId()}"></span> ==> 
        <span th:text="${person.getName()}"></span> ==> 
        <span th:text="${person.getAge()}"></span>
    </div>
</div>
</body>
</html>

 

 

**********************

使用测试

 

localhost:8080/hello

                          

 

 

好的,下面是一个简单的使用Thymeleaf模板引擎的Spring Boot示例: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 创建模板文件 在 src/main/resources/templates 目录下创建一个 HTML 文件,例如:index.html。在该文件中使用 Thymeleaf 模板语法。 例如: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf Demo</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 在上面的代码中,${message} 表示使用 Thymeleaf 的变量表达式,它会被替换为 Controller 中设置的 message 值。 3. 创建 Controller 创建一个 Spring Boot 控制器类,并在其中设置模板所需的数据。 例如: ```java @Controller public class DemoController { @GetMapping("/") public String index(Model model) { model.addAttribute("message", "Hello, Thymeleaf!"); return "index"; } } ``` 在上述代码中,使用 @Controller 注解表示这是一个控制器类,使用 @GetMapping("/") 注解表示访问根路径时,调用 index 方法。在 index 方法中,使用 Model 对象将 message 值设置为 "Hello, Thymeleaf!"。 4. 运行应用程序 运行应用程序,然后在浏览器中访问 http://localhost:8080/,你应该可以看到一个包含 "Hello, Thymeleaf!" 文本的页面。 以上就是一个简单的 Spring Boot 案例使用 Thymeleaf 模板引擎。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值