springboot中运用thymeleaf模板

thymeleaf是springboot推荐的模板引擎。那么如何在springboot工程中配置呢?

在appliaction.properties中加入

spring.thymeleaf.cache=false

spring.thymeleaf.mode=html

在pom.xml中将starter-web替换



org.springframework.boot

spring-boot-starter-thymeleaf

在src/main/resources下创建templates文件夹,里面存放模板文件。

例如 web.html

<!DOCTYPE html>





Layout









hello,



(注意html的html标签必须申明)

controller中

@RequestMapping(“/web”)

public String web(Map<String,Object> map) {

map.put(“username”,”Jack”);

return “web”;

}

需要输出转义的html

${..} --- OGNL 表达式

#{…}--- 输出properties文件中的

properties文件输出

和template中html同名的properties文件

比如在home.html中要输出 #{welcome}, 则需要一个home.properties文件,里面有一行 welcome=xxx

Simple expressions:

Variable Expressions: ${…}

Selection Variable Expressions: *{…}

Message Expressions: #{…}

Link URL Expressions: @{…}

Fragment Expressions: ~{…}

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: _

th:attr 分割

<img src=”../../images/gtvglogo.png”

th:attr=”src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}” />

th:action

th:value

th:href

H5标签

There are quite a lot of attributes like these, each of them targeting a specific HTML5 attribute:

th:abbr th:accept th:accept-charset

th:accesskey th:action th:align

th:alt th:archive th:audio

th:autocomplete th:axis th:background

th:bgcolor th:border th:cellpadding

th:cellspacing th:challenge th:charset

th:cite th:class th:classid

th:codebase th:codetype th:cols

th:colspan th:compact th:content

th:contenteditable th:contextmenu th:data

th:datetime th:dir th:draggable

th:dropzone th:enctype th:for

th:form th:formaction th:formenctype

th:formmethod th:formtarget th:fragment

0%