THE DATA BINDER IN SPRING MVC

spring

Before analyzing the binder via the input form, let us again make some important considerations. We again introduce what is the fundamental pattern of Spring MVC.

Schema spring mvc

We have seen how the different layers that make up its architecture allow us to get information compatible with our browsers, starting with the data within the DBMS.Then going through the persistence and service layer where if necessary, being the Business Logic layer, they undergo changes. We then get to the presentation layer which thanks to the Controller is exchanging this data in a still raw format with the View Resolver via model, finally getting to the JSP page which formats the data passes it to the Dispacher Servlet and the latter provides the response to the client. So far we have seen how this is possible by going through all the layers, read and filter data from the DBMS, filters created through Store Procedures and a powerful mechanism such as lambdas, which I remind you are triggered using the Stream() statement.

COMPONENTS USED IN THE PROJECT

In the project I used the latest version of Bootstrap available, which at the time of writing is 5.3 and fontawesome which both allow you to create pleasing user interfaces. If you want to learn more about Bootstrap 5 I recommend you read my posts on the subject. As mentioned, so far we have dealt with read and filter and data, in this section we will focus on the other three basic operations, namely creation, update and delete. I will tell you specifically about data entry, as you can see by downloading the sources the update and deletion do not introduce anything new.

NB: Before downloading the projects or rather the project of your interest at the end section of this article, make sure you have updated the Store Procedures posted in the previous post at
https://marcoalbasini.it/2023/12/spring-mvc-il-progetto-per-mysql/
The SQL code can be found at the end of this section.

CREATION OF THE ARTICLE DATA ENTRY VIEW

Let’s add a new view, so as a first step let’s update our tiles.xml file.

Tiles.xml

Our Controller will need to return the name insArticolo that extends the baseLayout. The JSP file we will create will be insarticolo.jsp. I will bring you an excerpt from this file, it is essential to include the following Tag Library

<%@ taglib prefix=“form” uri=“http://www.springframework.org/tags/form”%>

that allows us to use Spring forms, which, as we will see, are critical; in fact, they allow us to use what is commonly called the Data Binder, which is basically the ability of our forms to collect information in the various input fields and transfer it to the Controller.

form

In the jsp page excerpt the Tag Library I showed you earlier is used using the POST method since we are sending data to the server. Then there is the most important element, the modelAttribute which will be the name of the class that will contain our data, in our case this class is the Articoli domain class. Currently each control has a label hardwired into the code, this is incorrect and we will see how this can be avoided to encourage internalization.

CREATE GET METHOD IN THE CONTROLLER

When we talk about submitting form data to our Web Server to enter data we need to understand the steps involved in this operation. In the first phase, the user makes a request to the Web Server in order to obtain an empty form ready for filling out, using the http protocol and the GET method. Once we have entered the data we come to step number two which, via http protocol but this time in POST, sends the data to the server to be saved.

Fasi di un form

Between the first and second steps there may be intermediate steps such as validation of the data we have entered, which we will deal with shortly. Let us start analyzing the first phase through the Controller.

Controller method get

Instead of using the @RequestMapping notation by specifying the path and GET method, we can use the equivalent @GetMapping notation, which, as the name implies, handles the form’s http GET method. Let’s analyze the code. We create an empty article class that we will pass to the view to be filled during the POST phase. We then have to worry about loading the data to be displayed in the form from two tables. The commodity category table and vat table. To do this, I will not go into the explanation, the process is always the same, you create the domain classes, the appropriate interfaces and their implementations in the repository layer and the service layer, doing the appropriate Code Injection as we have seen done before.

PASS DATA FROM THE CONTROLLER TO THE VIEW

This data is pulled into the controller through the service layer to populate the two lists. We then pass the various data to the view via the model. One important thing I’ll point out is the last line before the return. insArticolo is the same name we gave at the jsp page level in the modelAttribute, this is critical because by doing so we are passing the article class to our form to then be populated with the data binder mechanism. Now let’s go to the JSP page to see how the two lists are used to populate the respective controls.

Tag Select

As you can see, the combo boxes (select controls) are populated with the data we passed through the model in items.

POST METHOD CREATION IN THE CONTROLLER

Let us now deal with the second step, we have filled out the form and now we have to submit it to our Web Server. You may remember that on the DBMS side we have created special Store Procedures, one of them is in charge of inserting or updating data in the ARTICOLI table, the discriminating factor is the CodArt, if it is not present we will proceed with the insertion, otherwise with the update. It is important to understand the mechanism of data binder, that is, of Spring MVC’s ability to collect the data compiled in the form, value a class, which in our case will be the Articoli class, and pass that class into the various layers to then derive the information with getter methods and insert the information into the database. We create a new method in the Controller, I report the code.

Controller Post method

About the BindingResult we will deal with it later, let us focus our attention on the @ModelAttribute notation its insArticolo parameter we already know. Importantly, this parameter must be the same as the one provided in the method that handles the GET phase. In particular when via model we return the empty article class to the view the parameter is always insArticolo. In addition, the same insArticolo must be present at the view level when we specify the methodAttribute. The @PostMapping notation tells Spring MVC that we are in the POST phase, obviously the URL must remain the same as seen in the GET phase. Through the @ModelAttribute we are getting an Articoli class named articolo valued at POST.

THE PATH

We then use the service layer whose Code Injection we did to pass our enhanced class to the InsArticolo method. Eventually with a redirect we return to the item list by passing the CodArt to the filter so that we have a single row checking whether the item has been entered. The empty articolo class passed to Spring MVC in the GET method is filled via the form-level path where the private fields of our articolo class are specified. With the path we say that our control should enter the filled-in data on the private field of the articolo domain class having the same name. It is essential to abide by this convention.

USE OF @INITBINDER NOTATION

The @InitBinder allows us to create a mapping of those fields that are authorized to the Data Binder, or we explicitly declare those fields excluded from the Data Binder. We have a two-pronged approach of either allowing all fields allowed in the data binder mechanism or explicitly denying which fields are not allowed. It is in the method that deals with the POST phase that we are then going to check for violations of the binding mechanism.

Init Binder

DOWNLOAD ARTICLE CODE

AlphaShopV2

AlphaShopV3

The AlphaShopV2.zipper project is for the SQL Server DBMS while AlphaShopV3.zipper is for MySQL.

THE JAVA LANGUAGE

THE JAVA LANGUAGE

LINKS TO PREVIOUS POSTS

SPRING FRAMEWORK