Spring MVC Controller | @Controller, @RequestMapping,@ResponseBody | Component Scan | MVC Annotation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] we are done with configuring our dispatcher servlet so they said dispatcher servlet was our front controller and we have completed configuring this thing and during our development time we have created a you know a configuration file a spring configuration file called tad - front controller - servlet dot XML where we will be actually configuring you know a lot of things for our application like component scanning like being creation like ortho wearing and lot of stuff right we'll be doing those thing right over here okay just like your spring course okay so we will be getting back to the particular point and we'll be seeing everything in action but right now the interesting thing that we are going to do is we are going to create few controllers we have created our font controller here in the web garde XML file right now let's create some controllers so if we go back to our example so probably this so here we have done with creating this thing now let's create few controllers like mom controller this controller Braille controller and let's map them with slash sugar slash makeup / cricket bat let's create the same example here right I will have a lot of fun while doing it because this is pretty simple looks pretty funny and obviously this is not a real time example but you know this example will make your comfortable with spring MVC then you can create thousands number of real time examples right you don't need me or you know you don't need help from me at that time right trust me whenever I'm saying it the goal is to learn the things right and after that once you once you have the basics clear everything will be simple for you so right now let's go back to our STS right okay here what I'm going to do right now I'm going to create few controllers so I'm going to go to Java resources and here I'm going to go to SRC and in SRC right now I'm going to create a package let's say right now the package name is home comm let's say that home dot controllers so we are going to place all our controllers here in this package so the first controller that we are going to create is let's say I want to create this controller let's say we have discussed like this videos mom right so let's create a mom controller okay so I'm going to do a right click here new class controllers are basically Java classes and we'll I'll say it mom controllers mom controller okay and I'll do a finish and my controller is created okay so let me maximize it okay now my controller is created so how to make this thing as a controller how do you know how to tell spring that this is a controller okay so the way we're going to tell spring hey this is a controller by marking it as we take controller annotation so I'll just say F controller here eight controller and I just you know use this particular import here coming from order spring framework the stereotypes just use it now this this particular class became a controller okay so at control annotation is basically tell spring that hey this particular class is not a normal class this is a controller class okay so the next thing is this is pretty interesting I will tell you one thing you know about each component annotation right we have studied the add component annotation during our spring core course and what is a component annotation whatever is the job of that component annotation could you please remember that if you know the job of add component annotation this annotation is as similar as add component annotation do you get that so let's say instead of at controller if I'm writing at component here what does it mean can you tell me one thing what does it mean right now at component I have just added add component here so what does that component annotation is going to do could replace tell me that yes you are right head component annotation we used to use because when we wanted to tell spring that hey spring please create the object for a mom controller and place it inside your container right so so that my my class object I don't need to create you create my class object right other time we used to use add component annotation and also we used to activate the component scanning in our spring configuration file for example if I want spring to create the clock this class object if I want spring to create a object for bomb controller class then whatever you do I'll basically go to the configuration file for in this case my configuration file name is tad front controller - servlet or XML if I'll go here and here or what I will do I will activate the context : components can and here I define the best package for example if I go here whose package this mob controller class present if I do link with editor here this symbol now it will tell me which because it is then now see bomb controller this particular class is there inside this particular package called calm dot home dot controllers so I need to basically in us put this particular package as a best occurs in my components can tack right so let me go here to my bin configuration file and inside the base peckers I can say come dot home dot controllers okay now what will happen whenever spring will lower this particular file it is going to see okay so the developers is not manually configuring the beam rather he is using the annotation approach so he is going to this particular package where this particular package now this particular package is here right inside this particular package whatever the class is finds with a add component annotation it will create the object for those particular classes and will place those object inside the spring container right this one add component annotation does and guys the same thing will happen whenever there will be a head controller annotation there is nothing new in it only the new thing here is that this mod controller is basically a controller and you know we are using it for our web project right that's why we're marking that with add controller annotation but spring will create the object for this particular class automatically same thing whenever spring is going to load this particular file at the time it will say okay there is a component scan that means the developer is using the annotation approach so right now I'm going to scan this particular package com'd at home.that controllers it will go to this particular package com'd at home two controllers now it find this class now he said okay this particular class does have a at controller annotation that means I will create the object for this particular class or I will initialize this particular class and once I initialize I will place this object inside my container [Music] okay great so right now we have created a controller successfully congratulation and now here I will create a method let's say I'll create a method here public less than my method will return some string data and and let's say this method name is give sugar okay sugar and there we go okay and let's return a string here called okay here is years ago this returning a string back so this particular method will be called when okay so if you see again this example I want to map this controller with a URL called slash sugar okay so this particular controller will handle a request called slash sugar so what I do here here in this method I will put an annotation called request mapping and I will map it to a URL called slash sugar okay so whenever somebody will you know in the URL if somebody somebody is going to hit home comm slash sugar then this particular method will be executed and it is going to return a string said okay here is your sugar okay guys so here one more thing so whatever we are expecting right now if somebody is going to hit slash sugar we want to show him this particular data in his web browser but this thing will not happen and to make it happen we need to use a another annotation called response body right response body what this response body annotation will do it is basically gonna write this particular message in your HTTP response so whenever somebody is giving us a HTTP request by hitting splash sugar we are going to execute this particular method okay because it is mapped with a URL called slash sugar then we are going to give him a response back we are going to write in his response body MSS that okay here Richard sugar this message we are going to write in his response that's why we are using a a notation here called add response body okay now let's do a control s and we are done writing our controller and we have successfully created a controller which handles a request called slash sucker sucker so right now let me run this particular project again I'll do right click run as run on server let me deploy this project here in the Tomcat again and I will do finish here so I'd say restart the server and obviously my server will be started without any error that is my confidence on myself so so let me see the console no error that's good now I'm going to hit slash sugar here okay right now let's go ahead and hit that URL home comm slash suger right home guard come is going to come from my dispatcher subway because I my url pattern should be starts from home calm then I'm going to hit whatever the controller I have written here among controller here this particular method is handling a request called / sugar so I'll write home comm / sugar then I can expect this particular method to execute okay so now let me go here and let me write home calm / sugar and there you go okay here is your sugar so this particular message is coming from where if you stay here here only you have written the particular message correct okay but one thing is confusing here so whom God come is my project name that's why again home.com is coming here home welcome is my project name so here it is coming localhost:8080 / calm calm okay then i'm writing whom door comes flustered it doesn't look good so let me try changing my project name so let me do right click the refactor rename let me say this my first where my post MVC project okay now this will make sense to you okay right now I changed my project name - my first MVC project and now let me run this particular application again and this time it will not be confusing because home door comes classroom comm / yogurt that doesn't make it now let's see the URL HTTP colon double slash localhost:8080 is your port number then your project name my first MVC project now I am going to get home calm this is the URL my dispatcher circuit is going to handle then its last sugar this is my controller is math with slash sugar right so again my mom controller is maps with a request called slash sugar so let me hit that here slugger and enter and they re called okay here little sugar right congratulation we have actually created a spring MVC project and we married up and running [Music] if we have created our first spring adhesive project and we met it work and we are actually doing things the way we have shown it in the presentation right so right now I'm on controller is ready and in our monk controller we have created a method which is basically going to handle this request called / sugar so right now this create few more controller like system toner broken puller and less you know math with / make up to sis controller and let's match / to get back to the row controller so to do that what I'm going to do I'll go to my STS again and right now let me close all these things here and like my mom controller I'm going to create a new controller here right click new plus let's say this bro controller okay I will be entered and my controller class is created so now to make spring no this is a controller first of all again I am going to write here add controller annotations and inside this pro controller I'm going to create a method public stream give cricket back right that's what we have seen in our example right if we see here / ticket back we want to map it to and this guy is going to give us back a quick attack right so let me go to test es again and let me say every town hey man okay this is your cricket okay cool let's keep a semicolon here okay so now this method will be executed when somebody will hire a URL let's say request mapping and the URL will be let's say slash cricket right when somebody will be hitting home comm slash cricket back then this particular method will be executed and this particular string will be returned to his browser okay and to make it happen to write this particular message to the HTTP response body what I will do I will use the annotation called a response for it correct now I will the ctrl s okay so that's it so I'm done writing this controller method right now and right now what I will do let me again redeploy it to my tomcat server so I'll do right click run it run on server I'll do next and finish so my cell phone will be up and running once and after that I can test to see what else right so I'll go over here and say last home.com class sugar okay this is what working okay here it is sugar right now let's choose class cricket backs your eyes seeking 50 cricket back okay there you go hey man this is your cricket back cool we have successfully created two different controllers and we know right now how to actually create a controller by using the add controller annotation and if we want to make anything printed with the HTTP response body then what we can do actually we can use a notation called add response button right pretty simple right now let's break the theory let us understand some fundamental and let's try to crack all this theory step by step and let's be a guru on spring MVC that's coming up right [Music]
Info
Channel: Selenium Express
Views: 53,449
Rating: 4.9234447 out of 5
Keywords: how to create controller in mvc, @controller, @responsebody, @requestmapping, spring mvc, spring web mvc tutorial, spring mvc by selenium express, spring mvc by abhilash, spring mvc project, spring mvc example, spring mvc tutorial for beginners, spring mvc annotations tutorial, what is controller in spring, what is controller in spring boot, spring mvc controller tutorial, spring mvc flow, java spring mvc tutorial, spring framework tutorial for beginners with examples
Id: gq84DReItek
Channel Id: undefined
Length: 18min 52sec (1132 seconds)
Published: Tue Aug 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.