网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

springBoot怎么与Thymeleaf模板引擎结合使用

这篇文章给大家介绍spring Boot怎么与Thymeleaf模板引擎结合使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

网站制作、网站设计服务团队是一支充满着热情的团队,执着、敏锐、追求更好,是创新互联的标准与要求,同时竭诚为客户提供服务是我们的理念。创新互联把每个网站当做一个产品来开发,精雕细琢,追求一名工匠心中的细致,我们更用心!

Thymeleaf:

  1. Thymeleaf是一个java类库,他是一个xml/xhtml/html5的模板引擎,可以作为mvc的web应用的view层。
  2. Thymeleaf还提供了额外的模块与Spring MVC集成,所以我们可以使用Thymeleaf完全替代jsp。

spring Boot

  1. 通过org.springframework.boot.autoconfigure.thymeleaf包对Thymeleaf进行了自动配置。
  2. 通过ThymeleafAutoConfiguration类对集成所需要的bean进行自动配置。包括templateResolver,templateEngine,thymeleafViewResolver的配置。

下面我将演示spring boot 日常工作中常用的Thymeleaf用法。

Spring Boot 日常工作中常用Thymeleaf的用法

1:首先,在创建项目的时候选择依赖中选中Thymeleaf,或者在pom中添加依赖

    
      org.springframework.boot
      spring-boot-starter-thymeleaf
    

或者项目名-右键-add Framework Support来添加依赖jar包。如图

spring Boot怎么与Thymeleaf模板引擎结合使用 

spring Boot怎么与Thymeleaf模板引擎结合使用  

2:示例javaBean

此类用来在模板页面展示数据用。包含name和age属性。

public class Person {
  private String name;
  private Integer age;

  public Person(String name, Integer age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }
}

3.脚本样式静态文件

根据默认原则,脚本样式,图片等静态文件应放置在src/main/resources/static下,这里引入了Bootstrap和jQuery,结构如图所示:

spring Boot怎么与Thymeleaf模板引擎结合使用 

4.演示页面

根据默认原则,页面应放置在src/main/resources/templates下。在src/main/resources/templates下面新建index.html,如上图。
代码如下:




  
  
  
  
  Title


  

访问model

列表

5.数据准备

代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@Controller
@SpringBootApplication
public class ThymeleafTestApplication {
  @RequestMapping("/")
  public String index(Model model){
    Person single=new Person("aa",1);
    List people=new ArrayList();
    Person p1=new Person("bb",2);
    Person p2=new Person("cc",3);
    Person p3=new Person("dd",4);
    people.add(p1);
    people.add(p2);
    people.add(p3);
    model.addAttribute("singlePerson",single);
    model.addAttribute("people",people);
    return "index";
  }
  public static void main(String[] args) {
    SpringApplication.run(ThymeleafTestApplication.class, args);
  }


}

6.运行

访问http://localhost:8080效果如图:

spring Boot怎么与Thymeleaf模板引擎结合使用 

单击“获得名字” f12产看页面控制台打印的日志效果如图:

spring Boot怎么与Thymeleaf模板引擎结合使用

关于spring Boot怎么与Thymeleaf模板引擎结合使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章名称:springBoot怎么与Thymeleaf模板引擎结合使用
当前链接:http://bjjierui.cn/article/pssdsc.html

其他资讯