Thymeleaf用法(参考链接)
(1)标准表达式语法
大多数Thymeleaf属性允许将它们的值设置为或包含表达式,由于它们使用的方言,我们将其称为标准表达式。这些表达式可以有五种类型:
${...} : 变量表达式。
*{...} : 选择表达式。
#{...} : 消息 (i18n) 表达式。
@{...} : 链接 (URL) 表达式。
~{...} : 片段表达式。
(2) 标准语法中的变量表达式
变量表达式是OGNL表达式 - 如果将Thymeleaf 与Spring - 集成在上下文变量上(也称为Spring术语中的模型属性),则为Spring EL。 它们看起来像这样:
${session.user.name}
它们作为属性值或作为它们的一部分,取决于属性:
<span th:text="${book.author.name}">
上面的表达式与下面是相同的(在OGNL和SpringEL中):
((Book)context.getVariable("book")).getAuthor().getName()
但是不仅在涉及输出的场景中找到变量表达式,而且还可以使用更复杂的处理方式,如:条件,迭代…等等。
<li th:each="book : ${books}">
这里${books}从上下文中选择名为books的变量,并在th:each中使用循环将其评估为迭代器。
(3)前台展示
- 展示
String
数据
后台:
前台:model.addAttribute("success","后台String类型的数据传过来了");
<span th:text="${success}"></span>
- 展示
List<T>
数据
后台:
前台:List<CSDN> csdnListResult model.addAttribute("csdnListResult",csdnListResult);
article_id、article_title为T类的成员变量<tr th:each="csdn:${csdnListResult}"> <td th:text="${csdn.article_id}"></td> <td th:text="${csdn.article_title}"></td> </tr>
- 展示
List<Map<String, Object>>
数据
后台:
前台:List<Map<String, Object>> result; model.addAttribute("aiYueBlog", result);
article_id、article_title为T类的成员变量<tr th:each="blog:${aiYueBlog }"> <td th:text="${blog.article_id}"></td> <td th:text="${blog.article_title}"></td> </tr>
(4)springboot热部署
application.yml:
debug: true
spring:
devtools:
restart:
enabled: true #设置开启热部署
freemarker:
cache: false #页面不加载缓存,修改即时生效
application.properties:
# 禁用缓存
spring.thymeleaf.cache=false
pom.xml:
<!-- 热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
(5)mysql插入数据中文乱码
1.application.yml:
datasource:
username: root
password: 1234556
url: jdbc:mysql://localhost:3306/SQLNAME?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# schema:
# - classpath:department.sql
2.修改mysql表配置
ALTER TABLE 表名 CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;