CREATION OF ARTICLE CONTROLLER CLASS

spring

The controller handles requests arriving at the Dispacher Servlet. We create a new class ArticoliController.java, which will handle all calls involving articoli. We insert the @Controller notation critical to the IOC and to the Dispacher Servlet that when it goes to look for the appropriate controllers, it will know that this is a Controller class.

Schema Web App

THE @REQUESTMAPPING NOTATION

The notation @RequestMapping(“/articoli“) stands for the fact that all calls that will involve articoli should have in the URL the slash /
followed by the word articoli. Let’s look at the code or rather the overload of one of the selection methods.

Selezione Articoli

The GetArticoliByFilter method has three input parameters, the notation @ModelAttribute(“<<name>>) followed by the Articoli domain class, the Model and HttpServletRequest. Let us try to explain these three parameters. @ModelAttribute refers to a property of the Model object (the M in MVC). I’ll show you an excerpt from the articoli JSP page to give you a better understanding of this first parameter.

Form

THE PATH ATTRIBUTE

As you can see, in the form in the modelAttribute attribute the same value has been returned as indicated in the @ModelAttribute notation. The concept is this: by means of a form input field and the path attribute applied to a field of the Articoli domain class (the filter attribute), a binding is made between the filter property of the Articoli class and the input field. In my case this was used to pass the content typed into the input field of type text used as a filter, to the controller. In fact, in the getter of the filter attribute in the controller I can retrieve what the user typed into the search filter on the articles JSP page and pass that value to the Store Procedure, which will do the rest of the work.

THE MODEL

The second parameter is the Model, and is used through the addAttribute method to add information to the model itself that will then be read and formatted in the articoli JSP page. Unused HttpServletRequest is used to retrieve any form fields of the articoli view on which Binding with path has not been done. The rest of the code is commented step by step.

JSP VIEW CREATION ARTICLES

First we go to the tiles configuration XML file and add the articoli view. I repeat that it is critical that the name we assign in the XML file is the same as that returned by the Controller class, in this case articoli.

Tiles

We create within the view folder the articoli JSP page.

JSP articoli

As you can see, we constructed a table having a header, (th tag) and used a forEach to populate it with the content returned by the controller. As for the detail of each item we will see later. Note the fact that when we want to access the model we put a $ in front, so that the notation ${Articoli} ,then stored in the articoli variable, represents the recordset valued in the Controller.

INTRODUCTION TO CONTROLLER @REQUESTPARAM NOTATION

We will see how this notation affects the URL of the http request. We introduce a method having the same name as the previous one and taking three parameters as input. I bring back the code snippet.

Request Param

The IdRep parameter indicates the department, the reference commodity category of our articoli. To the collection we derived with filter, we will apply an additional filter using lambdas. Before applying lambdas, the stream() instruction must be used.

PASS VALUES BETWEEN VIEW AND CONTROLLER WITH @REQUESTPARAM NOTATION

For example, to pass the filter parameter, we just need to set up a form in the JSP page with an input field of type text having as name the name of the parameter, i.e., filter. When the form is submitted, the value typed in the input field will be inserted into the query string and retrieved by the controller in the Filter parameter. IDFAMASS is the ID of the food category. The rest of the code remains unchanged from the method seen previously. With the @RequestParam notation, filters are applied after the question mark.

USE OF @DATETIMEFORMAT NOTATION IN THE CONTROLLER.

We create a new method that will allow us to filter our items by a certain time period. We will use the date of creation. Once again to filter the data by dates we will use lambdas, using the usual Store Procedure for selecting items.

Filtro per data

CREATION OF ARTICLE DETAIL PAGE

The detail of each articolo can be accessed from each row of the product grid from the JSP articoli page. Let’s look first at the controller. I’ll bring back the code.

ControllerDettagio

The controller is passed the Articolo Code, the search must produce the detail of a single articolo. @RequestParams extracts values from the query string while @PathVariables extracts values from the path URI. Basically what we are going to do in the JSP page articoli will be to pass the CodArt to the controller via the syntax I’m about to show you.

JSP articoli dett art

As you see the mapping has been respected and the articolo code passed to the controller with the syntax you see. We need to create a new view, the first thing to do is to configure Apache Tiles. The name must be exactly the same as what the controller returns, otherwise nothing would work.

Tiles

CODE BLOCK WITHIN JSP PAGES

Now we need to create the view dettarticolo.jsp inside the view folder. I’ll bring back the code. I point out how it is possible to insert Java code inside JSP pages, in this case I used the symbols <% for opening and %> for closing the code block.

info articolo

DOWNLOAD ARTICLE CODE

ITEM CODE

Search functionality by date range has been implemented and pagination.

THE JAVA LANGUAGE

THE JAVA LANGUAGE

LINKS TO PREVIOUS POSTS

SPRING FRAMEWORK