Spring Web Development
Objectives
◆ Building web applications using the Spring Framework.
Spring MVC (Model-View-Controller)
Spring WebFlux
Spring Boot
RESTful Web Services
Security
Integration with other technologies
◆ Understand a modern server-side Java template engine for both web and
standalone environments, capable of processing HTML, XML, JavaScript, CSS
and even plain text (Thymeleaf)
2
Spring Web Development Introduction
◆ The Spring Framework offers a robust and flexible platform for developing web
applications, with comprehensive support for integration, scalability, security,
and testing.
◆ With Spring Framework, a strong community support and alignment with
modern architectural patterns make it a compelling choice for building modern
and reliable web applications.
3
Spring Web Development Introduction
◆ Spring provides a comprehensive and flexible platform for building web
applications, whether you're using traditional MVC patterns or adopting newer
reactive programming paradigms.
Spring MVC (Model-View-Controller)
Spring WebFlux
Spring Boot
RESTful Web Services
Security
Integration with other technologies
Testing
4
Spring MVC (Model-View-Controller)
◆ Spring MVC is the traditional approach to
building web applications with Spring.
◆ It provides a robust architecture for
organizing your web application into
components such as controllers, views,
and models.
◆ Controllers handle incoming requests,
models represent the data, and views
render the response.
5
Spring WebFlux
◆ Spring WebFlux is the reactive approach introduced in Spring 5.
◆ It's based on Project Reactor and the Reactive Streams specification.
◆ WebFlux allows you to build non-blocking, reactive web applications,
which are particularly useful for handling high concurrency and
streaming data scenarios.
◆ The reactive-stack web framework, Spring WebFlux is fully non-
blocking, supports Reactive Streams ([Link]
[Link]/) back pressure, and runs on such servers as Netty,
Undertow, and Servlet containers.
6
Spring Boot
◆ While not exclusively for web development, Spring Boot greatly simplifies the
process of creating stand-alone, production-grade Spring-based applications.
◆ Consider using Spring Boot to quickly bootstrap your Spring applications.
◆ Spring Boot offers a convention-over-configuration approach, simplifying the
setup and configuration of Spring-based applications.
◆ Spring Boot is an excellent choice for rapidly developing web applications with
minimal setup and boilerplate code.
7
Spring Security
◆ Security: Spring Security is the de facto standard for securing Spring-based
applications.
◆ It provides comprehensive security features for web applications, including
authentication, authorization, session management, and more.
◆ It supports both traditional server-rendered applications (like Spring MVC) and
modern single-page applications (like those built with Angular or React).
8
Spring REST
◆ If your web applications require RESTful services, consider using Spring's
support for building REST APIs.
◆ Spring's REST capabilities, combined with Spring MVC or Spring WebFlux,
provide powerful tools for creating and consuming RESTful services.
◆ Spring MVC and Spring WebFlux can both be used to create RESTful services,
with features like content negotiation, request mapping, and input validation.
9
Integration with other technologies
◆ Spring integrates seamlessly with other technologies commonly used in web
development, such as databases (via Spring Data), messaging systems (via
Spring Messaging), and frontend frameworks (via RESTful APIs or WebSocket
support).
10
Spring Data
◆ For data access, Spring Data offers a range of options, including JPA,
MongoDB, Redis, and more.
◆ Choose the appropriate Spring Data module based on your data storage
requirements to simplify data access and manipulation within your web
applications.
11
Templating engines
◆ When it comes to templating engines, consider using Thymeleaf or Freemaker.
◆ Both provide powerful template processing capabilities, enabling the seamless
integration of dynamic content into web pages.
◆ Thymeleaf, in particular, is a popular choice for Spring-based web applications
due to its natural integration with Spring MVC.
12
Thymeleaf - a Java-based server side
template engine
What is Thymeleaf?
◆ Thymeleaf is a modern server-side Java template engine for both web and
standalone environments, capable of processing HTML, XML, JavaScript, CSS
and even plain text.
◆ The main goal of Thymeleaf is to provide an elegant and highly-maintainable
way of creating templates.
◆ Thymeleaf improves communication of design and bridges the gap between
design and development teams.
◆ Thymeleaf has also been designed from the beginning with Web Standards in
mind - especially HTML5 - allowing you to create fully validating templates if
◆ that is a need for you
14
Kinds of templates in Thymeleaf
◆ There are two markup template modes (HTML and XML), three textual
template modes (TEXT , JAVASCRIPT and CSS) and a no-op template mode
(RAW).
◆ The HTML template mode will allow any kind of HTML input, including HTML5,
HTML 4 and XHTML. No validation or well-formedness check will be
performed, and template code/structure will be respected to the biggest
possible extent in output.
◆ The XML template mode will allow XML input. In this case, code is expected to
be well-formed – no unclosed tags, no unquoted attributes, etc – and the parser
will throw exceptions if well-formedness violations are found. Note that no
validation (against a DTD or XML Schema) will be performed.
15
Kinds of templates in Thymeleaf
◆ The TEXT template mode will allow the use of a special syntax for templates of
a non-markup nature. Examples of such templates might be text emails or
templated documentation. Note that HTML or XML templates can be also
processed as TEXT , in which case they will not be parsed as markup, and
every tag, DOCTYPE, comment, etc, will be treated as mere text.
◆ The JAVASCRIPT template mode will allow the processing of JavaScript files
in a Thymeleaf application. This means being able to use model data inside
JavaScript files in the same way it can be done in HTML files, but with
JavaScript-specific integrations such as specialized escaping or natural
scripting. The JAVASCRIPT template mode is considered a textual mode and
therefore uses the same special syntax as the TEXT template mode.
16
Kinds of templates in Thymeleaf
◆ The CSS template mode will allow the processing of CSS files involved in a
Thymeleaf application. Similar to the JAVASCRIPT mode, the CSS template
mode is also a textual mode and uses the special processing syntax from the
TEXT template mode.
◆ The RAW template mode will simply not process templates at all. It is meant to
be used for inserting untouched resources (files, URL responses, etc.) into the
templates being processed. For example, external, uncontrolled resources in
HTML format could be included into application templates, safely knowing that
any Thymeleaf code that these resources might include will not be executed.
17
Install Thymeleaf Plugin for Eclipse
18
Standard Expression Syntax
◆ Simple expressions:
Variable Expressions: ${...}
Selection Variable Expressions: *{...}
Message Expressions: #{...}
Link URL Expressions: @{...}
Fragment Expressions: ~{...}
◆ Literals
Text literals: 'one text' , 'Another one!' ,…
Number literals: 0 , 34 , 3.0 , 12.3 ,…
Boolean literals: true , false
Null literal: null
Literal tokens: one , sometext , main ,…
19
Standard Expression Syntax
◆ Text operations:
String concatenation: +
Literal substitutions: |The name is ${name}|
◆ Arithmetic operations:
Binary operators: + , - , * , / , %
Minus sign (unary operator): -
◆ Boolean operations:
Binary operators: and , or
Boolean negation (unary operator): ! , not
20
Standard Expression Syntax
◆ Comparisons and equality:
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
◆ Conditional operators:
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
◆ Default: (value) ?: (defaultvalue)
◆ Special tokens:
No-Operation: _
21
HTML Template Example
22
Attribute Precedence
23
Create a simple project using Thymeleaf
24
Controller
25
The Standard Dialect
◆ Thymeleaf is an extremely extensible template engine (in fact it could be called
a template engine framework) that allows you to define and customize the way
your templates will be processed to a fine level of detail.
◆ An object that applies some logic to a markup artifact (a tag, some text, a
comment, or a mere placeholder if templates are not markup) is called a
processor, and a set of these processors – plus perhaps some extra artifacts –
is what a dialect is normally comprised of.
◆ Out of the box, Thymeleaf’s core library provides a dialect called the Standard
Dialect, which should be enough for most users.
26
The Standard Dialect
27
Attribute Tag Processor
28
Spring Web Configuration
29
Spring Web Configuration
30
Spring Web Configuration
31
Run Project
32
Example: Spring Thyme Seedstarter Manager
33
Example:The Good Thymes Virtual Grocery
34
Summary
Concepts were introduced:
◆ Building web applications using the Spring Framework.
Spring MVC (Model-View-Controller)
Spring WebFlux
Spring Boot
RESTful Web Services
Security, Integration with other technologies
◆ Amodern server-side Java template engine for both web and standalone
environments, capable of processing HTML, XML, JavaScript, CSS and even
plain text (Thymeleaf)
35