SpringBoot整合Thymeleaf

2019-06-25  本文已影响0人  _灵风y

Thymeleaf是新一代的Java模版引擎,支持HTML原型,既可以让前端工程师在浏览器中直接打开查看样式,也可以让后端工程师结合数据查看真实的效果。同时SpringBoot提供了Thymeleaf的自动化配置解决方案,因此在SpringBoot中使用Thymeleaf将会非常的方便。
步骤01:创建工程添加依赖

<!--引入thymeleaf的相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
<!--引入Web的相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

步骤02:配置Thymeleaf

#thymeleaf的配置
#是否开启缓存,开发时可设置为false 默认为true
spring.thymeleaf.cache=true
#检查模版是否存在,默认为true
spring.thymeleaf.check-template=true
#检查磨板位置是否存在,默认为true
spring.thymeleaf.check-template-location=true
#模版文件的编码
spring.thymeleaf.encoding=UTF-8
#模版文件的位置
spring.thymeleaf.prefix=classpath:/templates/
#Content-Type配置
spring.thymeleaf.servlet.content-type=text/html
#模版文件后缀
spring.thymeleaf.suffix=.html

步骤03:配置Tomact容器

#配置WEB容器的端口号
server.port=8080
#配置了当前项目出错的跳转页面
server.error.path=/error
#配置了Session的失效时间30分钟如果不写单位默认就是秒
server.servlet.session.timeout=30m
#表示项目名称如果不配置默认为/ 如果配置了就要在访问路径上加上配置的路径
server.servlet.context-path=/springboot
#配置了Tomact的请求编码
server.tomcat.uri-encoding=utf-8
#表示Tomact的最大线程数
server.tomcat.max-threads=500
#存放Tomact运行日志的文件
#server.tomcat.basedir=/home/sang/tmp

步骤04:配置实体类

package com.gsww.lf.springboot01.com.gsww.lf.springboot01.entity;

import org.springframework.stereotype.Repository;

/**
 * @Auther:yq
 * @Date:2019/6/25
 * @Description:com.gsww.lf.springboot01.com.gsww.lf.springboot01.entity
 * @Version:1.0
 */
@Repository
public class Book {
    private Integer id;
    private String name;
    private String author;

    public Book(Integer id, String name, String author) {
        this.id = id;
        this.name = name;
        this.author = author;
    }

    public Book() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

步骤05:配置控制器

package com.gsww.lf.springboot01.com.gsww.ls.springboot01.controller;

import com.gsww.lf.springboot01.com.gsww.lf.springboot01.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

/**
 * @Auther:yq
 * @Date:2019/6/25
 * @Description:com.gsww.lf.springboot01.com.gsww.ls.springboot01.controller
 * @Version:1.0
 */
@Controller
public class BookController {

    @GetMapping("/books")
    public ModelAndView books(){
        ArrayList<Book> books = new ArrayList<>();
        Book b1 = new Book();
        b1.setId(1);
        b1.setAuthor("罗贯中");
        b1.setName("三国演义");
        Book b2 = new Book();
        b2.setId(2);
        b2.setAuthor("曹雪芹");
        b2.setName("红楼梦");
        books.add(b1);
        books.add(b2);
        ModelAndView md = new ModelAndView();
        md.addObject("books",books);
        md.setViewName("books");
        return md;

    }

}


步骤06:创建视图

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>图书列表</title>
</head>
<body>
<table border="1">
    <tr>
        <td>图书编号</td>
        <td>图书名称</td>
        <td>图书作者</td>
    </tr>
    <tr th:each="book:${books}">
        <td th:text="${book.id}"></td>
        <td th:text="${book.name}"></td>
        <td th:text="${book.author}"></td>
    </tr>
</table>
</body>
</html>

步骤07:运行
在浏览器地址栏中输入http://localhost:8080/springboot/books即可看到运行效果如图:

image.png
上一篇下一篇

猜你喜欢

热点阅读