Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thymeleaf and Spring Controllers

Similar presentations


Presentation on theme: "Thymeleaf and Spring Controllers"— Presentation transcript:

1 Thymeleaf and Spring Controllers
Spring Essentials Thymeleaf and Spring Controllers Spring Essentials SoftUni Team Technical Trainers Software University © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

2 Table of Contents Thymeleaf Spring Controllers Standard Expressions
Forms Conditionals Loops Spring Controllers © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

3 Have a Question? sli.do #JavaWeb

4 Thymeleaf The Templating Engine

5 What is Thymeleaf? Thymeleaf is a view engine used in Spring
It allows us to: Use variables/collections in our views Execute operations on our variables "Thymeleaf is very, very extensible, and it allows you to define your own sets of template attributes (or even tags) with the names you want, evaluating the expressions you want in the syntax you want and applying the logic you want. It’s more like a template engine framework." - thymeleaf.org © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

6 How to use Thymeleaf? Use Spring Initializr to import Thymeleaf, or use this dependency in your pom.xml: Define the Thymeleaf library in your html file: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <html xmlns=" xmlns:th="

7 Change Thymeleaf Version
You also need to change the Thymeleaf version in your pom.xml: <properties> <thymeleaf.version> 3.0.2.RELEASE </thymeleaf.version> <thymeleaf-layout-dialect.version> 2.2.0 </thymeleaf-layout-dialect.version> </properties>

8 Thymeleaf Tags and Attributes
All Thymeleaf tags and attributes begin with th: by default Example of Thymeleaf attribute: Example of Thymeleaf tag(element processor): th:block is an attribute container that disappears in the HTML <p th:text="Example"></p> <th:block> ... </th:block> © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

9 Thymeleaf Standard Expressions
Variable Expressions Selection Expressions Link (URL) Expressions Fragment Expressions ${...} *{...} More on the standard dialect here - @{...} ~{...} © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

10 Thymeleaf Variable Expressions
Variable Expressions are executed on the context variables Examples: ${...} ${session.user.name} ${title} ${game.id}

11 Thymeleaf Selection Expressions
Selection Expressions are executed on the previously selected object Example: *{...} <div th:object="${book}"> ... <span th:text="*{title}">...</span> //equivalent to ${book.title} </div>

12 Thymeleaf Link Expressions
Link Expressions are used to build URLs Example: You can also pass query string parameters: Create dynamic URLs @{...} <a <a <a © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

13 Variables in Thymeleaf
Variables in Thymeleaf are defined using the following attribute: Where 'var' is the name of our variable and 'pesho' it’s the value Variables exists only in the scope of the tag th:with="var = 'pesho'" <div th:with="name = ${user.name}"> <p th:text="${name}"> // valid </div> <p th:text="${name}"> // invalid

14 Thymeleaf Fragment Expressions
Fragment Expressions are easy way to move around templates Example: ~{...} <div th:with="frag=~{footer :: #main}"> <p th:insert="${frag}"> </div> © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

15 Thymeleaf Fragments Including entire file Including only a fragment
Including fragment with parameter You can also use th:replace in order to replace the entire element th:insert="~{fragments/header}" th:insert="~{fragments/header :: dark}" th:insert="~{fragments/header (theme='dark')}"

16 Forms in Thymeleaf In Thymeleaf you can create almost normal HTML forms: You can have a controller that will accept an object of given type: <form th:method="post"> <input type="number" name="id"/> <input type="text" name="name"/> <input type="submit"/> </form> @PostMapping("/user") public ModelAndView User user) { ... }

17 Forms in Thymeleaf (2) You can pass objects to forms in order to use validations: The th:field attribute creates different attributes based on the input type <form th:method="post" th:object=${user}> <input type="number" th:field="*{id}"/> <input type="text" th:field="*{name}"/> <input type="submit"/> </form>

18 Conditional Statements in Thymeleaf
If statements can be created in the following way: You can create inverted if statements using th:unless: <div th:if="${…}"> <p>The statement is true</p> </div> <div th:unless="${…}"> <p>The statement is false</p> </div>

19 Conditional Statements in Thymeleaf (2)
You can also create switch in which the default case is "*": Or use ternary operator: <div th:switch="${writer.role}"> <p th:case="'ADMIN'">The user is admin</p> <p th:case="'MOD'">The user is moderator</p> <p th:case="*">The user has no privileges</p> </div> <p th:text="${row.even} ? 'even' : 'odd'"> ... </p>

