SpringBoot2.x系列:整合SpringMVC之模板引擎

其实现在的很多web项目开发,都采用了前后端完全分离的模式,也就是说后端只提供数据接口,前端通过AJAX请求获取数据,所以完全不需要用的模板引擎。

但是前后端分离的这种模式也有缺点,比如不利于SEO,并且在性能上也会稍微差一点。另外还有一些场景,使用模板引擎会更方便,比如说使用邮件模板。

所以本章节给大家简单介绍在Spring boot中使用Thymeleaf、Freemaker等模板引擎以及对JSP的集成。

一. SpringMVC的模板技术

Spring MVC支持各种各样的模板技术,包括Velocity, FreeMarker和JSP等,很多其他的模板引擎也提供了它们自己与Spring MVC集成的API。

1. Spring Boot支持的模板引擎

Spring Boot支持以下的模板引擎,为他们提供了自动配置。

  • FreeMarker
  • Groovy
  • Thymeleaf
  • Velocity(1.4已不再支持)
  • Mustache

默认情况下,对以上的任意模板引擎,Spring Boot都会默认从src/main/resources/templates目录下自动加载模板引擎文件。

2. 使用注意事项

1.由于在内嵌的servlet容器中使用JSP会存在一些限制,所以建议尽量不要在Spring Boot中使用JSP。

2.IntelliJ IDEA会根据运行应用的方式不同,会对classpath进行不同的排序。在IDEA里通过main方法运行应用,跟从Maven,或Gradle,或打包好的jar中运行相比,会导致不同的顺序,这可能导致Spring Boot不能从classpath下成功地找到模板。

如果遇到这个问题,你可以在IDEA里重新对classpath进行排序,将模块的类和资源放到第一位。比如可以配置模块的前缀为classpath*:/templates/,这样会查找classpath下的所有模板目录。

模板引擎有好几个,本章节就就讲解最常用的Thymeleaf,FreeMarker及与jsp的结合实现。

二. SpringBoot中整合Thymeleaf

我在前面的章节中,已经讲过SpringBoot与Thymeleaf的整合,所以本节略过。

请参考我前面的章节:
15_SpringBoot2.x系列教程15--Web开发01之Thymeleaf使用

知乎地址:

https://zhuanlan.zhihu.com/p/113864980

CSDN地址:

https://blog.csdn.net/syc000666/article/details/105087519

三. SpringBoot中整合JSP

我在前面的章节中,已经讲过SpringBoot与JSP的整合,所以本节略过。

请参考我前面的章节:
17_SpringBoot2.x系列教程17--Web开发03之支持jsp

知乎地址:

https://zhuanlan.zhihu.com/p/114074328

CSDN地址:

https://blog.csdn.net/syc000666/article/details/105087545

四. SpringBoot中整合FreeMarker

1. FreeMarker简介

FreeMarker是一个不错的模板引擎,在众多的网页静态化模板中口碑很好,今天就用Spring Boot来整合这个模板。

2. 创建web项目

我们创建一个Spring Boot项目,该项目结构如下。

3. 添加FreeMarker依赖

在pom.xml文件中添加FreeMarker依赖包。

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

4. 添加FreeMarker配置信息

在application.properties文件中,添加FreeMarker的一些配置信息。
spring.freemarker.template-loader-path指的是freemarker文件的路径信息;
spring.freemarker.cache这个表示的是缓存是否打开;
spring.freemarker.suffix=.ftl指明了freemarker文件的后缀名为.ftl;
其他几个都是常规配置,基本不需要修改的。

#.ftl文件存放位置
spring.freemarker.template-loader-path=classpath:/templates/
#spring.freemarker.prefix=
spring.freemarker.suffix=.ftl
spring.freemarker.cache=false
spring.freemarker.charset=utf-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.allow-request-override=true
spring.freemarker.allow-session-override=true
spring.freemarker.request-context-attribute=request

5. 创建资源文件

在resources目录下创建一个资源文件conf.properties,作为测试的数据源。

users.name=yiyige
users.desc=learn freemarker

6. 创建.ftl模板文件

在templates目录下,再创建一个ftl目录,里面创建一个模板文件index.ftl。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User</title>
</head>
<body>
<h1>${user.name}</h1>
<h1>${user.desc}</h1>
</body>
</html>

7. 创建读取资源文件的实体类

创建一个实体类,用于从conf.properties文件中读取配置信息。该类中使用了@Component,@ConfigurationProperties,@PropertySource三个注解,实体类属性对应资源文件,并添加Setter和Getter方法。

package com.yyg.boot;

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/25
 */
@Data
@ToString
@Component
@ConfigurationProperties(prefix = "users")
@PropertySource(value = "classpath:/conf.properties")
public class User {

    private String name;

    private String desc;

}

8. 创建Controller测试接口

创建controller类,添加调试接口方法,把资源数据通过Model传送到index.ftl上,这里的返回字符串上不用加后缀,因为默认.ftl。

package com.yyg.boot.web;

import com.yyg.boot.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/25
 */
@Controller
public class HelloController {

    @Autowired
    private User user;

    @GetMapping("/show")
    public String show(Model model) {
        model.addAttribute("user", user);
        //ftl是.ftl模板文件所在文件夹,index是模板名称.
        return "ftl/index";
    }

}

9. 创建启动类

package com.yyg.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Description Description
 * @Author 一一哥Sun
 * @Date Created in 2020/3/25
 */
@SpringBootApplication
public class TemplateApplication {

    public static void main(String[] args) {
        SpringApplication.run(TemplateApplication.class, args);
    }

}

10. 启动程序测试

在浏览器中输入地址
http://localhost:8080/show
可以看到如下界面效果,说明我们已经成功的在Spring Boot中集成了FreeMarker模板,渲染了出了模板页面。

原创文章 412 获赞 264 访问量 93万+

猜你喜欢

转载自blog.csdn.net/GUDUzhongliang/article/details/105766297