Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Jakarta EE Cookbook

You're reading from   Jakarta EE Cookbook Practical recipes for enterprise Java developers to deliver large scale applications with Jakarta EE

Arrow left icon
Product type Paperback
Published in May 2020
Publisher Packt
ISBN-13 9781838642884
Length 380 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Elder Moraes Elder Moraes
Author Profile Icon Elder Moraes
Elder Moraes
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. New Features and Improvements 2. Server-Side Development FREE CHAPTER 3. Building Powerful Services with JSON and RESTful Features 4. Web and Client-Server Communication 5. Security of the Enterprise Architecture 6. Reducing Coding Effort by Relying on Standards 7. Deploying and Managing Applications on Major Jakarta EE Servers 8. Building Lightweight Solutions Using Microservices 9. Using Multithreading on Enterprise Context 10. Using Event-Driven Programming to Build Reactive Applications 11. Rising to the Cloud - Jakarta EE, Containers, and Cloud Computing 12. Other Books You May Enjoy Appendix - The Power of Sharing Knowledge

Running your first MVC 1.0 code

If you have been following Jakarta EE 8 history for a while, you may now be wondering: why is MVC 1.0 here if it was dropped from the Jakarta EE 8 umbrella?

Yes, it is true. MVC 1.0 doesn't belong to the Jakarta EE 8 release. But that doesn't reduce the importance of this great API and I'm sure it will change the way some other APIs work in future releases (for example, JSF).

So, why not cover it here? You will use it anyway.

This recipe will show you how to use a Controller (the C) to inject a Model (the M) into the View (the V). It also brings some Jakarta CDI and JAX-RS to the party.

Getting ready

Add the proper dependencies to your project:

<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>8.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.mvc</groupId>
<artifactId>javax.mvc-api</artifactId>
<version>1.0-pr</version>
</dependency>
</dependencies>

How to do it...

You need to perform the following steps to try this recipe:

  1. Start by creating a root for your JAX-RS endpoints:
@ApplicationPath("webresources")
public class AppConfig extends Application{
}
  1. Create a User class (this will be your Model):
public class User {

private String name;
private String email;

public User(String name, String email) {
this.name = name;
this.email = email;
}

//DON'T FORGET THE GETTERS AND SETTERS
//THIS RECIPE WON'T WORK WITHOUT THEM
}
  1. Now, create a session bean, which will be injected later in your Controller:
@Stateless
public class UserBean {

public User getUser(){
return new User("Elder", "elder@eldermoraes.com");
}
}
  1. Then, create the Controller:
@Controller
@Path("userController")
public class UserController {

@Inject
Models models;

@Inject
UserBean userBean;

@GET
public String user(){
models.put("user", userBean.getUser());
return "/user.jsp";
}
}
  1. And finally, create the web page (the View):
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>User MVC</title>
</head>
<body>
<h1>${user.name}/${user.email}</h1>
</body>
  1. Run it on a Jakarta EE 8 server and access this URL:
http://localhost:8080/ch01-mvc/webresources/userController

How it works...

The main actor in this whole scenario is the Models class injected into the Controller, shown as follows:

@Inject
Models models;

It's a class from MVC 1.0 API that has the responsibility, in this recipe, of letting the User object to be available for the View layer. It's injected (using CDI) and uses another injected bean, userBean, to do it:

models.put("user", userBean.getUser());

So, the View can easily access the values from the User object using expression language as follows:

<h1>${user.name}/${user.email}</h1>

See also

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image