SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课

1、SpringBoot Starter讲解

简介:介绍什么是SpringBoot Starter和主要作用

1、官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-starter

2、starter主要简化依赖用的

spring-boot-starter-web->里面包含多种依赖

3、几个常用的starter

spring-boot-starter-activemq

spring-boot-starter-aop

spring-boot-starter-data-redis

spring-boot-starter-freemarker

spring-boot-starter-thymeleaf

spring-boot-starter-webflux

2、SpringBoot2.x常见模板引擎讲解和官方推荐使用

简介:介绍常用的SpringBoot2.x模板引擎和官方推荐案例

1、JSP(后端渲染,消耗性能)

Java Server Pages 动态网页技术,由应用服务器中的JSP引擎来编译和执行,再将生成的整个页面返回给客户端

可以写java代码

持表达式语言(el、jstl)

内建函数

JSP->Servlet(占用JVM内存)permSize

javaweb官方推荐

springboot不推荐 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations

2、Freemarker 

FreeMarker Template Language(FTL)  文件一般保存为 xxx.ftl

严格依赖MVC模式,不依赖Servlet容器(不占用JVM内存)

内建函数

3、Thymeleaf (主推)

轻量级的模板引擎(处理复杂逻辑业务的不推荐,解析DOM或者XML会占用多的内存)

可以直接在浏览器中打开且正确显示模板页面

直接是html结尾,直接编辑

xdlcass.net/user/userinfo.html

社会工程学

伪装

3、SpringBoot2.x整合模板引擎freemarker实战

简介:SpringBoot2.x整合模板引擎freemarker实战

1、Freemarker相关maven依赖

<!-- 引入freemarker模板引擎的依赖 -->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-freemarker</artifactId>

        </dependency>

2、Freemarker基础配置

# 是否开启thymeleaf缓存,本地为false,生产建议为true

spring.freemarker.cache=false

spring.freemarker.charset=UTF-8

spring.freemarker.allow-request-override=false

spring.freemarker.check-template-location=true

#类型

spring.freemarker.content-type=text/html

spring.freemarker.expose-request-attributes=true

spring.freemarker.expose-session-attributes=true

#文件后缀

spring.freemarker.suffix=.ftl

#路径

spring.freemarker.template-loader-path=classpath:/templates/

3、建立文件夹(通常在src/main/resources/templates/下)

1)src/main/resources/templates/fm/user/

2)建立一个index.ftl

3)user文件夹下面建立一个user.html

   4、简单测试代码编写和访问

代码示例:

application.properties:

 1 web.images-path=L:/images
 2 spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path}
 3 #指定某些文件不进行监听,即不会进行热加载devtool(重启后不会监听下面这个文件)
 4 #spring.devtools.restart.exclude=application.properties
 5 
 6 #通过触发器,去控制什么时候进行热加载部署新的文件
 7 spring.devtools.restart.trigger-file=trigger.txt
 8 
 9 server.port=8083
10 
11 #文件上传路径配置
12 web.file.path=L:/images
13 
14 #测试配置文件注入
15 test.name=www.xdclass.net
16 test.domain=springboot
17 
18 #自定义启动banner的路径
19 spring.banner.location=banner.txt
20 
21 # 是否开启freemarker缓存,本地为false,生产建议为true
22 spring.freemarker.cache=false
23 
24 spring.freemarker.charset=UTF-8
25 spring.freemarker.allow-request-override=false
26 spring.freemarker.check-template-location=true
27 
28 #类型
29 spring.freemarker.content-type=text/html
30 
31 spring.freemarker.expose-request-attributes=true
32 spring.freemarker.expose-session-attributes=true
33         
34 #文件后缀
35 spring.freemarker.suffix=.ftl
36 #路径
37 spring.freemarker.template-loader-path=classpath:/templates/

FreemakerController.java:

 1 package net.xdclass.demo.controller;
 2 
 3 import net.xdclass.demo.domain.ServerSettings;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.ModelMap;
 8 import org.springframework.web.bind.annotation.GetMapping;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 
