Upload
lay-leangsros
View
191
Download
7
Embed Size (px)
Citation preview
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
https://goo.gl/V10mJL
Web Frameworks
• Laravel, Symfony, CakePHP framework for PHP
• Django for Python
• Rails for Ruby
• ASP.NET for .NET
• Catalyst for Perl
Content
• What is Spring?• History
• Features
• Structure
• What is Thymeleaf?• Using Thymeleaf
• Let’s build it!
What is Spring?
• Spring is a mature yet still innovative
open-source framework
• Spring has a very active community
• Spring is light-weight
History
• 1999 J2EE 1.2
• 2004 Spring Framework 1.0• Injection
• POJO oriented
• AOP & transactions
• 2006 Java EE 5
• 2013 Spring Boot!
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
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.
Structure
• Controller (Request Mapping)
• Model (Object)
• Repository (Database, data manipulation)
• Service (Email, Notification..)
• Configure (Security, Endpoint, WebSocket, ..)
• …etc.
What is Thymeleaf?
Thymeleaf is a Java XML/XHTML/HTML5
template engine that can work both
in web (Servlet-based) and non-web environments.
Who's using Thymeleaf?
Using Thymeleaf
Simple expressions:
•Variable Expressions: ${...}• Selection Variable Expressions: *{...}•Message Expressions: #{...}• Link URL Expressions: @{...}
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
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}"/>
Let’s Build it!
Requirements
• JDK 1.7 or later
• Spring Tool Suite
• (Internet)
Create New Project
• File -> New -> Spring Start Project
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>
Let’s Build it!
Spring Boot Application
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Let’s Build it!
@Controller public class HomeController { @RequestMapping("/") public String greeting(Model model) { model.addAttribute("barcamp", "Hello Barcamp 2016"); return "index"; } }
Controller
Let’s Build it!
Thymeleaf Template (View)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1 th:text="${barcamp}"></h1>
</body>
</html>
Let’s Build it!
server.port = 9999
You have questions?
I (probably) have answers
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.