Spring Boot整合视图层技术 SpringBoot+Vue全栈开发第三章

Spring Boot整合视图层技术

  • 整合Thymeleaf

  • 整合FreeMarker

 

3.1 整合Thymeleaf

Thymeleaf是新一代Java模板引擎,类似于Velocity,FreeMarker等传统Java模板引擎.虽说前后端已经分离,但我们了解下还是可以的.

Thymeleaf支持HTML原型,既可以让前端工程师在浏览器中直接查看样式,也可以让后端工程师结合数据查看真实效果. 并且SpringBoot提供了Thymeleaf自动化配置解决方案,所以SpringBoot中使用Thymeleaf非常方便

步骤

1.创建工程添加依赖

需要spring-boot-starter-web和spring-boot-starter-thymeleaf依赖

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

2.配置Thymeleaf

SpringBoot 为Thymeleaf提供了自动化配置类ThymeleafAutoConfiguration, 相关的配置属性在ThymeleafProperties类中,其部分源码如下

public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    
    ....
    }

由上可知,模板的位置在classpath:/templates/,后缀名为.html

Thymeleaf的配置可以在application.properties中配置

#是否开启缓存,开发时可设置为false,默认为true
spring.thymeleaf.cache=false
#检查模板是否存在,默认为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

 

首先我们需要一个model类

public class Book {
    private String name;
    private String author;
    private String price;
    //省略getter,setter...
    }
    
    

然后编写Controller

@Controller
public class BookController {
​
​
​
    @GetMapping("/books")
    public ModelAndView books(){
        List<Book> list = new ArrayList<>();
        Book b1 = new Book();
        Book b2 = new Book();
        b1.setAuthor("罗贯中");
        b1.setName("三国演义");
        b1.setPrice("30");
        b2.setAuthor("曹雪芹");
        b2.setName("红楼梦");
        b2.setPrice("50");
        list.add(b1);
        list.add(b2);
        ModelAndView mv = new ModelAndView();
        mv.addObject("books", list);
        mv.setViewName("books");
        return mv;
    }
}

在resource中的template下创建html文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table>
        <tr>
            <td>书名</td>
            <td>作者</td>
            <td>价格</td>
        </tr>
        <tr th:each="book:${books}">
            <td th:text="${book.getName()}"></td>
            <td th:text="${book.author}"></td>
            <td th:text="${book.price}"></td>
        </tr>
    </table>
​
</body>
</html>

注意第二行,引入 xmlns:th="http://www.thymeleaf.org 名称空间

因为现在是前后端分离,不多介绍,了解更多的话,可以查看https://thymeleaf.org

 

3.2 整合FreeMarker

FreeMarker是一个非常古老的模板引擎,可以用在Web和非Web环境.

他和Thymeleaf不同的时,其需要经过解析才能显示出来.

FreeMarker不仅可以用来配置HTML页面模板,也可以做为电子右键模板,配置文件模板以及源码模板等.

SpringBoot 对其也有很好自持

 

1.创建项目引入依赖

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

 

2.配置FreeMarker

先查看部分源码

public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
    public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
    public static final String DEFAULT_PREFIX = "";
    public static final String DEFAULT_SUFFIX = ".ftl";
    }

可以看的出FreeMarker和Thymeleaf的模板位置是一样的,但是FreeMarker的后缀时.ftl

 

FreeMarker的常用配置

#HttpServletRequest的属性是否可以覆盖controller中model的同名项
spring.freemarker.allow-request-override=false
#HttpSession的属性是否可以覆盖controller中model的同名项
spring.freemarker.allow-session-override=false
#是否开启缓存
spring.freemarker.cache=false
#模板文件编码
spring.freemarker.charset=UTF-8
#是否检查模板位置
spring.freemarker.check-template-location=true
#Content-Type的值
spring.freemarker.content-type=text/html
#是否将HttpServletRequest中的属性添加到Model中
spring.freemarker.expose-request-attributes=false
#是否将HttpSession中的属性添加到Model中
spring.freemarker.expose-session-attributes=false
#模板文件后缀
spring.freemarker.suffix=.ftl
#模板文件位置
spring.freemarker.template-loader-path=classpath:/templates/

 

3.配置控制器

@Controller
public class BookController {
​
​
​
    @GetMapping("/books")
    public ModelAndView books(){
        List<Book> list = new ArrayList<>();
        Book b1 = new Book();
        Book b2 = new Book();
        b1.setAuthor("罗贯中");
        b1.setName("三国演义");
        b1.setPrice("30");
        b2.setAuthor("曹雪芹");
        b2.setName("红楼梦");
        b2.setPrice("50");
        list.add(b1);
        list.add(b2);
        ModelAndView mv = new ModelAndView();
        mv.addObject("books", list);
        mv.setViewName("books");
        return mv;
    }
}

 

4.创建视图

创建books.ftl文件

<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>书名</td>
        <td>作者</td>
        <td>价格</td>
    </tr>
    <#if books ??&&(books?size>0)>
    <#list books as book>
    <tr >
        <td>${book.name}</td>
        <td>${book.author}</td>
        <td>${book.price}</td>
    </tr>
    </#list>
</#if>
</table>
​
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值