11 
12 @Controller
13 @RequestMapping("/freemaker")
14 public class FreemakerController {
15 
16     
17     @Autowired
18     private ServerSettings setting;
19 
20     
21     @GetMapping("hello")
22     public String index(ModelMap modelMap){
23         
24         modelMap.addAttribute("setting", setting);
25         
26         return "fm/index";  //不用加后缀,在配置文件里面已经指定了后缀
27     }
28     
29 
30 }

src/main/resources/templates/fm/index.ftl:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 freemaker整合,index.html页面
 9 <h1>xdclass.net</h1>
10 
11 <h1> ${setting.name} </h1>
12 <h1> ${setting.domain} </h1>
13 
14 </body>
15 </html>

浏览器访问:http://localhost:8083/freemaker/hello

结果:

freemaker整合,index.html页面

xdclass.net

www.xdclass.net

springboot 

4、SpringBoot2.x整合模板引擎thymeleaf实战

讲解:SpringBoot2.x整合模板引擎thymeleaf实战

官网地址:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html

1、thymeleaf相关maven依赖

<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

2、thymeleaf基础配置

#开发时关闭缓存,不然没法看到实时页面

spring.thymeleaf.cache=false

spring.thymeleaf.mode=HTML5

#前缀

spring.thymeleaf.prefix=classpath:/templates/

#编码

spring.thymeleaf.encoding=UTF-8

#类型

spring.thymeleaf.content-type=text/html

#名称的后缀

spring.thymeleaf.suffix=.html

3、建立文件夹

1)src/main/resources/templates/tl/

2)建立一个index.html

4、简单测试代码编写和访问

注意:$表达式只能写在th标签内部

快速入门:https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html

代码示例:

application.properties:

 1 web.images-path=L:/images
 2 spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path},classpath:/templates/
 3 #指定某些文件不进行监听,即不会进行热加载devtool(重启后不会监听下面这个文件)
 4 #spring.devtools.restart.exclude=application.properties
 5 
 6 #通过触发器,去控制什么时候进行热加载部署新的文件
 7 spring.devtools.restart.trigger-file=trigger.txt
 8 
 9 server.port=8083
10 
11 #文件上传路径配置
12 web.file.path=L:/images
13 
14 #测试配置文件注入
15 test.name=www.xdclass.net
16 test.domain=springboot
17 
18 #自定义启动banner的路径
19 spring.banner.location=banner.txt
20 
21 # 是否开启freemarker缓存,本地为false,生产建议为true
22 #spring.freemarker.cache=false
23 #
24 #spring.freemarker.charset=UTF-8
25 #spring.freemarker.allow-request-override=false
26 #spring.freemarker.check-template-location=true
27 #
28 ##类型
29 #spring.freemarker.content-type=text/html
30 #
31 #spring.freemarker.expose-request-attributes=true
32 #spring.freemarker.expose-session-attributes=true
33 #        
34 ##文件后缀
35 #spring.freemarker.suffix=.ftl
36 ##路径
37 #spring.freemarker.template-loader-path=classpath:/templates/
38 
39 
40 #开发时关闭缓存,不然没法看到实时页面
41 spring.thymeleaf.cache=false
42 spring.thymeleaf.mode=HTML5
43 #前缀
44 spring.thymeleaf.prefix=classpath:/templates/
45 #编码
46 spring.thymeleaf.encoding=UTF-8
47 #类型
48 spring.thymeleaf.servlet.content-type=text/html
49 #spring.thymeleaf.content-type=text/html
50 #名称的后缀
51 spring.thymeleaf.suffix=.html

ThymeleafController.java:

 1 package net.xdclass.demo.controller;
 2 
 3 import net.xdclass.demo.domain.ServerSettings;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.ModelMap;
 8 import org.springframework.web.bind.annotation.GetMapping;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 
11 
12 @Controller
13 @RequestMapping("/tyhmeleaf")
14 public class ThymeleafController {
15 
16     
17     @Autowired
18     private ServerSettings setting;
19 
20     
21     @GetMapping("hello")
22     public String index(){
23         
24         
25         return "index";  //不用加后缀,在配置文件里面已经指定了后缀
26     }
27     
28     
29 
30     @GetMapping("info")
31     public String admin(ModelMap modelMap){
32         
33         modelMap.addAttribute("setting", setting);
34         
35         return "tl/admin/info";  //不用加后缀,在配置文件里面已经指定了后缀
36     }
37 }

