Mastering Spring 5.0
eBook - ePub

Mastering Spring 5.0

Ranga Rao Karanam

Condividi libro
  1. 496 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Mastering Spring 5.0

Ranga Rao Karanam

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

Develop cloud native applications with microservices using Spring Boot, Spring Cloud, and Spring Cloud Data FlowAbout This Book• Explore the new features and components in Spring• Evolve towards micro services and cloud native applications• Gain powerful insights into advanced concepts of Spring and Spring Boot to develop applications more effectively• Understand the basics of Kotlin and use it to develop a quick service with Spring BootWho This Book Is ForThis book is for an experienced Java developer who knows the basics of Spring, and wants to learn how to use Spring Boot to build applications and deploy them to the cloud.What You Will Learn• Explore the new features in Spring Framework 5.0• Build microservices with Spring Boot• Get to know the advanced features of Spring Boot in order to effectively develop and monitor applications• Use Spring Cloud to deploy and manage applications on the Cloud• Understand Spring Data and Spring Cloud Data Flow• Understand the basics of reactive programming• Get to know the best practices when developing applications with the Spring Framework• Create a new project using Kotlin and implement a couple of basic services with unit and integration testingIn DetailSpring 5.0 is due to arrive with a myriad of new and exciting features that will change the way we've used the framework so far. This book will show you this evolution—from solving the problems of testable applications to building distributed applications on the cloud.The book begins with an insight into the new features in Spring 5.0 and shows you how to build an application using Spring MVC. You will realize how application architectures have evolved from monoliths to those built around microservices. You will then get a thorough understanding of how to build and extend microservices using Spring Boot. You will also understand how to build and deploy Cloud-Native microservices with Spring Cloud. The advanced features of Spring Boot will be illustrated through powerful examples. We will be introduced to a JVM language that's quickly gaining popularity - Kotlin. Also, we will discuss how to set up a Kotlin project in Eclipse.By the end of the book, you will be equipped with the knowledge and best practices required to develop microservices with the Spring Framework.Style and ApproachThis book follows an end-to-end tutorial approach with lots of examples and sample applications, covering the major building blocks of the Spring framework.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Mastering Spring 5.0 è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Mastering Spring 5.0 di Ranga Rao Karanam in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Ciencia de la computación e Programación en Java. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2017
ISBN
9781787122338

Building a Web Application with Spring MVC

Spring MVC is the most popular web framework used to develop Java web applications. The beauty of Spring MVC lies in its clean, loosely coupled architecture. With a clean definition of roles for controllers, handler mappings, view resolvers, and Plain Old Java Object (POJO) command beans, Spring MVC makes use of all the core Spring features--like dependency injection and autowiring--to make it simple to create web applications. With its support for multiple view technologies, it is extensible too.
While Spring MVC can be used to create REST services, we discuss that in Chapter 5, Building Microservices with Spring Boot.
In this chapter, we will focus on reviewing the basics of Spring MVC with simple examples.
In this chapter will cover the following topics:
  • The Spring MVC architecture
  • The roles played by DispatcherServlet, view resolvers, handler mappings and controllers
  • Model attributes and session attributes
  • Form binding and validation
  • Integration with Bootstrap
  • Basics of Spring Security
  • Writing simple unit tests for controllers

Java web application architecture

The way we develop Java web applications has evolved during the last couple of decades. We will discuss the different architectural approaches to developing Java web applications and see where Spring MVC fits in:
  • Model 1 architecture
  • Model 2 or MVC architecture
  • Model 2 with Front Controller

Model 1 architecture

Model 1 architecture is one of the initial architecture styles used to develop Java-based web applications. A few important details are as follows:
  • JSP pages directly handled the requests from the browser
  • JSP pages made use of the model containing simple Java beans
  • In some applications of this architecture style, JSPs even performed queries to the database
  • JSPs also handled the flow logic: which page to show next
The following picture represents typical Model 1 architecture:
There are a lot of disadvantages in this approach, leading to quick shelving and the evolution of other architectures. A few important disadvantages are listed as follows:
  • Hardly any separation of concerns: JSPs were responsible for retrieving data, displaying data, deciding which pages to show next (flow), and sometimes, even business logic as well
  • Complex JSPs: Because JSPs handled a lot of logic, they were huge and difficult to maintain

Model 2 architecture

Model 2 architecture came in to solve the complexity involved with complex JSPs having multiple responsibilities. This forms the base for the MVC architecture style. The following image represents typical Model 2 architecture:
Model 2 architecture has a clear separation of roles between Model, View, and Controller. This leads to more maintainable applications. A few important details are as follows:
  • Model: Represents the data to be used to generate a View.
  • View: Uses the Model to render the screen.
  • Controller: Controls the flow. Gets the request from the browser, populates the Model and redirects to the View. Examples are Servlet1 and Servlet2 in the preceding figure.

Model 2 Front Controller architecture

In the basic version of Model 2 architecture, the requests from the browser are handled directly by different servlets (or Controllers). In a number of business scenarios, one would want to do a few common things in servlets before we handle the request. An example would be to ensure that the logged-in user has the right authorization to execute the request. This is a common functionality that you would not want to be implemented in every servlet.
In Model 2 Front Controller architecture, all requests flow into a single controller called the Front Controller.
Picture below represents typical Model 2 Front Controller architecture:
The following are some of the responsibilities of a typical Front Controller:
  • It decides which Controller executes the request
  • It decides which View to render
  • It provides provisions to add more common functionality
  • Spring MVC uses an MVC pattern with Front Controller. The Front Controller is called DispatcherServlet. We will discuss DispatcherServlet a little later.

Basic flows

Spring MVC uses a modified version of the Model 2 Front Controller architecture. Before we go into details about how Spring MVC works, we will focus on creating a few simple web flows using Spring MVC. In this section, we will create six typical web application flows using Spring MVC. The flows are listed as follows:
  • Flow 1: Controller without a View; serving content on its own
  • Flow 2: Controller with a View (a JSP)
  • Flow 3: Controller with a View and using ModelMap
  • Flow 4: Controller with a View and using ModelAndView
  • Flow 5: Controller for a simple form
  • Flow 6: Controller for a simple form with validation
At the end of every flow, we will discuss how to unit test the Controller.

Basic setup

Before we start with the first flow, we would need to get the application set up to use Spring MVC. In the next section, we will start by understanding how to set up Spring MVC in a web application.
We are using Maven to manage our dependencies. The following steps are involved in setting up a simple web application:
  1. Add a dependency for Spring MVC.
  2. Add DispatcherServlet to web.xml.
  3. Create a Spring application context.

Adding dependency for Spring MVC

Let's start with adding the Spring MVC dependency to our pom.xml. The following code shows the dependency to be added in. Since we are using Spring BOM, we do not need to specify the artifact version:
 <dependency> 
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
DispatcherServlet is an implementation of the Front Controller pattern. Any request to Spring MVC will be handled by the Front Controller, that is, DispatcherServlet.

Adding DispatcherServlet to web.xml

To enable this, we would need to add DispatcherServlet to web.xml. Let's look at how to do that:
 <servlet> 
<servlet-name>spring-mvc-dispatcher-servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/user-web-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet...

Indice dei contenuti