热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot入门一构建简单工程

环境准备:jdk1.7(推荐)以上,tomcat8(推荐)以上,或者使用插件自带。mevan插件3.2以上,eclipse编辑工具pom文件基本配置如下

环境准备:jdk1.7(推荐)以上,tomcat8(推荐)以上,或者使用插件自带。mevan插件3.2以上,eclipse编辑工具
pom文件基本配置如下



project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
modelVersion 4.0.0 /modelVersion
groupId springboot /groupId
artifactId weboot /artifactId
version 0.0.1-SNAPSHOT /version
packaging jar /packaging
name weboot /name
url http://maven.apache.org /url
properties
project.build.sourceEncoding UTF-8 /project.build.sourceEncoding
/properties
parent
!--Spring Boot基础父类,其中包含了很多必要的jar包,如果不使用父类,则需要自己去依赖这些jars --
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-parent /artifactId
version 1.3.3.RELEASE /version
/parent
dependencies
dependency
groupId junit /groupId
artifactId junit /artifactId
version 3.8.1 /version
scope test /scope
/dependency
!-- web程序的启动项依赖,通过此依赖可引入内嵌的tomcat等web必须的jars --
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-web /artifactId
/dependency
!-- spring-boot aop- --
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-aop /artifactId
/dependency
!-- thymeleaf程序的启动项依赖,spring-boot对thymeleaf模板引擎支持最好,建议模板引擎使用此框架 --
dependency
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-thymeleaf /artifactId
/dependency
!-- mysql依赖,使用spring-data-jpa需要指定一个数据库方言,用于连接数据库,即mysql驱动 --
dependency
groupId mysql /groupId
artifactId mysql-connector-java /artifactId
/dependency
/dependencies
build
plugins
!-- 通过maven构建的插件 --
plugin
groupId org.springframework.boot /groupId
artifactId spring-boot-maven-plugin /artifactId
/plugin
/plugins
/build
/project

手动添加 config,templates,static 文件夹 分别存放配置文件,模板,静态文件(js,css等)
模板格式其一详解

Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用。



Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。Thymeleaf的可扩展性也非常棒。你可以使用它定义自己的模板属性集合,这样就可以计算自定义表达式并使用自定义逻辑。这意味着Thymeleaf还可以作为模板引擎框架。

Thymeleaf的模板还可以用作工作原型,Thymeleaf会在运行期替换掉静态值。例如下面的html文件,当作为静态文件时,product name显示为Red Chair,当运行在容器中并提供product这个对象时,product name的值会自动替换为product.description对应的值。

1.bean值替换

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 2 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 1: bean values /h1
h2 Product information /h2
dl
dt Product name /dt
dd th:text="${product.description}" Red Chair /dd
dt Product price /dt
dd th:text="${product.price}" 350 /dd
dt Product available from /dt
dd th:text="${product.availableFrom}" 2014-12-01 /dd
/dl
/body
/html

2.简单数据转换(数字,日期)

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 2 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 2: bean values /h1
h2 Product information /h2
dl
dt Product name /dt
dd th:text="${product.description}" red Chair /dd
dt Product price /dt
dd th:text="${#numbers.formatDecimal(product.price, 1, 2)}" 180 /dd
dt Product available from /dt
dd th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}" 2014-12-01 /dd
/dl
/body
/html

3.字符串拼接

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 3 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 3: string concatenation /h1
h2 Product information /h2
dl
dt Product price /dt
dd th:text="${'$'+product.price}" 235 /dd
/dl
/body
/html

4.国际化

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title th:text="#{tutorial.exercise4}" Thymeleaf tutorial: exercise 4 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 th:text="#{tutorial.exercise4}" Thymeleaf tutorial - Solution for exercise 4: internationalization /h1
h2 th:text="#{product.info}" Product information /h2
dl
dt th:text="#{product.name}" Product name /dt
dd th:text="${product.description}" Red chair /dd
dt th:text="#{product.price}" Product price /dt
dd th:text="${#numbers.formatDecimal(product.price, 1, 2)}" 350 /dd
dt th:text="#{product.available}" Product available from /dt
dd th:text="${#dates.format(product.availableFrom, 'dd-MMM-yyyy')}" 28-Jun-2013 /dd
/dl
/body
/html