src/main/resources/templates/tl/admin/info.html:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 模板引擎整合thymeleaf  admin/info.html
 9 
10 <h1 >测试内容,未加th表达式</h1>
11 <h1 th:text="${setting.name}">测试内容</h1>
12 <h1>xdclass.net</h1>
13 </body>
14 </html>

测试结果对比:

浏览器访问:http://localhost:8083/tyhmeleaf/info

结果:

测试内容,未加th表达式

www.xdclass.net

xdclass.net

浏览器访问:http://localhost:8083/tl/admin/info.html

结果:

模板引擎整合thymeleaf admin/info.html

测试内容,未加th表达式

测试内容

xdclass.net

========7、SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课=========================
加入小D课堂技术交流答疑群:Q群:699347262

1、SpringBoot Starter讲解简介:介绍什么是SpringBoot Starter和主要作用
1、官网地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-starter2、starter主要简化依赖用的spring-boot-starter-web->里面包含多种依赖
3、几个常用的starterspring-boot-starter-activemqspring-boot-starter-aopspring-boot-starter-data-redisspring-boot-starter-freemarkerspring-boot-starter-thymeleafspring-boot-starter-webflux








2、SpringBoot2.x常见模板引擎讲解和官方推荐使用简介:介绍常用的SpringBoot2.x模板引擎和官方推荐案例
1、JSP(后端渲染,消耗性能)Java Server Pages 动态网页技术,由应用服务器中的JSP引擎来编译和执行,再将生成的整个页面返回给客户端可以写java代码持表达式语言(el、jstl)内建函数JSP->Servlet(占用JVM内存)permSizejavaweb官方推荐springboot不推荐 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations
2、Freemarker FreeMarker Template Language(FTL)  文件一般保存为 xxx.ftl严格依赖MVC模式,不依赖Servlet容器(不占用JVM内存)内建函数
3、Thymeleaf (主推)轻量级的模板引擎(负责逻辑业务的不推荐,解析DOM或者XML会占用多的内存)可以直接在浏览器中打开且正确显示模板页面
直接是html结尾,直接编辑xdlcass.net/user/userinfo.html社会工程学伪装






3、SpringBoot2.x整合模板引擎freemarker实战简介:SpringBoot2.x整合模板引擎freemarker实战
1、Freemarker相关maven依赖<!-- 引入freemarker模板引擎的依赖 -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-freemarker</artifactId>        </dependency>
2、Freemarker基础配置# 是否开启thymeleaf缓存,本地为false,生产建议为truespring.freemarker.cache=false
spring.freemarker.charset=UTF-8spring.freemarker.allow-request-override=falsespring.freemarker.check-template-location=true#类型spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=truespring.freemarker.expose-session-attributes=true#文件后缀spring.freemarker.suffix=.ftl#路径spring.freemarker.template-loader-path=classpath:/templates/
3、建立文件夹1)src/main/resources/templates/fm/user/2)建立一个index.ftl3)user文件夹下面建立一个user.html


4、简单测试代码编写和访问
4、SpringBoot2.x整合模板引擎thymeleaf实战讲解:SpringBoot2.x整合模板引擎thymeleaf实战
官网地址:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html1、thymeleaf相关maven依赖<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
2、thymeleaf基础配置
#开发时关闭缓存,不然没法看到实时页面spring.thymeleaf.cache=falsespring.thymeleaf.mode=HTML5#前缀spring.thymeleaf.prefix=classpath:/templates/#编码spring.thymeleaf.encoding=UTF-8#类型spring.thymeleaf.content-type=text/html#名称的后缀spring.thymeleaf.suffix=.html
3、建立文件夹1)src/main/resources/templates/tl/2)建立一个index.html
4、简单测试代码编写和访问注意:$表达式只能写在th标签内部快速入门:https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html

猜你喜欢

转载自www.cnblogs.com/116970u/p/10256067.html
今日推荐