Spring Boot带前后端 渐进式开发企业级博客系统(一)gradle、Thymeleaf

一、Gradle

1、Gradle介绍

Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置。

面向Java应用为主。当前其支持的语言限于Java、Groovy、Kotlin和Scala,计划未来将支持更多的语言。

2、Gradle下载配置

(1)下载地址:http://services.gradle.org/distributions/

这边我们下载4.8版本


(2)配置环境变量,设置path

(3)打开命令行界面,运行gradle -v,出现如下界面即为安装成功


3、Eclipse安装Gradle插件

http://download.eclipse.org/buildship/updates/e46/releases/2.x/


二、Thymeleaf

1、介绍

Java模板引擎。能够处理HTML、XML、JavaScript、CSS甚至纯文本。类似JSP、Freemarker

自然模板。原型即页面

语法简单易懂。OGNL、SpringEL

遵从Web标准。支持HTML5

2、Thymeleaf标准方言

(1)如何识别标准方言

<span th:text="...">
<span data-th-text="...">

(2)标准方言:

标准表达式、设置属性值、迭代器、条件语句、模板布局、属性优先级、注释、内联、基本对象、工具对象...

(3)标准表达式

-- 变量表达式

语法:${...}

<span th:text="${book.author.name}">

-- 消息表达式(也称为文本外部化、国际化或者i18n)

语法:#{...}

<table>
    ...
    <th th:text="#{header.address.city}">...</th>
    ...
</table>
    

-- 选择表达式

语法:*{...}

<div th:object="${book}">
    ...
    <span th:text="*{title}">...</span>
    ...
</div>

与变量表达式区别:它们是在当前选择的对象而不是整个上下文变量映射上执行

-- 链接表达式

语法:@{...}


-- 分段表达式

语法:th:insert或th:replace


-- 字面量

文本


数字


布尔

<div th:if="${user.isAdmin()} == false"> ...

null

<div th:if="${variable.someing} == null"> ...

算术操作

+、-、*、/、%

<div th:with="isEven=(${prodStat.count}%2 == 0)">

比较和等价

比较:>、<、>=、<=(gt、lt、ge、le)

<ul class="pagination" data-th-if="${page.totalPages le 7}">

等价:==、!=(eq、ne)

<option data-th-each="i:${#arrays.toIntegerArray({5,10,40,100})}" data-th-value="${i}" data-th-selected="${i eq page.size}" data-th-text="${i}"></option>

条件运算符

<tr th:class="${row.even}?'even':'odd'">...</tr>

无操作

_

<span th:text="${user.name} ?:_">no user authenticated</span>

-- 设置属性值

<form action="subscript.html" th:attr=""action="@{/subscribe}">
    <fieldset>
        <input type="text" name="email">
        <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
    </fieldset>
</form>

设置值到指定属性值

<form action="subscript.html" th:action="@{/subscribe}">
    <fieldset>
        <input type="text" name="email">
        <input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
    </fieldset>
</form>

固定值布尔属性

<input type="checkbox" name="option2" checked/> <!-- HTML -->

<input type="checkbox" name="option1" checked="checked"/> <!-- XHTML -->
<input type="checkbox" name="active" th:checked="${user.active}"/>

-- 迭代器

基本的迭代 th:each

<li th:each="book : ${books}" th:text="${book.title}">En las Orillas del Sar</li>

状态变量:

index  count  size  current  even/odd  first  last

<tr th:each="prod,iterStat:${prods}" th:class="${iterStat.odd}?'odd'">
        <td th:text="${prod.name}">Option</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}?#{true} : #{false}">Yes</td>
    </tr>

-- 条件语句

th:if 、th:unless 

<a href="comments.html"
    th:href="@{/product/comments(prodId=${prod.id})}"
    th:if="${not #lists.isEmpty(prod.comments)}">
    view
</a>
<a href="comments.html"
    th:href="@{/product/comments(prodId=${prod.id})}"
    th:unless="${#lists.isEmpty(prod.comments)}">
    view
</a>

switch

<div th:switch="${user.role}">
    <p th:case="admin">User is an administrator</p>
    <p th:case="#{roles.manager}">User is an manager</p>
    <p th:case="*">Usre is some other thing</p>
</div>

3、模版布局

定义和引用片段

(1)第一种方式

定义片段:

引用片段:


(2)第二种方式 不使用th:fragment


(3)th:insert、th:replace、th:include

① th:insert 它将简单地插入指定的片段作为正文的主标签

② th:replace 用指定实际片段来替换其主标签

③ th:include 类似于th:insert,但不是插入片段它只插入此片段的内容(3.x版本后,不再推荐使用)

4、内联

(1)[[...]]或[(...)]分别对应th:text和th:utext

<p>The message is "[(${msg})]"</p>
<p>The message is "<b>This is great!</b>"</p>
<p>The message is "[[${msg}]]"</p>
<p>The message is "<b>This is great!</b>"</p>

(2)禁用内联

有时候需要禁用这种机制,比如,想输出[[...]]或[(...)]文本内容

<p th:inline="none">A double array looks like this:[[1,2,3],[4,5]]!</p>

(3)JavaScript内联

<script th:inline="javascript">
    ...
    var username = /*[[${session.user.name}]]*/ "Gertrud Kiwifruit";
    ...
</script>
                 
<script th:inline="javascript">
    ...
    var username = "Sebastian \"Fruity\" " Applejuice";
    ...
</script>
(4)CSS内联
classname = 'main elems'
align = 'center'

<style th:inline="css">
    .[[${classname}]]{
        text-align: [[${align}]];
    }
</style>

<style th:inline="css">
    .main\ elems{
        text-align: center;
    }
</style>

5、表达式基本对象

基本对象

(1)#ctx:上下文对象。是org.thymeleaf.context.lContext或者org.thymeleaf.context.IWebContext的实现

(2)#locale:直接访问与java.util.Locale关联的当前的请求

request/session等属性

(1)param:用于检索请求参数

(2)session:用于检索session属性

(3)application:用于检索application/servlet上下文属性

Web上下文对象

(1)#request:直接访问与当前请求关联的javax.servlet.http.HttpServletRequest

(2)#session:直接访问与当前请求关联的javax.servlet.http.HttpSession对象

(3)#servletContext:直接访问与当前请求关联的javax.servlet.ServletContext对象





猜你喜欢

转载自blog.csdn.net/a911711054/article/details/80821900