面向文档学习编程,在遇到问题官方解决方案一般是最好的
模板引擎负责组装数据,以另一种形式或外观展现数据
浏览器中的页面是web模板引擎最终的展现。
PHP本身就是天生的模板引擎,
其他服务端模板引擎有JSP,mustache,FreeMark等。
介绍就到这里,今天总结的是SpringBoot官方推荐Thymeleaf
更简单,更强大。
一:静态资源的映射规则
1.写页面就需要css,js等,SpringBoot对静态资源文件的存放有明确规则。
1)webjars:引入jQuery等静态文件。
2)指定四个位置
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径
2.欢迎页:所有路径下的index.html
3.图标配置:favicon.ico
4.自定义静态静态资源加载路径
二:加载Thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
由于默认版本过低,使用properties更改版本号
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>
2.2.2
</thymeleaf-layout-dialect.version>
三:使用
1.只要把HTML放在templates,Thymeleaf自动渲染
2.在HTML导入名称空间(语法提示)
<html lang="en" xmlns:th="http://www.thymeleaf.org">
四:语法规则
1.表达式
Variable Expressions: ${...}:获取变量值;OGNL;
1)、获取对象的属性、调用方法
2)、使用内置的基本对象:
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object.
${session.foo}
3)、内置的一些工具对象:
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;
补充:配合 th:object="${session.user}:
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
Message Expressions: #{...}:获取国际化内容
Link URL Expressions: @{...}:定义URL;
@{/order/process(execId=${execId},execType='FAST')}
Fragment Expressions: ~{...}:片段引用表达式
<div th:insert="~{commons :: main}">...</div>
Literals(字面量)
Text literals: 'one text' , 'Another one!' ,…
Number literals: 0 , 34 , 3.0 , 12.3 ,…
Boolean literals: true , false
Null literal: null
Literal tokens: one , sometext , main ,…
Text operations:(文本操作)
String concatenation: +
Literal substitutions: |The name is ${name}|
Arithmetic operations:(数学运算)
Binary operators: + - * / %
Minus sign (unary operator): -
Boolean operations:(布尔运算)
Binary operators: and , or
Boolean negation (unary operator): ! , not
Comparisons and equality:(比较运算)
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
Conditional operators:条件运算(三元运算符)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Special tokens:
No-Operation: _
D:\maven\org\webjars\bootstrap\3.3.6\bootstrap-3.3.6.jar!\META-INF\resources\webjars\bootstrap\3.3.6\css\bootstrap.css
2.登录错误信息的提示
<p style="color: red;" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
3.抽取公共部分引入
thymeleaf有三种引入页面方式
抽取 : th:fragment
1) 引入 : th:insert
将整个公共片段插入引入中
2) th:replace
将声明引入的元素替换
3) th:include
将被引入的片段内容包含进标签
4.日期格式化
#dates.format(emp.birth,'yyyy-MM-dd HH:mm')
提交日期格式化(默认 / )
spring.mvc.date-format=yyyy-MM-dd
五:利器
<input type="hidden" name="_method" value="put" th:if="${emp!=null}"/>
将新增和修改设置为统一界面
但是在rest风格下,
新增是post,修改是put
所以引入以上代码实现判断当前操作