20 Loops in Thymeleaf Creating a for loop: Example:
<div th:each="element : ${#numbers.sequence(start, end, step)}"> <p th:text="${element}"></p> </div> <div th:each="element : ${#numbers.sequence(1, 5)}"> <p th:text="${element}"></p> </div> //

21 Loops in Thymeleaf (2) Creating a for-each loop: Getting the iterator:
<div th:each="book : ${books}"> <p th:text="*{author}"></p> </div> <div th:each="u, iter : ${users}" th:object="${u}"> <p th:text="|ID: *{id}, Username: *{name}|"></p> <p th:text="|${iter.index} of ${iter.size}|"></p> </div>

22 Annotations, IoC Container
Spring Controllers Annotations, IoC Container

23 Spring Controllers Defined with the @Controller annotation:
Controllers can contain multiple actions on different routes. @Controller public class HomeController { }

24 Controller Actions Annotated with with @RequestMapping(…) Or
@RequestMapping("/home") public String home() { return "home-view"; } @RequestMapping("/home") public ModelAndView home(ModelAndView mav) { mav.setViewName("home-view"); return mav; }

25 Request Mapping Problem when is that it accepts all types of request methods (get, post, put, delete, head, patch…) Execute only on GET requests : @RequestMapping(value="/home", method=RequestMethod.GET) public String home() { return "home-view"; }

26 Get Mapping Easier way to create route for a GET request:
This is alias for RequestMapping with method GET @GetMapping("/home") public String home() { return "home-view"; }

27 Post Mapping Similar to the GetMapping there is also an alias for RequestMapping with method POST: Similar annotations exist for all other types of request methods @PostMapping("/register") public String register() { }

28 Controller Routing You can set all actions in a controller to start with a given routing: Calling the adminPanel() will be done on route "/admin/" @Controller @RequestMapping("/admin") public class AdminController { @GetMapping("/") public String adminPanel() { }

29 Passing Attributes to View
Passing a string to the view: The Model object will be automatically passed to the view as context variables and the attributes can be accessed from Thymeleaf using the Expression syntax - @GetMapping("/") public String welcome(Model model) { model.addAttribute("name", "Pesho"); return "welcome"; } ${name}

30 Working with the Session
The session will be injected from the IoC container when called: Later the session attributes can be accessed from Thymeleaf using the expression syntax and the #session object: @GetMapping("/") public String home(HttpSession httpSession) { httpSession.setAttribute("id", 2); } ${session.id}

31 Setting a Cookie In order to set a cookie, we need to access the HttpResponse: @GetMapping("/darktheme") public String darkTheme(HttpServletResponse httpResponse) { httpResponse.addCookie("theme", "dark"); }

32 Getting a Cookie Accessing a cookie, you've set previously:
Setting a default value public String String theme) { } public String = "theme", defaultValue = "bootstrap.min.css") String theme ) { }

33 Http Request Accessing the HttpRequest: Accessing all cookies:
public String index(HttpServletRequest httpRequest) { } public String index(HttpServletRequest httpRequest) { Cookie[] cookies = httpRequest.getCookies(); }

34 Request Parameters Getting a parameter from the query string:
@RequestParam can also be used to get POST parameters @GetMapping("/details") public String Long id) { } @PostMapping("/register") public String String name) { … }

35 Request Parameters with Default Value
Getting a parameter from the query string: Making parameter optional: @GetMapping("/comment") public String defaultValue = "Annonymous") String author) { … } @GetMapping("/search") public String required = false) String sort) { … }

36 Form Objects Spring will automatically try to fill objects with a form data The input field names must be the same as the object field names @PostMapping("/register") public String UserDTO userDto) { }

37 Redirecting Redirecting after POST request: @PostMapping("/register")
public String UserDTO userDto) { return "redirect:/login"; }

38 Redirecting with Parameters
Redirecting with query string parameters @PostMapping("/register") public String UserDTO userDto, RedirectAttributes redirectAttributes) { redirectAttributes.addAttribute("errorId", 3); return "redirect:/login"; }

39 Redirecting with Attributes
Keeping objects after redirect @PostMapping("/register") public String UserDTO userDto, RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("userDto", userDto); return "redirect:/register"; }

40 Summary Thymeleaf is a powerful view engine
You can work with variables and helper objects You can create loops and conditional statements You can easily create forms The Spring controllers have built-in IoC container You can create routings on actions and controllers You have access to the HttpRequest, HttpResponse, HttpSession, Cookies and others You can redirect between actions

41 Spring Essentials © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

42 License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

43 Trainings @ Software University (SoftUni)
Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University Foundation softuni.org Software Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.


Download ppt "Thymeleaf and Spring Controllers"

Similar presentations


Ads by Google