springboot模板
-
springboot之thymeleaf模板
-
对应的后台代码
* 前台HTML页面1
21 * springboot之freemarker模板
2 -
pom依赖
* 实体类
* 相关controler层的Java代码1
21 * 前台
2
springboot之thymeleaf模板
关于Thymeleaf的优点,只说一条:它就是html页面。上代码:
相关pom依赖
也可以建springboot项目时选择就行了
1
2
3
4
5
6 1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-thymeleaf</artifactId>
4 </dependency>
5
6
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
1
2
3 1spring.thymeleaf.cache=false
2
3
正式环境还是要将缓存开启的
对应的后台代码
实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1package com.li.springboot01.entity;
2
3import lombok.Data;
4
5/**
6 * @author dragon
7 * @site
8 * @company
9 * @create 2019-11-08 18:11
10 */
11@Data
12public class User {
13 private String uid;
14 private String uname;
15
16 public User() {
17 }
18
19 public User(String uid, String uname) {
20 this.uid = uid;
21 this.uname = uname;
22 }
23}
24
25
26
ThymeleafController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 1package com.li.springboot01.controller;
2
3import com.li.springboot01.entity.User;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.servlet.ModelAndView;
7
8import java.util.ArrayList;
9import java.util.List;
10
11/**
12 * @author dragon
13 * @site
14 * @company
15 * @create 2019-11-08 11:55
16 */
17@Controller
18@RequestMapping("/thymeleaf")
19public class ThymeleafController {
20
21 @RequestMapping("/list")
22 public ModelAndView list(){
23 ModelAndView mv = new ModelAndView();
24 List list = new ArrayList();
25 list.add(new User("1","xs"));
26 list.add(new User("2","ls"));
27 list.add(new User("3","zs"));
28
29 mv.addObject("userList",list);
30 mv.addObject("msg","<span style='color:red'>双十一剁手活动即将开始</span>");
31 mv.addObject("name","zs");
32 mv.setViewName("list");
33 return mv;
34 }
35}
36
37
前台HTML页面
list.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 1<!DOCTYPE html>
2<html xmlns:th="http://www.thymeleaf.org">
3<head>
4 <meta charset="UTF-8">
5 <title>Thymeleaf页面</title>
6</head>
7<body>
8<h2>显示文本</h2>
9<span th:text="${name}"></span>
10
11<h2>显示HTML</h2>
12<div th:utext="${msg}"></div>
13
14<h2>循环</h2>
15<table>
16 <tr>
17 <td>用户的id</td>
18 <td>用户名</td>
19 </tr>
20 <tr th:each="u : ${userList}">
21 <td th:text="${u.uid}"></td>
22 <td th:text="${u.uname}"></td>
23 </tr>
24</table>
25
26
27<h2>包含页面</h2>
28<!--全部页面-->
29<!--<div th:include="role/common/head2.html"></div>-->
30<!--部分页面内容-->
31<div th:include="role/common/head2 :: h1"></div>
32
33</body>
34</html>
35
36
head2 .html
1
2
3
4
5
6
7
8
9
10
11
12 1<div th:fragment="h1">
2 第一部分类容
3</div>
4<div th:fragment="h2">
5 第二部分类容
6</div>
7<div th:fragment="h3">
8 第三部分类容
9</div>
10
11
12
springboot之freemarker模板
学习网址:
pom依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId>
8 <version>2.2.1.RELEASE</version>
9 <relativePath/> <!-- lookup parent from repository -->
10 </parent>
11 <groupId>com.li</groupId>
12 <artifactId>springboot01</artifactId>
13 <version>0.0.1-SNAPSHOT</version>
14 <name>springboot01</name>
15 <description>Demo project for Spring Boot</description>
16
17 <properties>
18 <java.version>1.8</java.version>
19 </properties>
20
21 <dependencies>
22 <!--freemarker-->
23 <dependency>
24 <groupId>org.springframework.boot</groupId>
25 <artifactId>spring-boot-starter-freemarker</artifactId>
26 </dependency>
27 <!--thymeleaf-->
28 <dependency>
29 <groupId>org.springframework.boot</groupId>
30 <artifactId>spring-boot-starter-thymeleaf</artifactId>
31 </dependency>
32 <dependency>
33 <groupId>org.springframework.boot</groupId>
34 <artifactId>spring-boot-starter-web</artifactId>
35 </dependency>
36
37 <dependency>
38 <groupId>org.projectlombok</groupId>
39 <artifactId>lombok</artifactId>
40 <optional>true</optional>
41 </dependency>
42 <dependency>
43 <groupId>org.springframework.boot</groupId>
44 <artifactId>spring-boot-starter-test</artifactId>
45 <scope>test</scope>
46 <exclusions>
47 <exclusion>
48 <groupId>org.junit.vintage</groupId>
49 <artifactId>junit-vintage-engine</artifactId>
50 </exclusion>
51 </exclusions>
52 </dependency>
53 </dependencies>
54
55 <build>
56 <plugins>
57 <plugin>
58 <groupId>org.springframework.boot</groupId>
59 <artifactId>spring-boot-maven-plugin</artifactId>
60 </plugin>
61 </plugins>
62
63
64 <!--可以不加,但是做项目的时候可能会用-->
65 <resources>
66 <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
67 <resource>
68 <directory>src/main/java</directory>
69 <includes>
70 <include>**/*.xml</include>
71 </includes>
72 </resource>
73 <!--freemarker模板也读取需要注释标红地方-->
74 <resource>
75 <directory>src/main/resources</directory>
76 <includes>
77 <!--<include>*.properties</include>-->
78 <!--<include>*.xml</include>-->
79 <!--<include>*.yml</include>-->
80 </includes>
81 </resource>
82 </resources>
83 </build>
84
85</project>
86
87
88
实体类
Role
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1package com.li.springboot01.entity;
2
3import lombok.Data;
4
5/**
6 * @author dragon
7 * @site
8 * @company
9 * @create 2019-11-08 18:11
10 */
11@Data
12public class Role {
13 private String rid;
14 private String rname;
15
16 public Role() {
17 }
18
19 public Role(String rid, String rname) {
20 this.rid = rid;
21 this.rname = rname;
22 }
23}
24
25
26
相关controler层的Java代码
FreemarkerController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 1package com.li.springboot01.controller;
2
3import com.li.springboot01.entity.Role;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.servlet.ModelAndView;
7
8import java.util.ArrayList;
9import java.util.List;
10
11/**
12 * @author dragon
13 * @site
14 * @company
15 * @create 2019-11-08 18:59
16 */
17@Controller
18@RequestMapping("/freemarker")
19public class FreemarkerController {
20 @RequestMapping("/list")
21 public ModelAndView list(){
22 ModelAndView mv = new ModelAndView();
23
24 mv.addObject("loginName","小鸡啄米");
25 List list = new ArrayList();
26 list.add(new Role("1","普通用户"));
27 list.add(new Role("2","会员"));
28 list.add(new Role("3","超级会员"));
29 mv.addObject("roleList",list);
30
31 mv.setViewName("list");
32 return mv;
33 }
34
35
36}
37
38
39
前台
application.yml文件的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 1server:
2 port: 88
3 servlet:
4 context-path: /springboot
5
6
7#缓存关闭
8spring:
9 thymeleaf:
10 cache: false
11 freemarker:
12 # 设置模板后缀名
13 suffix: .ftl
14 # 设置文档类型
15 content-type: text/html
16 # 设置页面编码格式
17 charset: UTF-8
18 # 设置页面缓存
19 cache: false
20 # 设置ftl文件路径,默认是/templates,为演示效果添加role
21 template-loader-path: classpath:/templates/role
22 mvc:
23 static-path-pattern: /static/**
24
25
list.ftl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <title>freemarker知识点</title>
6</head>
7<body>
8freemarker知识点
9<h2>获取值</h2>
10${loginname !'未知'}
11
12${loginName !'未知'}
13
14<h2>遍历</h2>
15
16<table border="1" width="60%">
17 <tr>
18 <td>角色id</td>
19 <td>角色名</td>
20 </tr>
21 <#list roleList as role>
22 <tr>
23 <td>${role.rid}</td>
24 <td>${role.rname}</td>
25 </tr>
26 </#list>
27</table>
28
29<h2>包含页面</h2>
30<#include 'common/head.ftl'/>
31<#include 'common/global.ftl'/>
32
33<h2>获取项目名 && 定义变量</h2>
34${springMacroRequestContext.contextPath}
35
36<#--全局变量-->
37${xmm}
38
39<#--<#assign xmm>-->
40<#-- ${springMacroRequestContext.contextPath}-->
41<#--</#assign>-->
42<#--${xmm}-->
43<h2>如何在页面定义变量</h2>
44
45
46
47
48
49</body>
50</html>
51
52
head.ftl
1
2
3
4
5
6
7
8
9
10
11
12
13 1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <title>Title</title>
6</head>
7<body>
8头部页面
9<span style="color: rebeccapurple">傻逼!?》D{!!</span>
10</body>
11</html>
12
13
global.ftl
全局属性定义
1
2
3
4
5 1<#global xmm>
2 ${springMacroRequestContext.contextPath}
3</#global>
4
5
over~