Skip to content

Instantly share code, notes, and snippets.

@fabianogoes
Last active April 11, 2019 13:08
Show Gist options
  • Save fabianogoes/7f259dd71532b3ebfb0e to your computer and use it in GitHub Desktop.
Save fabianogoes/7f259dd71532b3ebfb0e to your computer and use it in GitHub Desktop.
Spring Boot with Thymeleaf

Spring Boot with Thymeleaf

Thymeleaf - HTML Basic:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8"/>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta name="viewport" content="width=device-width" />
		<title>Spring Boot Web Application with Thymeleaf</title>
		<!-- Twitter Bootstrap -->
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
			integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"/>
	</head>
<body>

	<h2 th:text="${message}" class="text-center"></h2>
	
</body>
</html>

Thymeleaf - Basic HTML Reference


Thymeleaf - Foreach in List to Table:

<table class="table table-bordered">
	<thead>
		<tr>
			<td>Name</td><td>Document</td>
		</tr>
	</thead>
	<tbody>
		<!-- list = List added in Context by Java/Spring Controller -->
		<tr th:each="customer : ${list}">
			<td th:text="${customer.name}"></td>
			<td th:text="${customer.document}"></td>
		</tr>
	</tbody>
</table>

Thymeleaf Table Reference


Thymeleaf - Example Form:

<!-- customer = Model Class Added in Context by Java/Spring Controller -->
<form method="POST" th:action="@{/customer}" th:object="${customer}">
	<input type="text" class="form-control" placeholder="Name..." th:field="*{name}"/>
	<input type="text" class="form-control" placeholder="Document..." th:field="*{document}"/>
	<button type="submit" class="btn btn-primary">Save</button>
</form>

Thymeleaf Form Reference


Conditional expressions

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

<!-- Conditional expressions can also be nested using parentheses: -->
<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
  ...
</tr>

<!-- Else expressions can also be omitted, in which case a null value is returned if the condition is false: -->
<tr th:class="${row.even}? 'alt'">
  ...
</tr>

Thyneleaf - Conditional expressions Reference


Default expressions

<div th:object="${session.user}">
  ...
  <p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p>
</div>

<p>Age: <span th:text="*{age != null}? *{age} : '(no age specified)'">27</span>.</p>

<p>
  Name: 
  <span th:text="*{firstName}?: (*{admin}? 'Admin' : #{default.username})">Sebastian</span>
</p>

Thymeleaf Default Expressions Reference


Common Thymeleaf properties

spring.thymeleaf.cache=true # Enable template caching.
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
spring.thymeleaf.content-type=text/html # Content-Type value.
spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.
spring.thymeleaf.encoding=UTF-8 # Template encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain.
spring.thymeleaf.view-names= # Comma-separated

Common application properties Reference


Datasource Configuration Properties:

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Datasource Configuration Reference


Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment