Thymeleaf

Contents

  1. Introduction
    1. Content Organisation
    2. Flow of control
  2. Brief-on-usage
    1. Dependencies
    2. Defining Layout page
    3. Defining content page
    4. Using script in layout-Content page
  3. Resources and references

Contents can be organised on UI as:

  • master-page[layman term] orLayout page[thyme-leaf specific term] which loads other pages on it (header/Footer)
  • Content page

Flow of control

  • Sprinboot(once added with thymeleaf dependecy) looks for src/main/resources/template folder for UI pages
  • the template or the view pages (basically html files in template folder) interact with Controller
  • Use Model to return data to view and in the return statement put the name of view/template to return to.
  • One thing to note about template is that it’s a normal html file but has a extra attribute specific to thyme-leaf to support it’s features. refer below for syntax

Dependencies-

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

<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
    <version>2.5.1</version>
</dependency>




Defining Layout Page

The layout page is similar to any other HTML page. However, you must add the xmlns:layout attribute to the  html tag.

<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

Next, you need to specify the part of the layout page where you want the content to appear. This is done using a div tag with the fragment attribute. So anything outside this div tag would remain fixed while the content of the div tag would be changing. The code below shows how you do this.

<div layout:fragment="content">
    <p>Changing contents</p>
</div>

Defining Content Page

Just like in the layout page, you need to add the xmlns:layout attribute. But additionally, you also must add the layout: decorate attribute as well. The layout:decorate attribute would have a value set to the layout page(without the .html extension).  This is shown below:

<html lang="en" 
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="_layout">

Here, the layout page is _layout.html placed inside the templates folder.

Having added this HTML markup, you then need to specify the div tag that would wrap the whole content of the page. This is done as shown below:

<div layout:fragment="content">

<!--Content of the page-->

</div>

At this point, anything you want in the content page must be inside this div tag include modals.

4. Using Scripts in Layout-Content Page [Confusing – need to see reference resources]

First, note that scripts defined in the layout page, apply to both the layout and the content page. However, you may want a script that applies only to the layout page. This is achieved using the th:block tag.

For instance, we want a script called account.js to execute only for the content page. Then take the steps below:

In the layout page, add the following block,

<th:block layout:fragment="script"></th:block>

In the content page, add

<th:block layout:fragment="script">
    <script th:src="@{/js/accounts.js}"></script>
</th:block>

In this case, the accounts.js script is placed inside the js folder in the static folder.

Quarkus

What is Quarkus? –

It’s a container first (Kubernetes) application. Quarkus tailors your application for GraalVM and HotSpot. Amazingly fast boot time, incredibly low RSS memory (not just heap size!) offering near instant scale up and high density memory utilization in container orchestration platforms like Kubernetes. We use a technique we call compile time boot

Features

  1. It enables reactive
    1. What is reactive and how it enables?

Restful Services

Following topics are covered

  1. PUT vs POST
  2. [System Design]Caching at which layer .. which data to be cached [same data shouldn’t be cached at different layer]
  3. Api Design
    1. data can be send at 3 different part path param/Query Param/Header..what’s the difference?

Resources []Books for referebce:

  1. RESTful Web Services by Leonard Richardson [Best as mentioned]
    1. You will learn what URI is, how HTTP can be used as an application-level protocol, and not just a transport protocol to transfer data between client and server.
    2. Complementry Tutorial https://www.udemy.com/course/restful-web-service-with-spring-boot-jpa-and-mysql/?ranMID=39197&ranEAID=JVFxdTr9V80&ranSiteID=JVFxdTr9V80-JwlMFe5sTRDlo0zYN7GLpg&LSNPUBID=JVFxdTr9V80&utm_source=aff-campaign&utm_medium=udemyads

Other Books

  1. https://www.java67.com/2017/06/top-10-rest-api-and-restful-web-services-book.html
  2. https://medium.com/javarevisited/top-5-books-and-courses-to-learn-restful-web-services-in-java-using-spring-mvc-and-spring-boot-79ec4b351d12 [S[rong Specific]
  3. https://medium.com/javarevisited/top-5-books-to-learn-web-services-in-java-soap-rest-22d92adbefc1

REST Architectural Constraints

  1. https://restfulapi.net/rest-architectural-constraints/
  2. https://www.geeksforgeeks.org/rest-api-architectural-constraints/
  3. https://restfulapi.net/hateoas/

J2EE

Introduction

Java EE is the abstract specifications for developing enterprise applications. These specifications are implemented by the application server vendors like weblogic, websphere.

For example, JSP and Servlet are speciation for developing web applications. Obviously web applications deployed to the server. So, server vendors has to implement the specification.

Where as spring follows some of the specifications from Java EE and override with better options. It is a standalone framework built on top of the Java EE technology.


Servlet filter

https://www.journaldev.com/1933/java-servlet-filter-example-tutorial

Generic Exception Handling in Web-services

Creating Generic Exception class for Web-services:

Step 1. Create a Generic class that extends ResponseEntityExceptionHandler.
Step 2. Annotate the class with @RestController(as it will send Exception response to client) & @ControllerAdvice (to intercept all controller’s exception).

Method Level Changes:
Step 3. Create method(s) signature similar to one in ResponseEntityExceptionHandler to handle Specific Exception . Mark it with annotation @ExceptionHandler(Exception.class) where the parameter is type of exception it will handle.

@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllException(Exception ex, WebRequest request)

{

ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(),
request.getDescription(false));
return new ResponseEntity(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);

}


Step 4. methods return type is ResponseEntity with take body and status as parameter,
body is supplied form ExceptionType being thrown and status-code as per requirement(HttpStatus.CODE_NAME) .

Note : Syntax

ResponseEntity([header],[body],[status-code])

Project Reference:

Artifact – restful-web-services

Package – com.tutorial2implement.webservice.restfulwebservices.application.exception;

Note :

In earlier versions of Spring, extending ResponseEntityExceptionHandler and overriding its methods allowed developers to customize exception handling globally. However, with the release of Spring Framework 6.0, the design has evolved, and some of these methods are now final to promote a more flexible and configurable approach to exception handling.
solution - avoid extending as extends ResponseEntityExceptionHandler and use the method direcctly