Thymeleaf引入

Thymeleaf是一个Java模板引擎开发库,可以处理和生成HTML、XML、JavaScript、CSS和文本,在Web和非Web环境下都可以正常工作。

1.添加依赖包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.添加配置项目

# 模板配置
# 这个开发配置为false,避免改了模板还要重启服务器
spring.thymeleaf.cache=false
# 这个是配置模板路径的,默认就是templates,可不用配置
spring.thymeleaf.prefix=classpath:/templates/
# 这个可以不配置,检查模板位置
spring.thymeleaf.check-template-location=true
# 下面3个不做解释了,可以不配置
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

# 模板的模式
spring.thymeleaf.mode=HTML5

 

 

3.添加

 @RequestMapping("/list")
    public String  listUser(Model model) {
        List<User> userList = new ArrayList<User>();
        for (int i = 0; i <10; i++) {
            userList.add(new User(i,"张三"+i,20+i,"中国广州"));
        }
        
        model.addAttribute("users", userList);
        return "/user/list";
    }

 

相关文章:

  • 2022-12-23
  • 2021-07-23
  • 2021-10-22
  • 2021-11-19
  • 2021-06-23
  • 2022-02-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-21
  • 2021-08-16
  • 2022-12-23
  • 2021-10-24
  • 2021-05-10
  • 2022-01-18
相关资源
相似解决方案