此时html需要相应的配置文件。例如如下配置文件:

en:

tutorial.exercise4=Thymeleaf tutorial - exercise 4: internationalization
product.info=Product information
product.name=Product name
product.price=Product price
product.available=Product available from
back=Back

fr

tutorial.exercise4=Tutorial De Thymeleaf - exercice 4: l'internationalisation
product.info=Information du produit
product.name=Nom du produit
product.price=Prix du produit
product.available=Produit disponible depuis
back=Revenir

5.转义和非转义文本

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 5 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 5: escaped and unescaped text /h1
div th:text="${html}"
Some escaped text
/div
div th:utext="${html}"
Some unescaped text
/div
/body
/html

上述两个div分别生成的html代码为

div This is an em HTML /em text. b Enjoy yourself! /b /div
div This is an em HTML /em text. b Enjoy yourself! /b /div

6.迭代

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 6 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 6: iteration /h1
h2 Product list /h2
table
thead
tr
th Description /th
th Price /th
th Available from /th
/tr
/thead
tbody th:remove="all-but-first"
tr th:each="product:${productList}"
td th:text="${product.description}" Red Chair /td
td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}" $123 /td
td th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}" 2014-12-01 /td
/tr
tr
td White table /td
td $200 /td
td 15-Jul-2013 /td
/tr
tr
td Reb table /td
td $200 /td
td 15-Jul-2013 /td
/tr
tr
td Blue table /td
td $200 /td
td 15-Jul-2013 /td
/tr
/tbody
/table
/body
/html

7.迭代统计

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 7 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 7: iteration stats /h1
h2 Product list /h2
table
thead
tr
th Index /th
th Description /th
th Price /th
th Available from /th
/tr
/thead
tbody th:remove="all-but-first"
tr th:each="product : ${productList}"
td th:text="${productStat.count}" 1 /td
td th:text="${product.description}" Red chair /td
td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}" $350 /td
td th:text="${#dates.format(product.availableFrom, 'dd-MMM-yyyy')}" 28-Jun-2013 /td
/tr
tr
td 2 /td
td White table /td
td $200 /td
td 15-Jul-2013 /td
/tr
tr
td 3 /td
td Reb table /td
td $200 /td
td 15-Jul-2013 /td
/tr
tr
td 4 /td
td Blue table /td
td $200 /td
td 15-Jul-2013 /td
/tr
/tbody
/table
/body
/html

8.条件判断

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 8 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 8: conditions /h1
h2 Product list /h2
table
thead
tr
th Description /th
th Price /th
th Available from /th
th /th
/tr
/thead
tbody
tr th:each="product : ${productList}"
td th:text="${product.description}" Red chair /td
td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}" $350 /td
td th:text="${#dates.format(product.availableFrom, 'dd-MMM-yyyy')}" 28-Jun-2013 /td
td
span th:if="${product.price lt 100}" Special offer! /span
/td
/tr
/tbody
/table
/body
/html

