Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Jakarta EE Cookbook
  • Table Of Contents Toc
Jakarta EE Cookbook

Jakarta EE Cookbook - Second Edition

By : Moraes
5 (2)
close
close
Jakarta EE Cookbook

Jakarta EE Cookbook

5 (2)
By: Moraes

Overview of this book

Jakarta EE is widely used around the world for developing enterprise applications for a variety of domains. With this book, Java professionals will be able to enhance their skills to deliver powerful enterprise solutions using practical recipes. This second edition of the Jakarta EE Cookbook takes you through the improvements introduced in its latest version and helps you get hands-on with its significant APIs and features used for server-side development. You'll use Jakarta EE for creating RESTful web services and web applications with the JAX-RS, JSON-P, and JSON-B APIs and learn how you can improve the security of your enterprise solutions. Not only will you learn how to use the most important servers on the market, but you'll also learn to make the best of what they have to offer for your project. From an architectural point of view, this Jakarta book covers microservices, cloud computing, and containers. It allows you to explore all the tools for building reactive applications using Jakarta EE and core Java features such as lambdas. Finally, you'll discover how professionals can improve their projects by engaging with and contributing to the community. By the end of this book, you'll have become proficient in developing and deploying enterprise applications using Jakarta EE.
Table of Contents (14 chapters)
close
close

Using Jakarta Bean Validation for data validation

You can use Bean Validation to constrain your data in many different ways. In this recipe, we are going to use it to validate a JSF form so that we can validate it as soon as the user tries to submit it and avoid any invalid data right away.

Getting ready

First, we need to add our dependencies:

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>

How to do it...

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

  1. Let's create a User object that will be attached to our JSF page:
@Named
@RequestScoped
public class User {

@NotBlank (message = "Name should not be blank")
@Size (min = 4, max = 10,message = "Name should be between
4 and 10 characters")
private String name;

@Email (message = "Invalid e-mail format")
@NotBlank (message = "E-mail should not be blank")
private String email;

@PastOrPresent (message = "Created date should be
past or present")
@NotNull (message = "Create date should not be null")
private LocalDate created;

@Future (message = "Expires should be a future date")
@NotNull (message = "Expires should not be null")
private LocalDate expires;


//DO NOT FORGET TO IMPLEMENT THE GETTERS AND SETTERS

...
  1. Now, we need to define the method that will be fired once all the data is valid:
    public void valid(){
FacesContext
.getCurrentInstance()
.addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Your data is valid", ""));
}
  1. Now, our JSF page references each User class field that's been declared, as follows:
<h:body>
<h:form>
<h:outputLabel for="name" value="Name" />
<h:inputText id="name" value="#{user.name}" />
<br/>
<h:outputLabel for="email" value="E-mail" />
<h:inputText id="email" value="#{user.email}" />
<br/>
<h:outputLabel for="created" value="Created" />
<h:inputText id="created" value="#{user.created}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>
<br/>
<h:outputLabel for="expire" value="Expire" />
<h:inputText id="expire" value="#{user.expires}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>
<br/>
<h:commandButton value="submit" type="submit" action="#{user.valid()}" />
</h:form>
</h:body>

If you run this code, all the fields will be validated once you click the Submit button. Try it for yourself!

How it works...

Let's check each declared constraint:

    @NotBlank (message = "Name should not be blank")
@Size (min = 4, max = 10,message = "Name should be between
4 and 10 characters")
private String name;

The @NotBlank annotation will deny not only null values, but also whitespace values, and @Size speaks for itself:

    @Email (message = "Invalid e-mail format")
@NotBlank (message = "E-mail shoud not be blank")
private String email;

The @Email constraint will check the email string format:

    @PastOrPresent (message = "Created date should be past or present")
@NotNull (message = "Create date should not be null")
private LocalDate created;

@PastOrPresent will constrain LocalDate to be in the past or until the present date. It can't be in the future.

Here, we can't use @NotBlank as there is no blank date, only null, so we avoid this by using @NotNull:

    @Future (message = "Expires should be a future date")
@NotNull (message = "Expires should not be null")
private LocalDate expires;

This is the same as the preceding constraint, but constraints for a future date.

In our UI, there are two places we should take a careful look at:

 <h:inputText id="created" value="#{user.created}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>

...

<h:inputText id="expire" value="#{user.expires}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>

Here, we are using convertDateTime to automatically convert the data that's inputted into inputText according to the dd/MM/uuuu pattern.

See also

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Jakarta EE Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon