SpringBoot 学习笔记_整合视图层

SpringBoot 学习笔记_整合视图层

声明:

本次学习参考 《SpringBoot + Vue 开发实战》 · 王松(著) 一书。

本文的目的是为了记录我在学习的过程和遇到的一些问题以及解决办法。

如有侵权,请联系我删除。

SpringBoot 整合视图层技术

目前企业级开发中,前后端分离是趋势,但是视图层技术还占有一席之地。SpringBoot 对视图层技术提供了很好的支持,官方推荐使用模板引擎 Thymeleaf

整合 Thymeleaf

Thymeleaf 是新一代 Java 模板引擎,与传统 java 模板引擎不同的是,Thymeleaf 支持 HTML 原型,既可以让前端工程师在浏览器中直接打开查看样式,也可以让后端工程师结合真实数据查看显示效果。同时, SpringBoot 提供了 Thymeleaf 自动化配置解决方案。

  1. 在 pom.xml 中添加相关依赖

    <!--   Thymeleaf 相关依赖     -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>	
    
  2. 配置 Thymeleaf

    SpringBoot 为 Thymeleaf 提供了自动化配置类 ThymeleafAutoConfiguration, 相关配置属性在 ThymeleafProperties 类中

    查看该类的源码,部分如下:

    public class ThymeleafProperties {
    	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
    	public static final String DEFAULT_PREFIX = "classpath:/templates/";
    	public static final String DEFAULT_SUFFIX = ".html";
    	private boolean checkTemplate = true;
    	private boolean checkTemplateLocation = true;
    	private String prefix = DEFAULT_PREFIX;
    	private String suffix = DEFAULT_SUFFIX;
    	private String mode = "HTML";
    	private Charset encoding = DEFAULT_ENCODING;
    	private boolean cache = true;
    	private Integer templateResolverOrder;
    	private String[] viewNames;
    	private String[] excludedViewNames;
    	private boolean enableSpringElCompiler;
    	private boolean renderHiddenMarkersBeforeCheckboxes = false;
    	private boolean enabled = true;
    	...
    }
    

    可以看到,默认的模板位置在 classpath:/templates/ ,默认模板后缀 .html

    我们也可以在 application.properties 文件中对其进行自定义配置:

    # 是否开启缓存(通常:开发时 false,线上时 true)
    spring.thymeleaf.cache=true
    # 检查模板是否存在
    spring.thymeleaf.check-template=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
    
  3. 配置控制器(以 BookController 为例),在控制器中返回 ModelAndView

    @GetMapping("/books")
    public ModelAndView books(){
        List<Book> books = new ArrayList<>();
    
        books.add(new Book("三国演义", "罗贯中", 65.3F));
        books.add(new Book("西游记", "吴承恩", 55.1F));
        books.add(new Book("水浒传", "施耐庵", 36.7F));
        books.add(new Book("红楼梦", "曹雪芹", 52.3F));
    
        ModelAndView mv = new ModelAndView();
        mv.addObject("books", books);
        mv.setViewName("books");
        return mv;
    }
    
  4. 创建视图

    在 resource 目录中的 template 目录中创建 books.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>thymeleaf</title>
    </head>
    <body>
        <table border="1">
            <tr>
                <td>图书名称</td>
                <td>图书作者</td>
                <td>图书价格</td>
            </tr>
            <tr></tr>
            <tr th:each="book:${books}">
                <td th:text="${book.name}"></td>
                <td th:text="${book.author}"></td>
                <td th:text="${book.price}"></td>
            </tr>
        </table>
    </body>
    </html>
    
  5. 运行测试

    https://localhost:8080/books

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章