9.更多条件判断

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 9 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 9: more on conditions /h1
h2 Customer list /h2
table
thead
tr
th First name /th
th Last name /th
th Gender /th
th Payment method /th
th Balance /th
/tr
/thead
tbody th:remove="all-but-first"
tr th:each="customer : ${customerList}"
td th:text="${customer.firstName}" Peter /td
td th:text="${customer.lastName}" Jackson /td
!--
Use th:switch for selecting content based on ${customer.gender}.
As genre can be null if unknown, better use ${customer.gender?.name()}
for obtaining its name.
--
td th:switch="${customer.gender?.name()}"
img th:case="'MALE'" src="../../../images/male.png" th:src="@{/images/male.png}" alt="Male" / !-- Use "/images/male.png" image --
img th:case="'FEMALE'" src="../../../images/female.png" th:src="@{/images/female.png}" alt="Female" / !-- Use "/images/female.png" image --
span th:case="*" Unknown /span
/td
td
span th:text="${customer.paymentMethod.description}" Direct debit /span
!-- Show next message only when paymentMethod is not CREDIT_CARD --
span th:unless="${customer.paymentMethod.name() == 'CREDIT_CARD'}"
Payment must be done in the next 4 days
/span
/td
!-- Add when balance is greater than 10000 --
td th:class="${customer.balance gt 10000} ? 'enhanced'" th:text="${customer.balance}" 350 /td
/tr
tr
td Mary /td
td Johanson /td
td img src="../../../images/female.png" / /td
td span Credit card /span /td
td 5000 /td
/tr
tr
td Robert /td
td Allen /td
td img src="../../../images/male.png" / /td
td
span Credit card /span
span Payment must be done in the next 4 days /span
/td
td 50000 /td
/tr
/tbody
/table
/body
/html

10.Spring表达式语言

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 10 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 10: Spring Expression language /h1
h2 Arithmetic expressions /h2
p Four multiplied by minus six multiplied by minus two module seven: /p
p th:text="${4 * -6 * -2 % 7}" 123 /p
h2 Object navigation /h2
p Description field of paymentMethod field of the third element of customerList bean: /p
p th:text="${customerList[2].paymentMethod.description}" Credit card /p
h2 Object instantiation /h2
p Current time milliseconds: /p
p th:text="${new java.util.Date().getTime()}" 22-Jun-2013 /p
h2 T operator /h2
p Random number: /p
p th:text="${T(java.lang.Math).random()}" 123456 /p
/body
/html

11.超链接

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 11 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 11: links /h1
h2 Product actions /h2
ul
li a href="#" th:href="@{/exercise11/product.html(action='view')}" View product /a /li
li a href="#" th:href="@{/exercise11/product.html(action='edit')}" Edit product /a /li
/ul
/body
/html

12.表单

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 12 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 12: forms /h1
h2 Customer edition /h2
form action="saveCustomer.html" th:action="@{/exercise12/saveCustomer.html}" th:object="${customer}" method="post"
input type="hidden" th:field="*{id}" /
label for="firstName" First name: /label
input type="text" th:field="*{firstName}" value="John" /
label for="lastName" Last name: /label
input type="text" th:field="*{lastName}" value="Wayne" /
Genre:
div th:each="gender : ${genders}"
input type="radio" th:value="${gender}" th:field="*{gender}" /
label th:for="${#ids.prev('gender')}" th:text="${gender.description}" Male /label
/div
div th:remove="all"
input type="radio" /
label Female /label
/div
label for="paymentMethod" Payment method: /label
select th:field="*{paymentMethod}" th:remove="all-but-first"
option th:each="paymentMethod : ${paymentMethods}"
33 th:value="${paymentMethod}" th:text="${paymentMethod.description}" Credit card /option
option Another payment method /option
option Another payment method /option
/select
label for="balance" Balance (dollars): /label
input type="text" th:field="*{balance}" size="10" value="2500" /
input type="submit" /
/form
/body
/html

13.内联

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 13 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 13: inlining /h1
h2 Birthday email /h2
form action="#" method="post"
label for="body" Message body: /label
textarea id="body" name="body" th:inline="text"
Dear [[${customerName}]],
it is our sincere pleasure to congratulate your in your birthday:
Happy birthday [[${customerName}]]!!!
See you soon, [[${customerName}]].
Regards,
The Thymeleaf team
/textarea
input type="submit" value="Send mail" /
/form
/body
/html

--------------------------------------------------------------------------------------------------------------

