01. Introduction to Spring MVC

16 minutes
Share the link to this page
Copied
  Completed

Transcript

In this session, let us get an overview of spring MVC. Let us also understand the MVC architecture and the key components that are responsible for creating a spring MVC application. Spring MVC is used for creating loosely coupled web applications. Till now, whenever you want to create a web application you have used JSP and servlets. Now, with spring It is very easy to create loosely coupled web application. Spring also uses the concept of the MVC model view controller as a model class is nothing but a Java v view can be any type of view framework you can use is simple JSP are going for velocity framework or you can go in for timely depending upon your requirement.

You can choose your view framework, then come see controller When you are working in a JSP servlet applique web application, the servlet was behaving like a controller, wherein your class is tied to the I mean servlet API. In case of spring, the simple Java class behaves like a controller. Because of this, you can create loosely coupled web application. Spring also uses a key design principle, open for extension and closed for modification. You can create classes which extend the inbuilt classes that are available in spring framework and add your own logic hitting web applications using spring MVC. Most of the work is done by spring framework itself.

As a developer you will be concentrating on the logic to make up your web application to understand the Fillmore better, let us see the spring MVC architecture. This is the MVC architecture first Let us talk about the top level components, a front controller, a controller and a view template. Let me explain the flow. When a request comes in from the browser, whatever it is, it will go to the front controller only. The front controller in a spring framework is nothing but the dispatcher servlet a servlet, which extends HTTP servlet. The dispatcher servlet is going to take in the request based on the URL pattern that is available in the request, it is going to connect to the appropriate controller.

The controller is a simple Java class which is annotated with a direct controller. This controller is going to have many methods with a corresponding request mapping. Say for example, in your JSP page, you are given a heads up equal to grid then This request will be taken to a dispatcher servlet whatever request it is, it will be taken to the dispatcher servlet only. Now, there are multiple controllers which are created by you, all the controllers are nothing but simple Java classes with method. In a controller, you are having a method that is annotated with our direct request mapping with the name similar to the URL pattern. So, the dispatcher servlet will go in first of all the controllers that match this URL pattern, then this URL request will be given to that particular method.

It is achieved with the help of handler mapping. Once the request reaches a controller, that all the logic all the work is done in the controller. From the controller you can call the back in layers that is a service the one all the other things. One we are completed the work The controller is going to return the result back to the front controller that is to the dispatcher servlet. That perform it is basically called a sun controller because he is starting as a security with see me getting all the requests that is coming from the browser. Only the dispatcher servlet will decide to which controller the request has to go, the controller is responsible for creating a model, a model is nothing but a simple Java B.

The controller after doing all the business logic will return the model to the dispatcher servlet were done down the details given in the controller, the dispatcher servlet only is responsible for showing the view page. So, everything is self centered if used in the dispatcher servlet only if you see in this I mean in this architecture, whatever the request, whatever is coming in will go to the sun controller say for example While I'm having greed, it will go to the dispatcher servlet dispatcher servlet will identify the appropriate controllers. It is nothing but a Java class. Okay, say for example, I'm having it as my controller within which I'm having a method, some method this method is annotated with using at the rate record mapping blah blah blah and slash greet. So, automatically the dispatcher servlet will send the request to this method. From within this method, you will be calling the backend layer service, the one all the other stuff.

Once everything is complete, that the status or I mean the controller has to return something, what is this actually it is going to return it is going to return that the view page name. When you are working with JSP servlet. You use request dispatcher, request dispatcher or the Kota request dot get this packet and you pass the JSP page name, success dot j SP Jabra dot JSP. Here, you're not going to pass the extension, you are just going to pass the page name. It can be a JSP page or it can be timely for velocity or web flow or any other view framework, who is responsible for identifying it again, the dispatcher servlet only. So the dispatcher servlet will send the request to the controllers controller, the caller will return services back to the dispatcher servlet, the dispatcher servlet based on the input you are giving the controller as humans will identify the appropriate view page, the view so it is giving the model to the view page.

What is the meaning of giving the model to the view page. When you want to send data to the next page, you use request dot set attribute session dot set attribute in a servlet. Same thing, you attach the model from the controller. The model can be retrieved in the view page who is responsible For doing everything that dispatcher servlet you can just see it is delegate rendering of response. The responses lender to the view template, the template can be any view framework like JSP are timely for velocity, the view templates like do the appropriate or the proper view page to the process. Here, everything is self centered in a dispatcher servlet only.

And again, if you are going to give a new request from the browser, it will be carried only through the dispatcher servlet This is how a spring MVC architecture works. Now, let us see the key components that are in the spring MVC architecture. The first thing is the dispatcher servlet. Then about the controller, then about the handler mapping, and then the view resolvers. These are the key components dispatcher servlet, web application context handler, mapping controller and view server. Id.

I'll talk more about dispatcher servlet. It is assumed servlet which extends HTTP servlet. The dispatcher servlet is going to dispatch the requests to the appropriate harness their Gandhi handler mapping information that is, it will take the URL pattern that is available in DJs in the view page might be greed, our get our books, get books, something like that, that is taken by the dispatcher servlet it there depending upon the handler mapping information, it is going to identify the appropriate controller, the controller that is annotated with at the rate request mapping with this particular matching name. So, this is the work of dispatcher servlet. Next Web Application context already we have learned waters application context, application context with the IOC containers which is responsible for creating these beings inner spring application. Same way because you are working with a We are going in for web application context.

There are two things actually there is a plain the application context and then your web application context. Whatever nature to web should be, I mean is created and initialized by web application context. And whatever is related to the other layer service and dmo layer that is taken care by the root web application context. Let me just explain this as an example. This is the dispatcher servlet, the dispatcher servlet has got root web application context and servlet web application context. The web application context is responsible for creating controllers view resolver handler mapping objects.

Everything related to web route web application context is responsible for creating the infrastructure being that is being shared by all these guys. The service layer is shared by all the controllers. The da will layer is shared by all the controls The database details as needed by the controller mean those kind of being should be added in route web application context. This is all you need to create the objects. So, this is about web application context. Next, let us talk about handler mapping handler mapping is an interface, which is responsible for mapping a request to the appropriate handler object.

For this, the dispatcher servlet is using two types of handler mapping implementation. That is an interface This is an implementation being name URL handler mapping, default annotation handler mapping mapping, we are we will be using the fault annotation handler mapping. For this, we are just going to use a simple annotation at the rate request mapping. What exactly is this? Let me just show you a simple controller example so that it is easier for you to understand. Here I've got a class a simple class which is annotated with our Rate controller, because it is annotated with the rate controller, this class is called as a controller.

This class is having a method. This method is annotated with at the rate request mapping with some name slash greet. So now, whenever a request is coming with flat grids or whenever a URL pattern is coming with slash greet, the dispatcher servlet will identify this handler. The controller is the handler going for the appropriate method that is having request mapping a slash greet. This is all the mapping is done in a JSP servlet application. We do the mapping using the URL pattern, the URL pattern and the servlet name that is what we map in this case, the URL pattern and the request mapping name that is what we are going to match.

Okay, so, the handler mapping uses the default annotation handler mapping for this. We will be using at the rate request mapping our border method and our border class also in nav controller. This is about handler mapping. Next owner. A controller is a simple Java class annotated with that the rate controller. This controller only is responsible for calling the backend service layer and do one layer and from within the controller only you will be creating the model object and send it to a view page.

You decide to which page you want to go. So, you only in the controller you will be giving the view page just like how you set attribute in a record store session. Here also to send the model data to the next page. You can go in for any of this model and view model model map model and view it as a class create a model object and send it to a view. Next model. It is an interface.

Next model map it is a class with using any of this you can create a model and send it to a view How to which view what framework you are using, how actually it is identified, it is identified only with the help of dispatcher servlet remember this from the controller directly, you are not sending to the view page, you are sending it to a dispatcher servlet only the dispatcher servlet only responsible for identifying the view, the view template, the view framework that you are using, how actually this dispatcher servlet will do, it is done with the help of your servers, there are few routes or view resolver available internal resource view resolver resource bundle view resolver and like you are always you resolver and so many others. So, the dispatcher servlet only will take the name that is available in the controller and will go in search of a view based on the view resolver that you have given in your application.

So, let me just show the pictorial representation here again, a request comes in. It is taken by The dispatcher servlet then the dispatcher servlet has to identify the appropriate handles handler for this request. For example, if you are having it as act, it will go in search of a controller, which is having method that is annotated with at the rate request mapping ad okay. So, record calls from the dispatcher servlet to the controller, how it is identifying with the help of handler mapping, handler mapping only is responsible for mapping the request to the appropriate method in the controller. So, the controller methods you will be calling service layer, do some business logic then create model and then you are going to attach the model to the I mean attach the model as an attribute model dot add attribute then you need to return the view page.

So, it can be home or something like that. This is nothing but the name of that particular page where it will go from there. controller again together with the model, the information is sent to the dispatcher servlet the dispatcher servlet has to go and call I mean go to the View resolver to identify the view case, we will be using internal resource view resolver and we will be using JSP only SP view framework, the dispatcher servlet will identify the view frame I mean view based on the view resolver that you have used in your application, create a view and send it back to the browser. So, the dispatcher servlet only is responsible for all the stuff. So, this is these are the components that are involved in MVC architecture, the dispatcher servlet handler mapping controller view resolver it easy to create a spring MVC application. First thing is you need to understand the flow.

Once you know the flow or once you are very clear with the flow working with spring MVC becomes easier or whatever I have explained Over here I have given it in steps. Basically we are going to use Spring Boot to create a spring MVC application. So, in the next session, let us see a simple example of creating a spring MVC application using Spring Boot. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.