20
Modern Java web applications with Spring Boot and Thymeleaf Mr. LAY Leangsros Instructor & Web App Developer School of E-commerce and Logistics, NIPTICT m: +855 89 909 978 e: [email protected], [email protected] https:// goo.gl/V10mJL

Modern Java web applications with Spring Boot and Thymeleaf

Embed Size (px)

Citation preview

Page 1: Modern Java web applications with Spring Boot and Thymeleaf

Modern Java web applications with Spring Boot and ThymeleafMr. LAY Leangsros

Instructor & Web App Developer

School of E-commerce and Logistics,

NIPTICT

m: +855 89 909 978

e: [email protected],

[email protected]

https://goo.gl/V10mJL

Page 2: Modern Java web applications with Spring Boot and Thymeleaf

Web Frameworks

• Laravel, Symfony, CakePHP framework for PHP

• Django for Python

• Rails for Ruby

• ASP.NET for .NET

• Catalyst for Perl

Page 3: Modern Java web applications with Spring Boot and Thymeleaf

Content

• What is Spring?• History

• Features

• Structure

• What is Thymeleaf?• Using Thymeleaf

• Let’s build it!

Page 4: Modern Java web applications with Spring Boot and Thymeleaf

What is Spring?

• Spring is a mature yet still innovative

open-source framework

• Spring has a very active community

• Spring is light-weight

Page 5: Modern Java web applications with Spring Boot and Thymeleaf

History

• 1999 J2EE 1.2

• 2004 Spring Framework 1.0• Injection

• POJO oriented

• AOP & transactions

• 2006 Java EE 5

• 2013 Spring Boot!

Page 6: Modern Java web applications with Spring Boot and Thymeleaf

Features

• Stand-alone Spring applications

• Embedded Tomcat or Jetty

• Starter dependencies

• Automatic configuration

• Production-ready environment

• No code generation / no XML configuration

• Easy to use but powerful database transaction management capabilities

Page 7: Modern Java web applications with Spring Boot and Thymeleaf

Features

• Spring Data: Simplifies data access from

relational and NoSQL data stores.

• Spring Security: Robust security framework to

secure applications.

• Spring Social: Supports integration with

social networking sites like Facebook, Twitter,

LinkedIn, GitHub etc.

Page 8: Modern Java web applications with Spring Boot and Thymeleaf

Structure

• Controller (Request Mapping)

• Model (Object)

• Repository (Database, data manipulation)

• Service (Email, Notification..)

• Configure (Security, Endpoint, WebSocket, ..)

• …etc.

Page 10: Modern Java web applications with Spring Boot and Thymeleaf

Using Thymeleaf

Simple expressions:

•Variable Expressions: ${...}• Selection Variable Expressions: *{...}•Message Expressions: #{...}• Link URL Expressions: @{...}

Page 11: Modern Java web applications with Spring Boot and Thymeleaf

Using Thymeleaf

• th:field• th:form• th:alt• th:action• th:href

• th:src• th:value• th:replace• th:include• th:text

Binding expressions:

• th:if• th:each• th:remove

• th:switch/th:case

Page 12: Modern Java web applications with Spring Boot and Thymeleaf

Using Thymeleaf

Example

<h1 class="title" th:text="${post.title}"></h1> <div class="might-grid" th:each="post : ${posts}"></div> <img class="img-responsive" alt="" th:src="${post.thumbnailUrl}"/> <span th:utext="${post.content}"></span> <h4><a th:href="${'/news/' + post.id}" th:inline="text">[[${post.title}]]</a></h4>

<label th:text="#{name}"></label>

<input type="text" class="form-control" th:field="*{name}"/>

Page 13: Modern Java web applications with Spring Boot and Thymeleaf

Let’s Build it!

Requirements

• JDK 1.7 or later

• Spring Tool Suite

• (Internet)

Create New Project

• File -> New -> Spring Start Project

Page 14: Modern Java web applications with Spring Boot and Thymeleaf

Let’s Build it!

pom.xml<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

Page 15: Modern Java web applications with Spring Boot and Thymeleaf

Let’s Build it!

Spring Boot Application

@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

Page 16: Modern Java web applications with Spring Boot and Thymeleaf

Let’s Build it!

@Controller public class HomeController { @RequestMapping("/") public String greeting(Model model) { model.addAttribute("barcamp", "Hello Barcamp 2016"); return "index"; } }

Controller

Page 17: Modern Java web applications with Spring Boot and Thymeleaf

Let’s Build it!

Thymeleaf Template (View)

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<body>

<h1 th:text="${barcamp}"></h1>

</body>

</html>

Page 18: Modern Java web applications with Spring Boot and Thymeleaf

Let’s Build it!

server.port = 9999

Page 19: Modern Java web applications with Spring Boot and Thymeleaf

You have questions?

I (probably) have answers

Page 20: Modern Java web applications with Spring Boot and Thymeleaf

References

• Thymeleaf Advanced Usage https://doanduyhai.wordpress.com/2012/04/14/spring-mvc-part-iv-thymeleaf-advanced-usage/

• Thymeleaf Doc, http://www.thymeleaf.org/doc

• http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#the-template-engine

• Spring Boot – framework for micro services, Jakub Kubryński, ppt.