以上资料都来自http://itutorial.thymeleaf.org/。如果对Thymeleaf有兴趣,可以试试他们做的交互教程,很是好用。上面的html代码都是来自thymeleaf的交互教程

Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用。

Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。Thymeleaf的可扩展性也非常棒。你可以使用它定义自己的模板属性集合,这样就可以计算自定义表达式并使用自定义逻辑。这意味着Thymeleaf还可以作为模板引擎框架。

Thymeleaf的模板还可以用作工作原型,Thymeleaf会在运行期替换掉静态值。例如下面的html文件,当作为静态文件时,product name显示为Red Chair,当运行在容器中并提供product这个对象时,product name的值会自动替换为product.description对应的值。

1.bean值替换

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 2 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 1: bean values /h1
h2 Product information /h2
dl
dt Product name /dt
dd th:text="${product.description}" Red Chair /dd
dt Product price /dt
dd th:text="${product.price}" 350 /dd
dt Product available from /dt
dd th:text="${product.availableFrom}" 2014-12-01 /dd
/dl
/body
/html

2.简单数据转换(数字,日期)

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 2 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 2: bean values /h1
h2 Product information /h2
dl
dt Product name /dt
dd th:text="${product.description}" red Chair /dd
dt Product price /dt
dd th:text="${#numbers.formatDecimal(product.price, 1, 2)}" 180 /dd
dt Product available from /dt
dd th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}" 2014-12-01 /dd
/dl
/body
/html

3.字符串拼接

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 3 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 3: string concatenation /h1
h2 Product information /h2
dl
dt Product price /dt
dd th:text="${'$'+product.price}" 235 /dd
/dl
/body
/html

4.国际化

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title th:text="#{tutorial.exercise4}" Thymeleaf tutorial: exercise 4 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 th:text="#{tutorial.exercise4}" Thymeleaf tutorial - Solution for exercise 4: internationalization /h1
h2 th:text="#{product.info}" Product information /h2
dl
dt th:text="#{product.name}" Product name /dt
dd th:text="${product.description}" Red chair /dd
dt th:text="#{product.price}" Product price /dt
dd th:text="${#numbers.formatDecimal(product.price, 1, 2)}" 350 /dd
dt th:text="#{product.available}" Product available from /dt
dd th:text="${#dates.format(product.availableFrom, 'dd-MMM-yyyy')}" 28-Jun-2013 /dd
/dl
/body
/html

此时html需要相应的配置文件。例如如下配置文件:

en:

tutorial.exercise4=Thymeleaf tutorial - exercise 4: internationalization
product.info=Product information
product.name=Product name
product.price=Product price
product.available=Product available from
back=Back

fr

tutorial.exercise4=Tutorial De Thymeleaf - exercice 4: l'internationalisation
product.info=Information du produit
product.name=Nom du produit
product.price=Prix du produit
product.available=Produit disponible depuis
back=Revenir

5.转义和非转义文本

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 5 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 5: escaped and unescaped text /h1
div th:text="${html}"
Some escaped text
/div
div th:utext="${html}"
Some unescaped text
/div
/body
/html

上述两个div分别生成的html代码为

div This is an em HTML /em text. b Enjoy yourself! /b /div
div This is an em HTML /em text. b Enjoy yourself! /b /div

6.迭代

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 6 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 6: iteration /h1
h2 Product list /h2
table
thead
tr
th Description /th
th Price /th
th Available from /th
/tr
/thead
tbody th:remove="all-but-first"
tr th:each="product:${productList}"
td th:text="${product.description}" Red Chair /td
td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}" $123 /td
td th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}" 2014-12-01 /td
/tr
tr
td White table /td
td $200 /td
td 15-Jul-2013 /td
/tr
tr
td Reb table /td
td $200 /td
td 15-Jul-2013 /td
/tr
tr
td Blue table /td
td $200 /td
td 15-Jul-2013 /td
/tr
/tbody
/table
/body
/html

