The application is created in NetBeans and runs on the Glassfish server which is included with the IDE.

We will start by creating a simple web application:

Once created, we need to add necessary libraries. In Netbeans select the project properties, go to the “Libraries” section and then click on AddLibrary and select the required:

We will also need the web.xml configuration file, so we add to the WEB-INF directory:

We will also need some package in which we will put our classes:

First, to look at how to pass control over Spring calls, we will create a regular servlet. Remember to add information about it to web.xml!

When we look at web.xml after adding the servlet, we will see that the following entry appears in it:

In lines 7-9 we have written that calling the “Sample” subpage will be handled by our new servlet. Some of this entry will be processed. Look at line 9. The entry “/” means that the start page of our application will be serviced by our servlet. If we put the entry “/ *” this would mean that every address call in our application would be servlet supported by that servlet – ie each subaddress, eg http: // localhost: 8080 / SpringMVC / unmanaged.to also be handled. Difference in a star:)

Important information!!

This way of declaring a URL pattern supported by Spring MVC will also cause all static resources to be handled by Spring. This may prevent you from embedding image or PNG files in your application – these will not be supported by any controllers. It is safer to use such a structure:

<Servlet-mapping>

<Servlet-name> springmvc </ servlet-name>

<Url-pattern>. * To </ url-pattern>

</ Servlet-mapping>

We will now make a small change on our server. When we call our application on the screen, the browser should display the text “Halo, here servlet!”.

So launch our application:

We could split our application into modules and handle them by different servlets … or just one module to handle using Spring MVC. I’m working on my web.xml file:

Let’s compare the address of our servlet:

 

Now there will be a slight change. In line 10 to address / jakismodul / added *. This means that every call with the start / jakismodul / will be supported by our sample servlet:

And we check:

 

I hope after these examples that what is happening in new lines 14-21 in the web.xml file will be obvious to you. :)All calls that start with SpringMVC / spring / will be serviced by a servlet named “springmvc”. And this servlet is the DispatcherServlet class which …. we did not define. :)It is here that the control is passed to Spring. This is a Spring bundle class that takes control of calls in our application (at the specified address range). What if we were to declare “/ *” in line 20? All calls in our application would be handled by Spring.

OK, let’s check now what happens when we call the new module in the browser:

There is an error, and when we look at it we will see that the problem is the lack of springmvc-servlet.xml file This is a Spring MVC configuration file in which we will declare Controllers of our application. The file name springmvc-servlet.xml is not accidental. This follows from what we called the servlet for the DispatcherServlet class on line 15 of the web.xml file. If you typed in here instead of springmvc, for example, whereIsNemo, Spring would look for the file WhereName-servlet.xml. Case sensitivity is important.

Since Spring really needs this file, let’s give it to him:

We call it springmvc-servlet.xml:

We will also need some XML namespaces that we will use, so we mark them:

There is no way to select an MVC space so we need to add it manually to the new file. You can also download the file from the sample code, which you will find at the beginning of this chapter and place it in the WEB-INF directory.

Let’s try to access the module:

We look at the server console:

We see that Spring has found our new file (the line with Loading XML bean definitions ….). Let’s look at the last line with a warning. The problem is that we have not specified SpringMVC / spring / SpringMVC / spring / SpringMVC / Spring / SpringMVC-servlet.xml.

So we add a new entry to springmvc-servlet.xml:

Line 21 means that Spring should look for declarations of supported address mappings in the annotations in the package classes (and its subpackages) that we indicated on line 23. :)

In the package pl.jsystems.springmvc.controller we now add the usual class Hello. We introduce the “sayHello” method which prints the text “HELLO MUPPET” on the console. Be sure to add the @Controller entry above the class declarations, and @RequestMapping over the SayHello method.

The @Controller from line 16 declares that this class handles HTTP requests, and @RequestMapping with a parameter indicates which class to do and for which specific request. The / hello.do address is relative and means calling /SpringMVC/spring/hello.do.

Let’s call this address:

Our favorite 404. Does that mean that something did not work? It depends :)“This is not a bug, this is a feature” :) And so seriously – everything according to plan. Let’s take a look at the server console:

 

We have received the message “Hello Muppet”, and this means that our sayHello method has been invoked. Error 404 appears because I do not have a JSP page that I should see in response. Specifically, this file should be named hello.jsp – because the sayHello method returns the “hello” text string and should be directly in the WEB-INF directory, which we will declare in the springmvc-servlet file:

In line 27 I declare where Spring is looking for JSP files, and in line 28 to have the extension. If you want, you can add a subdirectory such as / WEB-INF / jsp / on line 27, or you can separate the directory to jsp files associated with that module eg / WEB-INF / jsp / spring /

Let’s create a hello.jsp file:

and include in it the following code:

Now when we call the /SpringMVC/spring/hello.do address we should see this message:

However, you could do more than display static values. So let’s pass some data from the controller to the view (see line 23):

and then display it on the JSP page (see line 16):

Note that in the JSP I refer to the passed element after the name that I defined in the first parameter of the addAttribute method ie “message”. Effect:

Of course, we can also send objects, lists and we can handle forms, but we will take care of the rest of this course. At this point we have made probably the simplest possible implementation of Spring MVC. Of course, there are many possible variants – such as, in place of an annotation, the use of bean declarations in an XML file.

I will allow myself a small development of the call management topic. Let’s say we create a large application consisting of several modules and we would like to have separate configuration files. The source code for the next examples is at:

We add another package to web.xml:

Since our servlet is called a second, we also add the file “sekodul-servlet.xml”. In it we add such entries as before, except that the JSP files will be in a separate subdirectory in WEB-INF (see line 27), and we will separate a separate package for the controllers of this module. This will allow us to have classes of controllers with the same name.

Of course we create such a package, and in it we put a slightly different class of controller:

Note that here also the mapping for “/hello.do” is specified, but as we remember it is relative address and in this case means “/SpringMVC/drugimodul/hello.do” instead of “/SpringMVC/spring/hello.do” . The method still returns hello, which means that Spring will look for the hello.jsp file to display it, but this time it will look for it in the “WEB-INF / seconddul” directory.

In the WEB-INF directory, we create a subdirectory (as we indicated in the entry in the second-servlet.xml file) and place a jsp file similar to the previous one, except that we will change some of its contents to distinguish it.

Let’s see: