Spring MVC Tutorial | Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so are you a developer and you are walking on sublet and JSP maybe starts framework and if you are looking for some simple solution here we are so we are talking about spring MVC so welcome to 37 links my name is 1920 and let's talk about filling MVC now so what exactly spring MVC easy I'm you might have heard about this from lots of people let's move to spring MVC let's learn spring MVC so why everyone is moving to spring MVC it's because it is damn easy to work with again so there are lots of reasons why we are going for spring MVC the first one is it is easy to learn second it provides you that flexibility that you can change anything anytime and third the separation of concerns or separation of the modules example we have different views we have different models and then we have different services to work with spring provides you all those features and if you have already worked on spring you know the power of spring right and this is spring MVC no when you talk about spring MVC we can create amazing websites using things spring MVC I mean nothing different from what is what we done what we do in servlet and JSP but doing the same thing in a very effective way right now if I remember when you worked with servlets what we have did is you have created the soffit class and then you say exchange actually be servlet then you have to bethe method which is which is service right and then you send a request so client sends the request goes to server and that's how it works how spring MVC works is again when you when you learn these things you might find it bit difficult to grasp everything but trust me if you make one simple application or even a small coding spring MVC after that if you want to expand it it is very easy so let's start with it so let's try to understand the architecture or how exactly spring MVC works so just for that what we will do is let's imagine we have certain controllers here okay so we have certain controllers right so if you go to your server technology every servlet behave like a controller right and your JSP behaves like a view so let's say we have something now what controllers I'm talking about let's say we have a login controller we have a logout controller we have add a controller we have multiplication controller so let's say we have this four controllers here from the client side when client sends the request request goes to your favorite file which is web dot XML file right in that XML file we mentioned for which request you want to call which controller right that's what we do in in in your normal survey technology in Summit MVC right but so the problem is if you have lots of controllers sometimes it difficult to manage all the controllers right let's say we have 15 they have we have 100 controllers so it is difficult to manage all the controllers second each servlet will behave like one controller we wanted something called as multi action controller for that again the spring MVC will help you so if you try to convert the same architecture in spring and we see what we can do is we have a client machine here client machine sends the request to this server and server says ok now we are not using normal MVC we want to use spring MVC that means to handle all this controller we need one more controller who will be the front controller ok so with all these controllers we have one more controller ties your front controller then you will think ok now we have to provide one more controller no you don't have to do that thing NBC will create a controller for you you just have to create your controllers but different controller will be given by spring MVC now that front controller in spring and Miss it is called as dispatcher servlet that means all the client requests which goes to grab XML file should know that you are using spring MVC and all the requests should go to your your front controller which is your deployed which is your order dispatcher servlet how can you mention that so it's very simple in your web.xml file just mentioned that for all the requests we have to send the request to dispatch the servlet it's that simple right again you can you know how to do it right you can create a subject tag you can create a solid mapping tag and you can do that now once your request goes to dispense object then what now dispatcher servlet will think okay the request is for login I should call a login solve it I mean I to call a login controller right the quest is for added so you have to call add controller requests for logout you have to call logout controller so if despite this object should know which one to call but there's one more issue here the issue is how would your dispatch a servlet knows which controls at all now your your dispenser which is not a Superman right it will know everything so what happens is when request goes to web.xml file it sends your request to despite a servlet now this for the servlet needs a configuration file so in that file you have to mention which request to call which controller but then you will say that's not good right because we were very happy with servlets no need to that no need for that extra file but holdout pling and missy provides you something called as annotations so all your controllers should be annotated with the help of controller so we have to use a controller annotation and that's it so with that kind of an annotation every controller will have some mapping some mapping steps again we will see that when we do when we do programming or the back of implementation but every controller will have a mapping right you have to only mention in that configuration file that in which package you have all your conjugation and you're done I mean in which package you have on your controllers of course right these are Java file so it you'll be having all this thing in one particular package right or maybe multiple packages so you just have to mention which package you're working with that's it dispatches servlet will specify ok we have to go to this package to find the controllers you didn't have to do that it will be done by your spring MVC ok so let me repeat clients as a request request goes to web.xml file web excel file says ok we have to send all the requests to the dispatcher servlet and dispose of it will check the complication file and say ok we have to send that request to login controller now that login controller will do some processing of course right when you when you call a method it is views which is done for with the help of it is done for the processing right so after the processing as a client what you need some page right so that controller will return you up Paige not exactly that controller will give you the name of the page because it might happen that you have lots of pages right so when you say you are building a website you'll be having lots of views right when you say views I'm talking about the pages which a client sees right so there are lots of pages which page to call that's a that's a question second to see we have a page right it needs two things first which page to call second what is the data now data will be generated by your controller but which place to call you have to mention that in your controller it will mention the page name it will mention the data that data and the page name goes to dispatcher servlet again your front controller you see that it's not directly controller to page it is through dispatcher servlet so the controller will send the response to the dispatcher servlet now this special subject will get two things the data and the view name it will call that particular view okay and it will also add so your view technology will fetch the data from the controller make sense and then that page goes from this well sublet to declined and if you can see everything is done by dispatcher servlet as a client you're only interacting with your trunk controller you don't even know how many controllers you have on the backend side right everything is done by your front controller then you will think you know everything is good but why do we need yy-your dispenser that should know which you to call it's because it may happen that let's say for time when you are using JSP as your wheel technology in future if you want to change your view technology let's say you want to use theme leaf if you want to use maybe some other velocity or 3ma free marker if you want to use all this view technology you just have to change the configuration file you just have to change the configuration file for your what it is for a servlet right you can change it from JSP to theme leaf like this change your views and just change the conjugation your controllers will not even know which technology which mute technology you're walking so that is insufficient of concerned your controllers don't know what movie are you and your view technology don't know what controllers we are using is that awesome is that flexible right so Spring Embassy is awesome there are lots of features you know I feel spring MVC is like a magic and as a developer VR magician right so it's an amazing technology to know more how to do all this request mapping and how to work with the controllers how to create this configuration file how to get a client we already know we'll see all those things in the practical session in this video we'll talk about how to create a simple spring MVC application so as we progress in this video or in the tutorial will go for will first create a simple MVC application which is spring MVC application then we'll move towards the complex part of it and will move towards how to deploy deploy that app on openshift server so before knowing how to or before starting with this video there are some prerequisite first you should know what is spring code okay so basic soft spring I'm not saying you have to be expert in spring but the basics of spring will do second you should know what is mavin or if you even if you don't know mavin you can just pause this video go back to my go back to this channel in that channel I have I have one video on and we see in fact I have sorry I have two videos on mavin so you can refer those videos and come back to this video a matter is not that difficult even if you don't know mavin I will give you some glimpse of it in this video but to just to make you you know more comfortable you can watch that video now so now let's create an application initially you will find it bit difficult to understand why we have all this flow but trust me once you learn this concept once you create your own example or own implementation for this exam for this spring MVC it will be awesome you'll love this concept so let's start with the application itself without wasting any more time so let's create a new project so basically if anyone say you want to create a new project for web so since pre is spring MVC is a web framework right so instead of directly creating a web project since we want the project structure we want lots of libraries for spring so initial going for normal project will directly go for a maven project so using so mavin is a build tool so using map and you'll be getting lots of libraries from a bean repository you will be getting a project structure what you need and even if you want in future if you want to add a new library you can simply edit your what you say the quantification file you will get new libraries I mean how to do that we'll see in the video so just select a maven project here and say next now it will ask you for our default location we'll say we'll say T the default location as it is now in this you will be getting lots of options whenever you see a thumb goes up type in Navin this is a project structure or you can see the architecture now if you want to create a simple codes or application you can select archetype QuickStart or if you want to create a web application you can select archetype web app okay since we want to create a web application here we'll select web app okay now is go for next it will ask you for a project it will ask you for the group ID so group ID is basically a package name okay so you can you can mention any package name here so we'll say comm dot telescope since as I have doing this project maybe I have done this two to three times that's why I'm getting this condor telescope for the for your project you will get his blank so make sure your intercom dot or whatever the package name you want next is artifact ID here you have to mention your project name so we'll say this project is maybe demo of MVC okay and you can have any name will say there might be C here and then click on finish now once you click on finish it will create your project this is a java web project which is which is build using maven so this is the mavin structure here ok and this is the most important file form a vents which is p om dot xml which stands for project object model now you can see there are some errors now since this is a web application you need a web server so i need to link my application with the web servers I have to right click here and go to properties there will be option of target targeted runtimes now we have so I have already have our tongue apache tomcat version 8.0 if you don't have any web server you can simply click on new it will give you some much some choice here Tomcat 7 Tomcat 6 depend up on your Eclipse version so check the suitable Tomcat version available then click on next it will give you option of you can download Tomcat from internet and you can simply browse it to link otherwise you can if you don't want to do all those stars by yourself you can just click on download and install it will automatically download the apache tomcat version and it will you can take a link it and that's it you you will get this you will get a one-time here since I have already have a run time here I will click on I will check it and ok so once I click on OK you can see we don't have any errors now this is the main file you have the main folder and this will be having two things one is your web app and resources you can also create a Java file in this web app I already have index dot JC page just to verify that everything is working fine I was on this application I will say run as the non server ok I have to choose this server here I will click on next so that yeah so the app is there click on finish it will ask you to restart the server it will take some time depending on your machine speed and you can see we got the output as hello world I don't want to you view it Nena internal browser so this is the internal browser for Eclipse I want to view this in the external browser which is the browser for maybe Google Chrome or Firefox so I will simply say window a web browser will select default web browser which is my Chrome if I'm not wrong again II fell in this application click on finish ok let me close my other things here okay so that's hello world here right so we got hello world now I want to add some more functionality I want to create a simple maybe simple calculator or all those things so before doing that what we need is to a we're gonna do something in Java so we require some Java code right so let's say you want to add two numbers so you have to of course you have to of course insert a Java code but it's always recommended to not use Java code inside JCCC page we need to create a sublet right photographer that for that processing but why to create a servlet when you create a normal pooja and you can convert that pojo into solid later on so how to create that of how to create that class which will do this processing as let me first create a form here and so let me create a form so how to get a form will say to say it is a form okay I will will have to input will say input type equal to text we'll name this as t1 which will take the first value I also need a break just so that I will see the next tag in the new line I will say input type equal to again text and we'll say name equal to t2 and that's BR cool but you also need a button which you if you click on that button it will call this second page so we'll say submit we don't have to mention the name it will directly take this submit name as it is and whenever I click on this button I need to call a servlet right we'll name time being we'll name that servlet as maybe add something okay something in that add but need to mention the extension also right so we'll simply or will not mention extension now I will mention extension later okay so I made to call add so maybe this add is a sublet now what next now in order to convert your project into MVC application or spring MVC application you have to do some bottles what you say the extra stuff okay so due to that extra stop into first configure your project into a spring MVC project so it will take some time to configure your project so what we'll do is we'll do it in the next video okay in the next video we'll configure your normal project which is the normal project into a spring MVC project okay so time being so complete this walk then go for the or you can just implement this thing in your own laptop and we'll move towards the next video so that you can follow me on now what's different between your normal MBC and spring embassy so in normal NBC what we have is we have or model we have view and we have a controller so whenever you send a request to a web server the request will be processed by your controller now controller will ask data from the database or from some other resources in the format of a model and the data will be displayed on a page which will be called as view so you send requests to our controller you generate data in a model format you send that data to the view so that you can show all the data now if we talk about spring and if you talk about normal and they say so you have a model you have a controller so you always imagine you have one controller but if you talk about very big applications you know huge applications in that you might you might need multiple controllers to work with multiple views so for different different tasks will be having different different controllers now when you send a request who is responsible to to accept your request I mean how will they know what you want so let's say you want you want to do you want to add two numbers so there will be a specific controller to add two numbers let's say you want to log into a system there will be a different controller to log into your system if let's say you want to log out again there will be a different controller to do that task for you so we'll be having different different controllers so you cannot send a request to multiple control the same time so far all those controllers you will be having a main controller which will be also called as front controller so you have to send a request to a front controller now front controller will decide okay depend upon your request it will check your request and it will send to the request with a particular controller now to achieve that we have to first define who is your controller here and to define this whenever you click on a submit button here the request will go to web dot XML file right so you have to mention your thing here you have to mention where you want to send your request so if you dial you can exactly send your request to a particular controller you have to use a front controller now it is front controller so in spring MVC that front controller is called a dispatch or sublet okay now this dispatcher servlet is a servlet of course so we have to send your request to this servlet and to do that we need to we need to Skype way too your type the redirect data here which is sublet tags now instead of wasting my time in typing that I already created a cheat code here or cheat sheet you can say I will be uploading this cheap sit on my website and you can directly what is this website go to the my website and you can copy this I will provide a link in description area so let me just copy this thing so this is my changes so I have to mention a spring dispatcher saw it here which is a G dot spring framework dot web dot servlet dispatcher solve it so whenever you send any requests so when I say I have to send any requests so whenever I send any requests here which is slash for all the requests I need to call dispatcher servlet now dis- how we will take care for the further processing okay so I'm going to mention this thing so we have to mention a servlet tag and a sublet mapping which simply means for all the requests you have to call dispatcher sublet now just a point one thing we have a servlet name which is telesco again was the importance of this name we'll see later but time and just remember we have something else that is Co as a sub it name perfect now if we talk about this class which is despite a servlet is it a part of Tomcat and the answer is no this is not a part of Tomcat so in order to use this class we need to add some libraries so from where you will get this lab is you have to go to spring website so you have to say spring dot IO in that spring dot I was this is the official website for spring then you have to go to project section then you have to go for Spring Framework you will be getting all your libraries here so which is your spring MVC right which is we are going for spring MVC so we have to we cannot download those lobbies here the only way to get spring libraries we have to use Mammon okay so what I will do to add those libraries in your project you have to use this file which is P om dot XML which is pom file in this you will be having lots of things we don't we are not concerned about all this thing we have only concerned about e dependencies so you whenever you need something in your project you have to add some type of dependencies in your project so let's say you want a jade unit library so you have to add this dependency with the was your name let's say you want to add mask you'll connect up for database connectivity so you have to add one more dependency social typing all the dependency are gonna have a cheat sheet in which I have entered all the I have already entered some dependency so I will just copy this dependencies again I will let you know what are the dependencies are so let me copy this and paste so the dependencies we need here the first one is J unit mean in order to test your application in this I will not test anything so I we don't require this but let's keep it there second you require your spring MVC libraries so this is your spring MVC library next we need may be in future if you want to connect your application with database so we require this dependency now may you may need to work with J STL which is Java JSP standard tag libraries we have to use this library here ok so again you will get all this thing in the cheat sheet now once you say say we it will download all the libraries from Internet okay all them have and repository since I have already done this project earlier so I have a local repository for that so that that's why it was damn fast download all those avenues if you're going for this for the first time it will take some time for you to download so you can see your progress in this section the right section cool let me just show you that how it works so if I say 4.11 I hope we have some type of version there so you can see this type of progress will be there ok let's go back to three point eight point one if I'm not wrong yeah so once we have entered this so whenever you click on a button whenever you click on the submit button it will call it will go it will go to web device ml file which says for all the requests you have to call this page of servlet and then you will get dispose of it from this file we don't want on file now dispatch a servlet needs to send that request to some class right so we need to create that class here so we'll go for two main we'll say new simple Java class I will say simple class with class 0 so we have a class with click on next and way to provide a name of the provide first package will say to us or Saudi comm dot disco okay and in this I will provide a particular name so name of my controller will be let's say add controller okay and slick on finish now this controller this is possible to add two numbers okay now I will create a method here which is public void add which will add two numbers okay and so time it will not take any values just print the output so I will simply say sis out oh so it is this out there's something wrong with my Eclipse yeah since this out and we'll say I am here okay so that at least you fire on this application it should print ham here right so if I run this if I click on submit button and if I the request let's run this white talk all this thing let's run or you start the server okay so we have a form let me just zoom in this time okay so let me enter two value seven and eight doesn't matter it will not add those values but if I click on submit oh we got an error other says file not found so you can see again lots of people hate errors but you know these are the good things to learn new new things so always go to the root cause and search for the error so it says next an exception which is Java dot IO dot file not found exception okay so we are guessing with some file and the file is this one that is Co - sublet dot XML now what is this file this file is basically whenever you run this application whenever your request goes to webbed our XML it will go to dispatch a servlet but dispatches servlet is a configurable class so you despite a sublet should know whenever you call for add you need to call this method now how to teach despair a servlet how to do that so we need to create a new file which is a configuration file for dispatcher servlet and the type of file will be XML will get XML file here and then we need to mention a file name but what should be the file name so it says the file name should be tedisco - servlet dot xml will enter this name again we'll see why to enter that name so I just I just go - servlet dot XML okay so we got a failure but why this filename that is co - sublet so the your file name your configuration file name should be something - it should be something - servlet dot XML so this thing is confirmed this should be fixed you need to change this now this thing is depend upon your sublet names I hope you remember I have talked about how they told you about this you have to remember this part telescope so since this is telescope so this should be telescope so that should be a file name okay now once you got this filename we have to configure this solve let me need to add some bin thing again mission of typing all those things I have a cheat sheet here let me just copy this part and paste now it will specify what to do so whenever I call whenever I call for add my spring embassy need to search for a package which is comm dot choice of a package name o comm dot telescope okay so it need to search for this package is comm telescope I am also saying that it should work with annotation configuration so in the last video we have seen how to create this file which is sublet dot XML file now I can see we are getting some error here and the other is because of this double quotes so let me change this upper cut to a proper double quotes because I have copied this from my other file it was there was some error so make sure you have the proper the Mukesh format there and you can see we are not we have we we have resolved that error now even if you run this there might be some error let me check was the error so if I say finish run it will take some time to load and okay let me close all the other files and if I expand this let's say 7 & 6 if I the submit oh okay it's again you have given me the error which is four not four so whenever you see four not four it is actually file not found and it's not able to map they should have a request with the URI the problem is whenever you call add how would your application knows which method to call so in order to make this as a controller we have to use some sort of annotations so we have to use annotation here which is controller okay so we have to use a control orientation which defines that this is a controller and you need to also mention the request for which request you are executing this method and to specify every time you call add you need to execute this method we have to say a v2 is an annotation which is called as the quest mapping annotation and this equation mapping annotation we have to mention in double quotes will mention for add request you have to call this method called as ad we don't have to make sure we don't have to have the same name and an ad you can have any name provided you're calling that particular name here our URL and now if we run this nothing wait to restart the server so they start it will like some time and let's try this once again if I say run this okay so we got something and we got an error so it is something with the beauty so all the word as a different thing but what we wanted is to print I am here right we got something so since we are printing and here we got an here simple so that's how you can map your application from HTML page of the JC page to our controller but hold on we don't want to show the output on the console we want to show the output on the GSB page or the page right so for that what we need me to call a page here know how to call a page to call a page for sure to get a bridge so let's create a page and we'll name this page as will say this page as maybe display dot JSP let's make this this small so we've got a page here which is displayed JSP in this page let me print I am here that's it and I'm printing I am here now how to call this page now in order to call this page you can what you can simply do a station of reckoning or void we can mention and return a string okay and here we have to say return and you have to mention the name of the file which is display dot JSP so this will call display dot JSP ok so anybody call ad it will call this plate of JSP let's run this and boom so you got a page which is displayed or JSP and you can see it's written and here now the amazing thing about this is even if you are passing numbers we are not accepting those numbers but instead of printing here a dot or something what you say displayed on JSP of our printing ad so we are actually hiding which type what type of technology we are using but we don't want this out but we want to add two numbers right now to how to add that two numbers we'll say in the next part so in the last video we have seen we got this output which is which is I'm here right now we don't want to print I am here we want to print the output of the addition of 7 & 6 so for that what we do is first we need to take the Ricoh the what you say the values from the user so whenever you enter values here within t1 t2 you're calling this controller which is add ok now I'm going to take the values so if we have to say int I it will take your first value so as you if you are comfortable with sublet and JSP you might be knowing this so you have to take input using request dot getparameter I will say pass end and here we have to mention request dot getparameter so good barometer and we have to provide our packets and in this table in this packet you have to mention the name of your field which is t1 and we are getting an error so this error is what is the request object so first we have to do we have to create create or request object and we can create that request object in this add method so I'm going to create two objects here as we create in do get do post in sublet so we have to say HTTP servlet request which is will say request object comma HTTP servlet response which is for we'll name this as response object so I'm going to create this two objects here ok let me input the package for request also now you can use this request object now same thing you do for the second variable which is J so which is int J equal 2 and this will be 2 now once we got these two values let's add those values we'll say one more variable k equal to I plus J now once we got these two values we need to send this value to this display page okay now how to send that value to send the value we need to use something called as model object okay now how to use model object so we have to create something called as model and view object or so model and use a class and we'll say MD equal to new model and view so using model view you can pass the data which you have generated so we have generated our data which is K so you can pass this value using model and view object now whenever you walk with mother and view object we need to specify two things first thing will be MV dot set view name which view you want to call so you have to say display dot JSP it's your view the second thing is what data you need to pass so we have to say MV dot so in order to add data we to say add object and every object will have two things first will be the label for the object will say label is result and the data is scale so we want to send this k to the page now since we are using modern view object we should not return a string we need to return something called as MV object which is modern view object now since we are returning MBA where MV object instead of string will mention model and view the type we have to mention that cool now using this we can pass the data to the view now let's see how to add that thing in tour how to fill that value here so we need to type something on as result is and give you a : now that object or the value belongs to request object so we need to fetch value from request object will be using expression here and we'll say request dot gain attribute and we have to mention something called as result here simple and once you do this changes let's go back to your Eclipse or your browser refresh will pass two values 3 or 4 and 5 will click on submit and the answer you got is result is 9 so that's how you create a simple MVC application in spring so that's your spring MVC so stay tuned in the further tutorials we'll talk about some extra annotations for you extra annotation you should know about spring MVC now once we got this output which is addition of two numbers so this was the output V chord which is result is 13 right so let me close all the extra tags yeah okay so this is what we were getting right let me run this again so that the output okay it's taking some time let's so let me pass two values here which is seven and eight and you're getting the output as 15 right now there's one may one more way of writing this code so instead of specifying the request dot kill attribute we can actually use something called as e l so e l stands for the expression language so how to use a l so we have to use our dollar symbol and curly brackets we have to mention the name of your label which is result so if you are if you see this we have result and then here also you have to mention result okay now let's run this let's see what happens if I run this code so it may enter two values nine and eight if I say submit it says result is in dollar result we want the output not this part right the problem is there are multiple times where e L is not supported so first raise maybe your your Tomcat your your webserver is not supporting e l odd you're using a sublet version which is below 2.4 I'm using 3.0 so you might you might be using a sublet version below 2.4 odd maybe your JC page has actually ignored all the expression expression language so how to enable that so in the page section we can use something called as is e L ignored so by default is false but in my mache it is true now so what I will I will do it is again false I don't want to ignore e l so you just have to add this line if in case if you are getting error and let's go back to the code let's run this and you've got the end of it you got the output which is result is 17 cool so that's how you you have to use e L here now what we'll do next is we'll be creating an application on login form in spring MVC okay so I hope you got the basic idea about spring MVC so whenever you try to you can do one more thing here what we can do is whenever you talk about web applications so we'll be having multiple layers so this is your controller layer so when you're adding two values we should not add values inside your controller so all the logical processing should be done in something called a service method so how to use service so we have to a service class so we have to create a new class here we'll name this class as add service and make sure it belongs to a different package will say package name is comm dot tennis-court service okay and in this service will be having a method which is called as public int add which will take two parameters and which will return one parameter we'll say return I plus J okay and in this controller what we can do is instead of creating the object we can oh yeah we have to create object of add service here so you have to say a service we'll name this as AAS equal to new add service and then we'll be using a s so we need to import the package so we'll say control space so there are two ways of inputting the package go to the class and then say control space that's one way or second ways you can type ctrl shift o if you're a Mac user you can type command shift oh okay and we'll fetch that value will say int K equal to a as dot add it we have to pass two values and then it will add your values and you will get the output now you will say to add two numbers we have we have to create a nooks new class all together see in your in your real life project will be not building additional addition code or calculator you'll be doing something very big right maybe you're creating a login form maybe you have to fetch the current stock market price so you can ask your service class to do that for you so this if you're fetching stock stock market price this will not be only one line this will there may be 50 to 40 to 50 lines in this video we'll talk about spring MVC with annotations so what we have done is this ash this JSP page in which we are asking for two numbers when you click on this button submit it will call this it will send a ad request by sending these two values so I can see in the address bar we are sending a ad request here and then we are sending these two values so as soon as we send a request the request will go to your web dot XML file right so it has the main file so that's what deployment descriptor so if you have if you have the icons if you have the idea about how salmet works so when you send a request recurse goes to your vector XML file and lets you map the request so for all the requests which means when you say slash it means for all the request the request should go to dispatch a servlet which means you don't want your normal web XML to handle your request you want Spring Framework to handle all the requests as soon as you forward the request to dispose of it it will try to access this file which is telesco - servlet or xml now why this file name should be like this the file name should be something - Salvatore XML so since we are adding service name asterisk oh it should be police go - servitor XML if it is ABC it could be a B C - sublet dot XML in this what we are doing is we are aware we have seen that all the all the requests will be mapped with the help of components ok so it will be on a nutrition configuration so you will get all your controllers in the in this package which is Condor telesco right so let's go back to our class which is add controller so in this package is called a telescope we have a controller right in which you have a method which is trying to map a request so let's say when you send add request it will get mapped here with it so when you are sending a different request let's say that's a subtraction request for that e to create another method within that method s sub and will pause the same parameters equals spawn so that we can face the values and then we can actually set the we can do the processing there and we can written the value now since we are sending a add request this thing will work here right so what we want to now as we want to make this as annotation based conjugation so you will say okay which is already configured with annotations right since we are using annotations here so we have an annotation here we have an annotation here that somewhere producing XML right so this file which is the spring MSE quantification file so this is basically this patch of circuit configuration file in which you have to mention the controller names but since we are working with foundations so we are not mentioning anything here thereby to even use this XML file can be replaced this okay that's one so we need to replace this XML file second this one since we are walking with a solid project which is above 3.1 so if I go to properties I don't know where to dimensioned I think it will be in where it will be let's search so sometimes we mentioned that we are using a socket washer which is more which is more than 3.1 and over it has it should be somewhere visit run know okay that's weird but we have mentioned somewhere that we are walking with a socket voucher which is more than more than three which is three point in fact I guess I'm working with a point one so when are you walking with our circuit washer which is about three you can use something on a sanitation configuration now how to specify that you are using annotation here first we'll replace this file here which is the newest go - server.xml now how do you replace these files first we have to create a class four to replace this will say write like class and we'll name this class as we can imitate anything we want but here I will mention it as well just go conflict okay we can give any name so we so conflict we can finish now as soon as you create this class you have to make this class as a conjugation class so in order to make this as a quantification class since this is a quantification further we are mentioning all the quantification here we need to make this class as configuration to do that we have to use an annotation called as at configuration okay because this is a configuration the next thing we have to mention here what are we to mention that we need to scan the components so we can do that with the help of Act components can write and in bracket we can specify the package in which you have this so you can make we can mention multiple packages that's why it allows you to put an array so that we can pick multiple packages so we say comm dot what's the package name it is Jalisco right so we have the package name as chromed or telescope so let's say you have one more package in which you have a components you can just give a comma here and you can add one more component there and that's it it's so simple right so this is what we have done in the XML file so now if you're even if you remove this file it will work ok we'll do that once we start we once we've done all the quantification will remove those two files we'll try to run this so once we have done for this disperse of it configuration let's replace this XML thing so to remove web to XML we can use we can create a class specifically if you are walking with a Soviet version which is more than three so we can create a class and we'll name this class as my web initializer you can name it anything you want okay but you to make sure that you are extending a class which is called as abstract right will be here so it will be abstract annotation configuration dispatchers servlet initializer I know this is a very big name right so this is a class name you need to exchange this class to achieve that feature and again it is only present in Spring Framework okay so for that innate to exchange all the Spring Framework files so click on finish it will give you this class which is my web initializer and you need to extend this class which is abstract annotation configuration dispatches orbit visualizer I know it's a very big named right so let's ignore that part so what we to do here is we ignore these two things should we ignore those two things okay we can do that a way to wait to set this mapping first so for C in XML file we are setting the mapping right so for all the requests so here also we need to mention that how can you mention you can since your Akash ring of adding we're create a string of adder here by saying some value since we have only request types of we give slash so for all the requests you have to call a dispatch is tablet and how to mention that so we need to mention this file name rights we need to mention those Co configuration and here and to do that we to use our method which is not root we need to mention it here so let me remove this and you show this null way to return since we have to return a class re right so we'll say this is class a D so we can specific class ad in which we can mention multiple classes so we said this is my class name has configuration there is conflict dot class white which is that simple so by doing we are back reading this too far this there is a conflict and my visualizer what we are doing is we we don't want to we do we should not is not compensate to use web de XML and these two files so what we can at least will remove this we will so they did it okay and one more thing let's not remove XML let's remove this this this stuff here since we are not mentioning that path so it will not get activated your map my beverage my web initializer will work here let's try since we are changing lots of stuff let's restart the server once that's my Tomcat so I'm measuring is over now so what we have done here is we have removed the XML configuration and now we are using energy annotation based configuration so this is for despite a soffit and this is for XML web de XML file so I guess myself you started you're done it's done let's run this file it's index dot JSP and you can see it's here let me enter those two values four and seven submit and they got the answer they got the answer Vica without any XML file and that's the power of annotation based conjugation sounds good right we can do one more thing here is which we have not covered in the XML XML configurations see I'm calling this displayed or JC page right which is here so we are calling this page will just plot displayed or JSP we are mentioning the extension right so what happens when we walk with spring MVC is we should not mention the extension name here the thing is in future you may want to change to a different view technology so now you're walking with JSP maybe in future you want to walk with free mark or you want to walk with velocity so you don't want to mention the extension here so even if you are changing from JSP to JSF you just have to mention display here somewhere you have to mention that you're walking with dot JSF or dot JSP how can you do that as in your web initializer so in your telescope configuration we can mention a material or we can mention that public we need to create an object of internal view resolver so internal view resolver or internal resource videos although is responsible to search for the view name so as a controller you will just mention I want to call this play what display may be JSP maybe dot JSF doesn't matter we want RJ we want a page with the name display so it is our responsibility to mention the mention the extension the second thing is let's say this display page is not inside web app it is inside web-inf so we are trying to hide that file so we are making this file as private so no one can access that file directly now right so for that we need to create a method which will return internal so that's internal resource view resolve oh ok and we can give any method name that's ok we can say this is view resolver and here we'll try to return the object of internal videos all but first we have to create that object here so we'll say internal view resolved well intonation speed resolver will name this as V R equal to new internal resource videos all go once we got this object will say return we are ok but before returning we have to specify two things the path where you have your JC files and second stop we have to mention the extension so how to specify that so you have to say V r dot set prefix so to set the path we have to specify the prefix so we say it is inside a web either folder web - on earth okay and the extension so we have to say you guessed it dry so we have to use suffix and that should be dot JSP so let's say in future if you want to change it to dot JSF you just need to come to your configuration just make it got JSF it will work okay oh not exactly Jess if you have to make a dot ASP XHTML right that that's like a jet JSF works now since we are following annotations way to specify that this will give you an object of internal view resolver for that mate to use one more addition which is ad bean so if you're not clear what is admin annotation you can just check my spring cor annotation video I will try to make the link in the description area so you make sure you you watch that video if you want to know about how admin addition works okay so now we are not mentioning the extensions are just mentioning display okay let me just make it display one just just for the experiment and let me run this file let's see what happens it should give you an error because we don't have any file name color display one dot JSP that that's what we are expecting right so let me save twenty three and five submit so I can see record the error so if you're searching for a 5 which is display one dot JSP and see in which fold bite is web-inf right so where they have mentioned that we have mentioned that thing here so search in the folder which is web-inf width extension dot JSP we are not mentioning any path any extension here okay just to make it work we'll say display okay let's go back refresh the page is it refreshing yes the first done submit and you got the output simple so that's how we can achieve a spring MVC with the help of annotation configuration okay there are some some more annotation so what you can do is look into some more some more stuff here in this video we'll talk about a new annotation which is request param so why require like why will aquatic requests per ampere so let me run this code now so wild running okay it will take some time to load so it's running it's running so it will open in Firefox now so you can see when I relax when I search when a table number which is five and eight and if I click on the submit query in this address bar we are sending the name of the request we just add and then we are passing two values right which is T 1 and T 2 so these two values they are the quest parameter right so how to face that value in the controller is with the help of request parameter right but for that way to use the sequest object response object and show that what we can do is what we're sending values here we can directly access those values in the method itself you can simply say int I and int J so the high value so the t1 value should come to AI and t2 value should come too J that means we don't have to mention these two things so it should happen at them automatically right while you have to face the value assign the value then how all this will happen automatically is with the help of request panem attribute or annotation in which you have to mention the name of the field which is t1 so the value of t1 will go inside I and the same thing they have to do for the second one second variable so have to say at request param and here will mention it is t2 right so if we have the second object s t2 right and will give a space so the t1 value will go into I and t2 value will go to J and those I and J will go for the addition right so let's try run this code after making some changes so it will open in the Firefox now and what's happening with my tank is were running slow so if I is 77 and 99 oh that's that's 99 and record the output doesn't matter was the output is what matters is this value is exactly going into the variables with the of at request padam annotation okay so so there are lots of annotations in spring MVC or we have we have talked about the controller annotation so basically when you talk about controller it comes under component so controller adaptation is a part of component annotation so when you talk about this component annotation it has three types we have repository we have controller we have service uncontrolled one of them then we can also provide the configuration with their probe add configuration annotations we have add beam and then we have yeah we have this thing so that's it from this spring MVC video or we have talked about lots of things right so in the in the subsequent tutorials if I'm if we can make we can talk about different features of spring we can include in spring MVC like interrogation of hibernate onto all those tops in this video we are going to create a very simple spring MVC application using Java based configuration now again when you make a spring application you can use XML you can use annotations or you can go for Java based configuration and in the earlier videos we have talked about spring MVC using annotations or using XML in this video let's see how to configure that with the help of Java which configuration so for that what I will do is I've got a new project in my in my STS again you can go for eclipse as well your choice I'll be going forest years because it provides it just provides some extra features for spring applications in fact you know I will just change the perspective to STS here to spring so now let me get a project I will say new and I will quit on my own project again you can go for dying web project but then you have to do lots of confusion by yourself let's use maven and now I will say next now in this you have to select web app project because you're making a web application click on next here you have to mention the project name I can mention any project name I will say this is spring MVC Java so these are ways quantification yeah so I will click on finish the moment I do that it will it will try to create a project I'm a bean project for you so where is that it's you can see our project is here now it is giving you an error because I have not linked my tomcat with this project yet so let's do that quickly so I will right click here I will sell properties target one time select Tomcat here and click on apply close and you can see we have we have get got rid of those errors now what I want is I just want to create a very simple application so instead of going for a complex one let's say I want to add two numbers or maybe I want to just print hello world so just to make it more simple let's go for it of two numbers because we are not concerned about logic here we are concerned about how do we create a project so we already have our index file right so in this index file let me just create a form so I will say form action I will try to call add yeah and then I will this will accept two parameters one is t1 and t2 and then we'll click on submit button so let's do that quickly okay so as you can see I have just added two text phil' here T 192 and then click on submit the moment I click on submit it call it should send a request right and normally if you do that in sublet what we do is we get a sublet and then we extend that service via HTTP servlet and then we have to use two objects to face this value now since we are working with splitting and we see what we'll do is let's create a controller here we can say new and I want to get a class and that class would be what first of all I don't have a Java the source folder let's do that later you can see it is going in a resource folder now I just want to move that to a Java folder if I say browse will it give me object option of creating a new folder it's not giving that let's do that later then I will just name this file as a controller has add controller yeah and then I will say okay nothing fancy here just click on finish now this is where it will give you a very simple class but then to make it work as a controller you just need to use an annotation called as add controller so there's no need to X in any class or occurred in specific method you can simply say controller and your job will be done so you have add controller oh it's not working it's because if you want to work with spring annotations you need a spring the agile files right and if you can see in this project I have not added any Java file it's missing so how do you add those Java files just go to your pom excel file and this is where you mention the Java files again you can get this Java files from the mamonde repository just go to google and search for mb and repository and by going that you can search for spring MVC I show instead of going there what I will do is already have a thing or you have a copy with me I will simply copied from there and paste it here just to save some time for this video so you can see I have added a spring MVC dependency which is which is four point three point twelve you can go with any version it will work it will I mean it may work they've been upon when you are watching this video so yes so this this version versus of now now if I go back to the controller if I say control space you can see we got the record the import yeah so once we have done with the controller now we need a we need a method who will take the waiting to the quest right so I would say my method name is public string it will be add which will add two numbers of course and it will take two parameters now how do you pass two parameters again you can go with the way simple concept you can create a request object which is a stupid request object and you can simply face the value otherwise you can use some special feature of spring you can say request parameter request padam and the request parameter will be coming in a format of t1 so the t1 wa are not this t1 you will say the value will be coming in t1 and the value will be assigned to paint int let's say I or maybe num1 so in the value no one will be assigned from t1 so whatever value we are sending from t1 will be assigned to num 1 now if by any chance if you are wondering what that t1 is it is basically a name of the field in HTML so if you can see we have t1 and t2 so the same value will be assigned to na 1 and M 2 here now we just need to return a page name which you want to call so after adding those two values I will call a result page obviously still dot JSP technically what you should be doing is instead of returning a page name of course you will be adding two Stu values so you will say int k equal 2 num1 plus num2 so don't you think you have two things to return now one is the value and second is the page name because this page will have nothing it will simply have it will have a design of course and the data will be depend upon this key so as your K value changes the data will change so of course you cannot make a static content there you need to make it dynamic so how do you make a dynamic that's a question right so what we will do is instead of using instead of entering a string here we can also specify that I wanna go for a model and a view object so we can achieve a model and view object here so we can say model in view MB because in this object you can specify both the things the model or the the view name and the data so it's a model in view so once we once we got this object this key will be added so if we can simply say MV dot add object and we can assign the value of K and we'll set that in maybe so it's a add object I would say this is my result and I would say I want to purpose so a passing key with the help of a result key now once you got this add object we can simply specify this view name as well so I'm trying to call a diesel dot JSP and again you don't need to return now a string format you can return em be because this MV has both of things the data and a view name and you can simply specify hey I want to return a model V object a model and a view object now if you if you if you are wondering you know even if I even if I returned MB and it was string there I was not getting any error why it is not giving you any error it's because you are running your writing your java code inside resource folder and this mistake might happen when you work on a project so make sure that all your java files will be belongs to a job folder so what I will do is I will just create a new folder inside this and I will name that folder as Java folder so I will say here in SRC in main I want to have a job folder as well which is missing in this case I would say Java folder and let's move this add controller here now you can see if I if I make any mistake it will give you error and we are getting out now so it just none - thankfully it will it is I've just moved it there so that I could I can see the errors but it went to a default package that's bad it should be some into some package right so I would say this is comm dot the disco so I would say package comm dr. disco dart web yeah and then I just move it to that package particular package done so you can see in our you know this this ad controller in this in this regard our controller everything seems good right but will this work of course it will not work because we don't have that page yet which is users ESP let's quickly make it I would say hey I want a JC page it's JSP and the name of this JSP page would should be result or JSP and I'll click on OK in this I just want to print the output I would say result is : and here I would say dollar and I would say result so that's it it's just this is how you print data but will it work of course not but still let's see what what else so if I run this code if I right click and run as run on server I will say Tomcat server I'll click on finish okay so this is where I got my output can see that it is asking for two values I would say 4 and 5 I click on submit he's not working why it's not working let's verify by going to sts and if you have not got any problem here the thing is whenever you send a request you have a very typical file here which is called as web excel file and this is a main file for your application right this is where you define all the navigation and unfortunately I have not done any configuration now first of all we are not doing a normal seven application we are building a splitter with the applications so here we have to mention hey web external file if you get any requests just send it to this - omelette now if you remember in the spring MVC theory video I have talked about you in dispatch in spring you have a concept called a disparate omelet dispatcher servlet is responsible to navigate the entire application so it is your front controller and we have not done that yet so I can be again we can do that with XML but I don't want to do that I want to do that with X with with Java Java conjugation so let's make quickly do that so if you don't want to work with your external file anymore what can you can do is you can create a simple class of a simple class right click now and I want a class and I will name this class as my run controller again you can name this class as anything you want but this looks cool right my friend controller now this class is responsible to load so this this class is possible to mention hey this is the spring application this is a spring application which is based on Java based configuration so how do you mention the configuration it's very simple just to make it work you simply need to extend a very small class name okay when I say small it's very big so we have abstract so class name is abstract annotation config dispatch a servlet initializer okay don't have to buy hide this name normally when you work on a project you can just simply copy it from anywhere so yeah so you just need to extend this class so this is abstract annotation web contract annotation config this by the soft dispatcher servlet initializer so what I will do is I will just write so this is the abstract class basically so it will ask you for such certain parameter or certain methods to implement it so I would say add an unimproved methods don't get care looking at these three things it's very easy actually now if you remember whenever you when you've been able used to do the quantification in the XML file the web external file you need to specify them sort mapping right for which request you need to use this application and we used to always say hey for all the request doesn't matter is it a request for a home page or a contact page doesn't matter you have to use different controller now in this case as well how do you mention that for all the requests we have to use it so since you can see the return type here is a string array so of course we have to return something in a format of string array so I would say neo string array and the values since we have only one type of request which we are going to accept which is all so we simply say star slash so when I said slash it means for all the requests just use this my friend controller is that simple I guess that's it from this file now the second problem is whenever you work with spring applications you need to configure some other things as well now let's say I want some other things I'm talking about you know we have a very unique file for every every application that is your something - select external file then you specify the configuration for this by a servlet and we don't have that XML file right and of course we don't want to work with XML files we want to work without with Java quantification so this create a file which will replace the XML file from there the file name is let's say MVC config so it pretty Pete a we are trying to replace a configuration file XML file for for the expressed by a servlet so in total you have to get to confusion files one is this one is this one so this will replace your web external file and this will replace your the dispersed of an XML configuration now in this class you just need to extend one class to make it work and that is extends web MVC so this M web MVC configured up top so this will help you in navigation navigate your application so now you are enabling that hey I am using spring MVC but you have to also give the permission so you have to use one more annotation here which is unable where m we see now why we are using web enabled MVC is because I want to use annotation which conjugation so again you can you can manually specify all the beans here odd so if you want to specify all the beans you have to create a bean for added controller as well but if you can see I'm using add controller here right so why you have to navigate it so you can simply specify enabled web MVC connotation it will use annotations everywhere does the first generation we have to mention we have to mention one more now if you remember in the config file of XML if you if you want me to show it I guess I have a XML configs ample here so if I say web app web and if we can see that we quad we use this two files whenever you want to configure with the help of XML so we use a bevel file where you mentioned the mapping yeah and then we mentioned that is Co - serve it when you mentioned the components can so these two things are missing so this thing is configured using MVC config so we have to use one more by specifying the components can so I would say component scan and here I'll mention the the component in the package will have all these things so I will say combo combo to disco so comdata disco has all the controllers in fact we have only one here but we have to use that and we have to mention that this is a configuration file so I will say configuration so on every class on top of your the plug so on top of your every class you have to mention something or component right but in case of quantification you just have to say configuration again confusion comments are almost same but this works yeah that's it you just need to mention these things and you have configured everything so this XML replacement so the XML replacement here is this one MVC configuration and the web excel file replacement is this my controller so we have done both the configuration but with the help of Java Java based okay now only one thing is missing the one thing is now you can see we have created this config file right how would your my controller my friend kind of knows that this is the file you have to load and that is what you have to mention here can see that it is get route config classes so you simply say hey I have to I want to sit on a list of classes or array of classes and if the class which want which I want to return here is it name is its MVC config dot class that's it we are good at this point after doing all those settings let me just restart map you shut my server I just hope it will work and let's refresh let's one more say 3 & 4 and if I click on is not working it says they are not going to disclose the one exists okay what's wrong let's go back to the code it says it's not able to map ad y but if you see if in this oh I forgot one thing now every method need to have a request mapping right how it will map then so we have to say request mapping now since we are using annotations so this is what you do I didn't hear you mentioned whenever you get a request more ad this is what you have to call it's that simple let's Richard once again and I will go back here and on the output page can submit and it worked off my card it worked it worked but then the value which we were expecting here is something the value right then but we are not getting tender so what's the mistake let's go back to our code sometimes you know your this is expression language which is e l and sometimes L is not so when you get a moment project by default it will not give you support for Al or it will ignore l you just need to say hey don't ignore al so you simply say is ignored false using not you have to say false now let's go back to the page and say submit and you can see we got 10 so finally we are happy she is working and we got the value which is 10 it's that simple yeah so just go for a quick recap what we are trying to do here we got indexed page very asking for two values it was the request will go to the head controller where you are mentioning controller and mapping and we are fetching the values using t1 t2 we are cutting the original model view because I want to send both the things not just view name but the data as well so we got an object we go out to view and we Richard we don't play tuning it but to handle this we need a front controller white and so we have to choice we can go for XML based configuration or we can go for Jarbidge configuration in this example I'm going for Java based that's why I'm quitting on my own front controller which exchange abstract annotation config despite a servlet initializer you have another choice we can go for web application initializer that is just that we have to do lots of settings there using this you just need to set minimum things like you have to mention for which request you are accepting this and what is your confusion file name and the confusion file is here and this you are mentioning hey this is my car this is my package but I have all the components and I want to anybody ever even see annotations and then this is a confusion file that's it now I'm also planning to make a video where I will be using a servlet and as I mean spring em is the application with hibernate and that to a job is quantification so till that point enjoy this week if you enjoyed this video click on the like button and do share this video with your friends thank you so much for watching
Info
Channel: Telusko
Views: 593,857
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: g2b-NbR48Jo
Channel Id: undefined
Length: 74min 12sec (4452 seconds)
Published: Fri Jun 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.