Download Complete (Ebook) Beginning Java MVC 1.0: Model View Controller Development to Build Web, Cloud, and Microservices Applications by Peter Späth ISBN 9781484262795, 9781484262801, 1484262794, 1484262808 PDF for All Chapters
Download Complete (Ebook) Beginning Java MVC 1.0: Model View Controller Development to Build Web, Cloud, and Microservices Applications by Peter Späth ISBN 9781484262795, 9781484262801, 1484262794, 1484262808 PDF for All Chapters
com
DOWLOAD EBOOK
ebooknice.com
ebooknice.com
https://ebooknice.com/product/sat-ii-success-
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018
ebooknice.com
(Ebook) Master SAT II Math 1c and 2c 4th ed (Arco Master
the SAT Subject Test: Math Levels 1 & 2) by Arco ISBN
9780768923049, 0768923042
https://ebooknice.com/product/master-sat-ii-math-1c-and-2c-4th-ed-
arco-master-the-sat-subject-test-math-levels-1-2-2326094
ebooknice.com
ebooknice.com
ebooknice.com
ebooknice.com
Peter Späth
Beginning Java MVC 1.0: Model View Controller Development to Build Web, Cloud,
and Microservices Applications
Peter Späth
Leipzig, Sachsen, Germany
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
Output���������������������������������������������������������������������������������������������������������������������������������� 148
Variables������������������������������������������������������������������������������������������������������������������������������ 149
Loops����������������������������������������������������������������������������������������������������������������������������������� 150
Conditional Branching��������������������������������������������������������������������������������������������������������� 152
Cookies�������������������������������������������������������������������������������������������������������������������������������� 153
The View: Facelets�������������������������������������������������������������������������������������������������������������������� 154
Facelets Files���������������������������������������������������������������������������������������������������������������������� 155
Facelets Configuration��������������������������������������������������������������������������������������������������������� 155
Templating via Facelets������������������������������������������������������������������������������������������������������� 157
The <ui:decorate> Tag�������������������������������������������������������������������������������������������������������� 161
An Example Facelets Project����������������������������������������������������������������������������������������������� 164
Mixing Facelets and JSTL���������������������������������������������������������������������������������������������������� 177
Unified Expressions������������������������������������������������������������������������������������������������������������� 178
The Controller��������������������������������������������������������������������������������������������������������������������������� 179
Controller Basics����������������������������������������������������������������������������������������������������������������� 179
Getting Pages���������������������������������������������������������������������������������������������������������������������� 180
Preparing the Model������������������������������������������������������������������������������������������������������������ 182
Posting Data into Controllers����������������������������������������������������������������������������������������������� 183
Exercises����������������������������������������������������������������������������������������������������������������������������������� 187
Summary���������������������������������������������������������������������������������������������������������������������������������� 187
viii
Table of Contents
ix
Table of Contents
x
Table of Contents
Appendix:�������������������������������������������������������������������������������������������������������������� 393
Solutions to the Exercises��������������������������������������������������������������������������������������������������������� 393
Chapter 1 Exercises������������������������������������������������������������������������������������������������������������� 393
Chapter 2 Exercises������������������������������������������������������������������������������������������������������������� 394
Chapter 3 Exercises������������������������������������������������������������������������������������������������������������� 394
Chapter 4 Exercises������������������������������������������������������������������������������������������������������������� 396
Chapter 5 Exercises������������������������������������������������������������������������������������������������������������� 399
Chapter 6 Exercises������������������������������������������������������������������������������������������������������������� 403
Chapter 7 Exercises������������������������������������������������������������������������������������������������������������� 405
Chapter 8 Exercises������������������������������������������������������������������������������������������������������������� 413
Chapter 9 Exercises������������������������������������������������������������������������������������������������������������� 423
Chapter 10 Exercises����������������������������������������������������������������������������������������������������������� 428
Chapter 11 Exercises����������������������������������������������������������������������������������������������������������� 433
Index��������������������������������������������������������������������������������������������������������������������� 437
xi
About the Author
Peter Späth graduated in 2002 as a physicist and soon afterward became an IT
consultant, mainly for Java-related projects. In 2016, he decided to concentrate on
writing books on various aspects, but with a main focus on software development.
With two books about graphics and sound processing, three books on Android app
development, and a beginner’s book on Jakarta EE development, the author continues
his effort in writing software development-related literature.
xiii
About the Technical Reviewer
Luciano Manelli was born in Taranto, Italy, where he
currently resides with his family. He graduated in Electronic
Engineering at the Polytechnic of Bari at 24 years of age and
then served as an officer in the Navy. In 2012, he earned a
PhD in computer science from the IT department, University
of Bari - Aldo Moro. His PhD focused on grid computing
and formal methods, and he published the results in
international publications. He is a professionally certified
engineer and an innovation manager, and in 2014, he began
working for the Port Network Authority of the Ionian Sea – Port of Taranto, after working
for 13 years for InfoCamere SCpA as a software developer. He has worked mainly in the
design, analysis, and development of large software systems; research and development;
testing; and production with roles of increasing responsibility in several areas over the
years. Luciano has developed a great capability to make decisions in technical and
business contexts and is mainly interested in project management and business process
management. In his current position, he deals with port community systems and digital
innovation.
Additionally, he has written several IT books and is a contract professor at the
Polytechnic of Bari and at the University of Bari - Aldo Moro. You can find out more at his
LinkedIn page: it.linkedin.com/in/lucianomanelli.
xv
Introduction
Starting at the very infancy of software creation, developers tried to modularize their
applications in order to streamline their projects and increase the maintainability of
the software they created. Soon, a very basic segregation scheme was identified: One
part of the software must deal with data and persistence, another part must deal with
presenting the data to the user, and one last part must handle data input and frontend
view propagation.
This segregation scheme showed up in so many projects that it was promoted to a
common software design pattern, called Model-View-Controller, or MVC for short. Its
power also manifested in its versatility, even with big paradigm changes, like the onset of
the Internet age. With database products for the model layer, browsers for the view layer,
and some kind of user input processing for the controller layer, the pattern’s accuracy
and applicability to the majority of software projects became even more apparent with
web applications.
Interestingly, even though most web application frameworks under the hood apply
some kind of MVC layer demarcation, Java Server products up to JEE 7 did not include
a dedicated MVC framework. With JSR-371 (Java Specification Request number 371)
only recently and starting with JEE 8/Jakarta EE 8, an MVC specification entered the Java
Enterprise application realm, which is one of the reasons this book was born. It does
not describe all MVC Frameworks that you can add to Java EE/Jakarta EE as an external
library. There are just too many of them and you can learn about them by looking at
each library’s documentation. Instead, we talk about the genuine Java MVC library as
described by JSR-371.
The target version of Java MVC is 1.0, and we use a Jakarta EE version 8.0 compliant
server to run Java MVC on it.
xvii
Introduction
as this book is not a reference in the sense that all API classes and methods are listed.
Instead, it presents techniques and technologies that help professional Java Enterprise
level developers leverage web application programming by including Java MVC in their
software.
The book uses the Linux operating system as the development platform, although
the code can be run on other platforms (Windows and macOS) without complex
adaptions. This book also does not talk about hardware issues (in case you don’t use a
laptop, a PC, or a server).
The readers will in the end be able to develop and run Java MVC programs of mid- to
high-level complexity.
S
ources
All sources shown or referred to in this book can be accessed via the Download Source
Code button located at www.apress.com/9781484262795.
xviii
CHAPTER 1
• Controller: Handles user input and prepares the data set necessary
for the view part to do its work. While a view shows model items, the
view never has to know how data is stored and retrieved from some
persistent storage (database). This is the controller’s responsibility.
Because the user input determines what an application has to do next,
the controller also contains the application logic. Any calculation and
data transformation happens in the control part of MVC.
1
© Peter Späth 2021
P. Späth, Beginning Java MVC 1.0, https://doi.org/10.1007/978-1-4842-6280-1_1
Chapter 1 About MVC: Model, View, Controller
For example, consider a book club application. In this case, the model consists of
elements such as books (including rental status), book storage location (building, room,
or shelf ), and member. For search application modules, you normally define lists of
books, users, and so on, as model values.
The view part of the book club application will contain pages that show books, show
members, show book locations, enable members to rent books, add club members, show
book and member lists, as well as various search functionalities, and so on. Technically,
this will often go hand in hand with a templating engine that defines placeholders for
model elements, shortcuts for loops (for tables and lists), and other view elements like
menus and buttons.
The controller handles the data the user enters. If, for example, the view currently
shows a search page for books and the user enters a book’s name and clicks on the
Search button, the controller is informed as to which button was clicked. The controller
then reads the request parameters (the book’s name in this case) and possibly some
model values (for example, the username and whether the user is logged in), queries
the database, builds a result list, creates a model from this list, and finally decides which
view page to show next.
There exists some fluffiness concerning the implementation details. This comes
from the technical details of the data flow between view elements and model elements.
MVC makes no assumption about when updates to view elements and model elements
actually happen and which procedure is chosen to keep them synchronized. This is why,
for MVC, you find many different diagrams in the literature.
For Java MVC, we can narrow our ideas about MVC to the following—a model
(stored in memory) defines the application’s state; a view shows model values and sends
user interactions to a controller; and the controller prepares model data, handles user
input and accordingly changes model values, and then decides which view page to show
next. This kind of MVC model is depicted in Figure 1-1.
2
Chapter 1 About MVC: Model, View, Controller
3
Chapter 1 About MVC: Model, View, Controller
The real power of MVC was revealed in the 1990s with the rise of the Internet.
Although some technical details changed—such as the exact technical characteristics of
the data flow and the point in time when data traverses the layer boundaries—the idea
remained the same: a model holds the application state, a view presents the browser
pages, and a controller handles the interaction between the browser and the model, and
decides which view page to show.
Various MVC web frameworks were invented; https://en.wikipedia.org/wiki/
Comparison\_of\_web\_frameworks shows you a comprehensive list (further down on
the page, MVC capabilities are also listed).
4
Chapter 1 About MVC: Model, View, Controller
• Updating the view: With web applications, the way a view is updated
is crucial. Either the complete page is loaded after the controller
works a request, or only those parts of a web page that actually need
an update are transmitted from the server to the browser. Again, the
latter method reduces network traffic.
From these points, you can see that programming a MVC framework for web
applications is not an utterly trivial task. This is also why there are quite a large number
of different MVC frameworks you can use for web applications. In the rest of the book, I
will show you why choosing Java MVC is not the worst thing you can do if you need MVC
software for your Java platform.
5
Chapter 1 About MVC: Model, View, Controller
M
VC for Java
In the Java ecosystem, a framework named Struts entered the software world around
2000. It is a MVC framework aimed at web applications and integrating with Java EE/
Jakarta EE and Tomcat (a server product boiled down to web functionalities). It has been
used in many software projects and is still being used, albeit it is not part of the Java EE/
Jakarta EE specification. Instead, Java EE/Jakarta EE names JSF (Java Server Faces) as the
dedicated web framework. JSF, in contrast to MVC, uses a component-oriented approach
for creating web applications.
JSF works out-of-the-box for any Java EE/Jakarta EE 8 or later product. Up to version
7, if you wanted to use MVC, Struts was one of the prominent frameworks you could
use. However, in order for Struts to work, an external library had to be added to the
application, and Struts always felt like an extension and not so much like something that
seamlessly integrated with Java EE/Jakarta EE.
With Java EE 8/Jakarta EE 8, the MVC world reentered the game in form of a Java
MVC specification. It is still kind of a second-class citizen in the Java EE/Jakarta EE
world, but there are reasons to favor MVC over JSF. We talk about the merits and
disadvantages of MVC over other frameworks like JSF at the end of this chapter.
Note We use Eclipse Krazo as the Java MVC implementation library. See
https://projects.eclipse.org/proposals/eclipse-krazo
or
https://projects.eclipse.org/projects/ee4j.krazo
We will later see how to install Eclipse Krazo for your web application.
6
Chapter 1 About MVC: Model, View, Controller
Java MVC is a lean and clever extension of the REST technology JAX-RS included
within Java EE/Jakarta EE. This relationship gives Java MVC a modern touch and allows
for a concise and highly comprehensive programming style.
We already learned that MVC allows for some fluffiness concerning the
implementation details. Figure 1-1 describes how Java MVC works quite well: A request
for a first page in the browser window routes to the controller, which prepares model
values (with or without querying some backend for additional data). The controller
then decides which view page (browser page) to show next (maybe a login page). The
view can access model values. With a data set entered by the user and submitted to
the controller, the controller takes request parameters (for example, the login name
and password), possibly queries the backend (the user database), updates the model,
and finally selects a new view page (for example, a welcome page after successful
authentication).
But there is an additional feature that seamlessly integrates with Java MVC. Instead
of always loading a complete new page after each HTTP request, you can decide to
let parts of your web application use AJAX for more fine-grained frontend-backend
communication. Because we use Java MVC in a Java EE/Jakarta EE 8 (or later)
environment, we can use JAX-RS for that aim out-of-the-box.
W
hy MVC
With so many web frontend technologies out there, it is not easy to decide which to use
for your project. The new Java MVC certainly is an option and it might very well suit your
needs. In order to help you make a decision, here is a list of pros and cons of Java MVC.
Cons:
7
Chapter 1 About MVC: Model, View, Controller
Pros:
• Write a short program that does something simple, like output the
string "Hello World".
• Build a deployable artifact from the string (for example, a .war file).
8
Chapter 1 About MVC: Model, View, Controller
REM Windows:
REM Note, if the OPENJDK8_DIR contains spaces, wrap it
REM inside "..."
set AS_JAVA=OPENJDK8_DIR
# Linux:
AS_JAVA="OPENJDK8_DIR"
You must replace OPENJDK8_DIR with the installation folder of the OpenJDK 8 installation.
REM Windows:
chdir GLASSFISH_INST_DIR
bin\asadmin start-domain
9
Chapter 1 About MVC: Model, View, Controller
# Linux:
cd GLASSFISH_INST_DIR
bin/asadmin start-domain
build
|- <empty>
src
|- java
| |- book
| |- javamvc
| |- helloworld
| |- App.java
| |- RootRedirector.java
| |- HelloWorldController.java
|- webapp
| |- META-INF
| | |- MANIFEST.MF
| |- WEB-INF
| |- lib
| | |- activation-1.1.jar
| | |- javaee-api-8.0.jar
| | |- javax.mail-1.6.0.jar
| | |- javax.mvc-api-1.0.0.jar
| | |- jstl-1.2.jar
| | |- krazo-core-1.1.0-M1.jar
| | |- krazo-jersey-1.1.0-M1.jar
| |- views
| | |- greeting.jsp
| | |- index.jsp
| |- beans.xml
10
Chapter 1 About MVC: Model, View, Controller
| |- glassfish-web.xml
make.bat
make.sh
// App.java:
package book.javamvc.helloworld;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/mvc")
public class App extends Application {
}
// RootRedirector.java
package book.javamvc.helloworld;
import javax.servlet.FilterChain;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Redirecting http://localhost:8080/HelloWorld/
* This way we don't need a <welcome-file-list> in web.xml
*/
@WebFilter(urlPatterns = "/")
public class RootRedirector extends HttpFilter {
@Override
protected void doFilter(HttpServletRequest req,
HttpServletResponse res,
11
Chapter 1 About MVC: Model, View, Controller
// HelloWorldController.java
package book.javamvc.helloworld;
import javax.inject.Inject;
import javax.mvc.Controller;
import javax.mvc.Models;
import javax.mvc.binding.MvcBinding;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("/hello")
@Controller
public class HelloWorldController {
@Inject
private Models models;
@GET
public String showIndex() {
return "index.jsp";
}
@POST
@Path("/greet")
public Response greeting(@MvcBinding @FormParam("name")
String name) {
models.put("name", name);
return Response.ok("greeting.jsp").build();
}
}
12
Chapter 1 About MVC: Model, View, Controller
Manifest-Version: 1.0
13
Chapter 1 About MVC: Model, View, Controller
<body>
Hello ${name}
</body>
</html>
10. As beans.xml, create an empty file (the file must exist, though!).
#!/bin/bash
JAVA_HOME=/path/to/your/openjdk-8
$JAVA_HOME/bin/javac \
-cp src/webapp/WEB-INF/lib/javaee-api-8.0.jar:
src/webapp/WEB-INF/lib/javax.mvc-api-1.0.0.jar \
-d build/WEB-INF/classes \
src/java/book/javamvc/helloworld/*
cd build
$JAVA_HOME/bin/jar cf ../HelloWorld.war *
cd ..
14
Exploring the Variety of Random
Documents with Different Content
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com