7.迭代统计

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 7 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 7: iteration stats /h1
h2 Product list /h2
table
thead
tr
th Index /th
th Description /th
th Price /th
th Available from /th
/tr
/thead
tbody th:remove="all-but-first"
tr th:each="product : ${productList}"
td th:text="${productStat.count}" 1 /td
td th:text="${product.description}" Red chair /td
td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}" $350 /td
td th:text="${#dates.format(product.availableFrom, 'dd-MMM-yyyy')}" 28-Jun-2013 /td
/tr
tr
td 2 /td
td White table /td
td $200 /td
td 15-Jul-2013 /td
/tr
tr
td 3 /td
td Reb table /td
td $200 /td
td 15-Jul-2013 /td
/tr
tr
td 4 /td
td Blue table /td
td $200 /td
td 15-Jul-2013 /td
/tr
/tbody
/table
/body
/html

8.条件判断

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 8 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 8: conditions /h1
h2 Product list /h2
table
thead
tr
th Description /th
th Price /th
th Available from /th
th /th
/tr
/thead
tbody
tr th:each="product : ${productList}"
td th:text="${product.description}" Red chair /td
td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}" $350 /td
td th:text="${#dates.format(product.availableFrom, 'dd-MMM-yyyy')}" 28-Jun-2013 /td
td
span th:if="${product.price lt 100}" Special offer! /span
/td
/tr
/tbody
/table
/body
/html

9.更多条件判断

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 9 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 9: more on conditions /h1
h2 Customer list /h2
table
thead
tr
th First name /th
th Last name /th
th Gender /th
th Payment method /th
th Balance /th
/tr
/thead
tbody th:remove="all-but-first"
tr th:each="customer : ${customerList}"
td th:text="${customer.firstName}" Peter /td
td th:text="${customer.lastName}" Jackson /td
!--
Use th:switch for selecting content based on ${customer.gender}.
As genre can be null if unknown, better use ${customer.gender?.name()}
for obtaining its name.
--
td th:switch="${customer.gender?.name()}"
img th:case="'MALE'" src="../../../images/male.png" th:src="@{/images/male.png}" alt="Male" / !-- Use "/images/male.png" image --
img th:case="'FEMALE'" src="../../../images/female.png" th:src="@{/images/female.png}" alt="Female" / !-- Use "/images/female.png" image --
span th:case="*" Unknown /span
/td
td
span th:text="${customer.paymentMethod.description}" Direct debit /span
!-- Show next message only when paymentMethod is not CREDIT_CARD --
span th:unless="${customer.paymentMethod.name() == 'CREDIT_CARD'}"
Payment must be done in the next 4 days
/span
/td
!-- Add when balance is greater than 10000 --
td th:class="${customer.balance gt 10000} ? 'enhanced'" th:text="${customer.balance}" 350 /td
/tr
tr
td Mary /td
td Johanson /td
td img src="../../../images/female.png" / /td
td span Credit card /span /td
td 5000 /td
/tr
tr
td Robert /td
td Allen /td
td img src="../../../images/male.png" / /td
td
span Credit card /span
span Payment must be done in the next 4 days /span
/td
td 50000 /td
/tr
/tbody
/table
/body
/html

10.Spring表达式语言

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 10 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 10: Spring Expression language /h1
h2 Arithmetic expressions /h2
p Four multiplied by minus six multiplied by minus two module seven: /p
p th:text="${4 * -6 * -2 % 7}" 123 /p
h2 Object navigation /h2
p Description field of paymentMethod field of the third element of customerList bean: /p
p th:text="${customerList[2].paymentMethod.description}" Credit card /p
h2 Object instantiation /h2
p Current time milliseconds: /p
p th:text="${new java.util.Date().getTime()}" 22-Jun-2013 /p
h2 T operator /h2
p Random number: /p
p th:text="${T(java.lang.Math).random()}" 123456 /p
/body
/html

