Freemarker官网
Template + data-model = output - Apache FreeMarker Manualhttps://freemarker.apache.org/docs/dgui_quickstart_basics.html
整体结构
1、在pom文件中导入依赖
<dependencies>
<!--spring-boot-start启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--freemarker模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--lombok作用实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
父工程版本依赖
因为此处引用了父工程的版本依赖,附上
<properties>
<java.version>1.8</java.version>
<tomcat.version>8.5.28</tomcat.version>
<!--<spring-boot.version>2.0.6.RELEASE</spring-boot.version>-->
<springframework.version>5.0.5.RELEASE</springframework.version>
<mybatis-spring-boot.version>1.3.1</mybatis-spring-boot.version>
<mybatis.version>3.4.5</mybatis.version>
<druid.version>1.1.6</druid.version>
<commons-io.version>2.6</commons-io.version>
<org.apache.commons.io.version>1.3.2</org.apache.commons.io.version>
<commons-fileupload.version>1.3.3</commons-fileupload.version>
<commons-codec.version>1.10</commons-codec.version>
<commons-lang3.version>3.6</commons-lang3.version>
<okhttp.version>3.9.1</okhttp.version>
<feign-okhttp.version>8.18.0</feign-okhttp.version>
<lombok.version>1.18.20</lombok.version>
<springfox-swagger.version>2.7.0</springfox-swagger.version>
<fastjson.version>1.2.30</fastjson.version>
<fastdfs-client-java.version>1.27.0.0</fastdfs-client-java.version>
<mysql-connector-java.version>8.0.11</mysql-connector-java.version>
<elasticsearch.version>6.2.1</elasticsearch.version>
<guava.version>24.0-jre</guava.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<!--<spring-cloud.version>Finchley.SR1</spring-cloud.version>-->
</properties>
<!--test-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>${feign-okhttp.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${org.apache.commons.io.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>${fastdfs-client-java.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
2、resources下的页面模板
在resources下新建templates文件夹
并添加页面user.ftl
注意:文件夹名称templates不可改变,页面后缀.ftl不可改变。(在补充解释)
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome ${user}!</h1>
<p>Our latest product:
<a href="${latestProduct.url}">${latestProduct.name}</a>!
</body>
</html>
3、application.yml配置
server:
port: 8888
spring:
application:
name: test-freemarker
freemarker:
charset: UTF-8
#suffix:
cache: false
settings:
template_update_delay: 0 #检查模板更新延迟时间,设置为 0 表示立即检查,如果时间大于 0 会有缓存不方便进行模板测试
4、controller层——TestFreemarker
import com.lxw.pojo.Product;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Map;
@Controller
public class TestFreemarker {
@GetMapping
public String test(Map map){
map.put("user","李白");
Product product = new Product();
product.setUrl("http://www.baidu.com");
product.setName("百度");
map.put("latestProduct",product);
return "user";
}
}
5、pojo层——Product 类
package com.lxw.pojo;
import lombok.Data;
@Data
public class Product {
private String url;
private String name;
}
6、启动类——TestFreemarkerApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestFreemarkerApplication {
public static void main(String[] args) {
SpringApplication.run(TestFreemarkerApplication.class,args);
}
}
启动测试!
最后在启动类中启动测试,访问网址:http://localhost:8888/
补充:
resources下的文件夹和页面名后缀为什么不能随意更改呢?
上依赖文件源码!
springboot采用约定大于配置的原则,默认属性都在这里设置,现在明白了吧!
那么如何自定义呢?
问的好!
答:在application.yml配置文件中给对应属性赋值就会覆盖默认属性,以达到自定义的效果。
例如:修改页面文件的后缀名.ftl为.html,只需要给该属性赋值就好了
先在就可以 以.html结尾了。