11.超链接

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 11 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Answer for exercise 11: links /h1
h2 Product actions /h2
ul
li a href="#" th:href="@{/exercise11/product.html(action='view')}" View product /a /li
li a href="#" th:href="@{/exercise11/product.html(action='edit')}" Edit product /a /li
/ul
/body
/html

12.表单

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 12 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 12: forms /h1
h2 Customer edition /h2
form action="saveCustomer.html" th:action="@{/exercise12/saveCustomer.html}" th:object="${customer}" method="post"
input type="hidden" th:field="*{id}" /
label for="firstName" First name: /label
input type="text" th:field="*{firstName}" value="John" /
label for="lastName" Last name: /label
input type="text" th:field="*{lastName}" value="Wayne" /
Genre:
div th:each="gender : ${genders}"
input type="radio" th:value="${gender}" th:field="*{gender}" /
label th:for="${#ids.prev('gender')}" th:text="${gender.description}" Male /label
/div
div th:remove="all"
input type="radio" /
label Female /label
/div
label for="paymentMethod" Payment method: /label
select th:field="*{paymentMethod}" th:remove="all-but-first"
option th:each="paymentMethod : ${paymentMethods}"
33 th:value="${paymentMethod}" th:text="${paymentMethod.description}" Credit card /option
option Another payment method /option
option Another payment method /option
/select
label for="balance" Balance (dollars): /label
input type="text" th:field="*{balance}" size="10" value="2500" /
input type="submit" /
/form
/body
/html

13.内联

!DOCTYPE html
html xmlns:th="http://www.thymeleaf.org"
head
title Thymeleaf tutorial: exercise 13 /title
link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /
meta charset="utf-8" /
/head
body
h1 Thymeleaf tutorial - Solution for exercise 13: inlining /h1
h2 Birthday email /h2
form action="#" method="post"
label for="body" Message body: /label
textarea id="body" name="body" th:inline="text"
Dear [[${customerName}]],
it is our sincere pleasure to congratulate your in your birthday:
Happy birthday [[${customerName}]]!!!
See you soon, [[${customerName}]].
Regards,
The Thymeleaf team
/textarea
input type="submit" value="Send mail" /
/form
/body
/html

--------------------------------------------------------------------------------------------------------------

以上资料都来自http://itutorial.thymeleaf.org/。如果对Thymeleaf有兴趣,可以试试他们做的交互教程,很是好用。上面的html代码都是来自thymeleaf的交互教程


   



推荐阅读
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 开发笔记:spring boot项目打成war包部署到服务器的步骤与注意事项
    本文介绍了将spring boot项目打成war包并部署到服务器的步骤与注意事项。通过本文的学习,读者可以了解到如何将spring boot项目打包成war包,并成功地部署到服务器上。 ... [详细]
  • 本文介绍了在sqoop1.4.*版本中,如何实现自定义分隔符的方法及步骤。通过修改sqoop生成的java文件,并重新编译,可以满足实际开发中对分隔符的需求。具体步骤包括修改java文件中的一行代码,重新编译所需的hadoop包等。详细步骤和编译方法在本文中都有详细说明。 ... [详细]
  • 解决java.lang.IllegalStateException: ApplicationEventMulticaster not initialized错误的方法和原因
    本文介绍了解决java.lang.IllegalStateException: ApplicationEventMulticaster not initialized错误的方法和原因。其中包括修改包名、解决service name重复、处理jar包冲突和添加maven依赖等解决方案。同时推荐了一个人工智能学习网站,该网站内容通俗易懂,风趣幽默,值得一看。 ... [详细]
  • Skywalking系列博客1安装单机版 Skywalking的快速安装方法
    本文介绍了如何快速安装单机版的Skywalking,包括下载、环境需求和端口检查等步骤。同时提供了百度盘下载地址和查询端口是否被占用的命令。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
author-avatar
手机用户2502882291
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有