π Add to Chrome β Itβs Free - YouTube Summarizer
Category: N/A
No summary available.
00:00
welcome back aliens my name is Ain ready and let's continue the series on Spring framework now to this point we have worked with spring Boot and spring framework but that was for the standard on application or console based application now we want to focus on the
00:15
web see the project which we were discussing is actually a web project and of course we have seen the project before but uh if you talk about the layers we do have a client here right now this is a client and then we have a server with different layers and then we have a database
00:30
of course we are using database here so that we can get some data and we want to store that data for the permanent purpose so that will store in database and then we can fetch it uh we are using a server which will accept the request from the client which will process the request uh maybe getting data from
00:46
database so there different operations here the client is someone who is using this application so you are the client I'm the client so whoever opens this application or the website on the on the browser is a client now this client is not just a web application it can be a
01:02
mobile application as well so maybe you can have one more client here which is your mobile application and then this mobile application can also send request and get response okay so basically in the mobile application the layout will be there what you are going to send from the server is just data and most of the
01:18
time or most of the application which you building now follows this approach where basically you only send the data so think about a mobile application maybe any uh any application which is your favorite example if you are watching a sport it can be any sport and if you don't want to watch the match you
01:35
but you want to check the score so we have certain applications which will give you this score but if you open that application of course you will get the layout and you will also get data but let's say internet is not working so that app will give you the layout but
01:51
that app cannot give you the data because data is coming from the server that means app will already have the layout what you are going to send from the server it is just the data same goes for your client see in the earlier days you know if you go back way back in the time uh when you send something from the
02:09
server the server has to send two things first the layout and also the data right U so when I say data it can be simple data but what about the layout layout has to be done with the help of HTML and CSS okay and that's what you will make
02:26
your page look beautiful on the client side so server is a responsible to generate both but nowadays what we are doing is we are using two different applications for the for the front end and for the for the back end so what you do is you use certain certain things like react or maybe angularjs in this
02:43
you basically have the layout ready which will go to the client and then you will send the request for the data from the server and then server will send the data and this data can be represented with the help of Json or maybe XML okay so that means as a back end you are
02:59
responsible to to send only the data the client can be a browser which has react application uh loaded of course you guys have to send that from the server itself but as a separate thing or maybe your front end or maybe your client is mobile
03:14
application where the layout is already there what you will send here is again Json now there are different formats like Json XML uh Json is very famous which stands for JavaScript object notation uh which is a very simple and goodlook format data which you get on the client side Okay so when I say
03:30
good-looking not exactly it will look good it's just that it's simple to understand and simple to represent okay so basically uh this is what we are building right now we are not concerned about react here of course you will get the code in the upcoming videos we are concerned about building a back end how
03:45
do you build a web back end so we are going to create this controller this is what is important now how we are going to do this now this has to be a special special uh project not not the project which we are doing because in this project we don't have the web feature how do I know that see when you go to the pom.xml file we are saying that it
04:02
is a spring boot startup project in the startup project you will not get web by default you want web if you remember when we went for the spring projects the web was a separate part so we have to add spring web here of course you can simply add a dependency that will that will make your job good but I don't want
04:19
to add dependency now again it will be more confusing so what we'll do is and of course you can do that it's just that I'm avoiding it so what you can do is you can go back to your browser remember this P this uh particular particular website which is start. spring.io this is where you can create a new project for web so I will create a m project and
04:36
then the language is Java the spring boot version so when we started the videos it was 3.2.5 now it is 3.2.6 upgraded version and okay so I will change the group ID which is com. telescope project is simple web app and then we are going to create a packaging
04:52
of jar and the version which I'm using in this machine is 21 so I will go with that next we have to add dependency since spring start boot or spring boot starter will be already there you don't have to add that what we have to add is web so we'll be using spring web enter
05:09
apart from this I'm also going to add the dev tools see what happens is every time you make a change in your code you have to restart your server what I want to do is I want to have a live reload and that's what you get here so basically you can also it also improves
05:24
your application restarts if you want to restart it will provide you live live reload that will help to speed up so we just need this two of course we don't want to include database at this point but if you want do that you can add one more dependency for our databases maybe
05:41
uh the MySQL driver if you want to use a postgis I'm going to use postgis but if you want to use myql that's your choice uh but at this point we don't need any of the dbms or databases driver so I will simply click on generate this will generate the project for me and that's
05:56
what is here uh we just have to unzip it and load in your project so I'm going to unzip this so unzipping done and now we have to basically open that in the IDE so I'll go back to the IDE I will say open the project so this is the project which we got I will just
06:11
click on open U I will use a new window or maybe this window I just want I don't want this window anymore and you can see uh okay we already have demo spring we'll close that so you can see this is your spring project and the beauty is this is a web project right and I have
06:27
not done any coding yet in the normal world I mean before spring boot if you want to run your web application basically you have to run a server it can be a tomat server or some other servers basically first you have to run
06:42
the server in your machine and then you can basically run your project after the configuration but now I have not done any coding still I will go back to my main if you can see we already have a main code here I'll just right click
06:58
here and say run without doing any coding and it should work and it worked if you can see it says spring awesome this text here and it says the server started the server started on port number 8080 so that means even doing any
07:14
code it is running at this point I will just stop it I just want to show you something I will just go back to my browser and I will just go Here Local H colon 880 why 880 is because we were running the project on ad8 I will say enter at this point it says this side cannot be reached is because the server
07:30
is not running but what I will do is I will just run this now let's run the project and yeah bit fast cool uh it says the it started on 880 I will just say enter can you see that we got something of course not the output we
07:46
were expecting but we got something right we are not getting that page which says uh this site is not reachable you are getting something it says 404 because we are sending a request for the homepage and we have not handled the request how do we do that how do we
08:01
handle the request maybe I just want to say welcome to telescope how do I do that here and for that basically we need someone on the server who can accept our request the request is going to the server but there's no one on the server side who can say Hey you know let me
08:16
handle it and let me return welcome to the lisco let's create that particular person there not a person some service the way you can do that is you can right click on your project or you can right click on your package and create a new Java class and you'll be saying seriously we have to create a Java class
08:34
to handle the request yes a simple class can do that let me show you maybe I'm just lying here let's try so what I will do is I will say uh maybe home I want to I want someone who can handle the homepage request now question arise who
08:50
is that particular person can I say person here it doesn't matter who is that particular person who can handle this and that's where we have talked about the controller so we need a layer here a class maybe who is a controller who can handle your request so I will
09:05
say this is my home controller and in this home controller I can accept the request and respond but the question is if you want to do that in Java how do you how do you make something work so basically we need a method here so I will create a method and this method
09:23
returns a string because I want to say welcome to the list code right so I will say public string it returns a string and the method name can be let's say greet so someone is coming to the homepage I will say greet and I will simply return welcome to the discore that's it simple class simple method and
09:42
you just save this and restart and see the magic so I'll just restart this go back to my page and the magic nothing happened that's the magic nothing happened see the thing is spring is awesome spring is Magic but not entire
09:58
magic we have to we still have to do some configuration uh not much the problem is see this is a normal class right now spring has no idea that this class is responsible to handle request for the homepage and we don't want spring to guess because once you start expecting
10:14
your framework to guess something for you it becomes uncontrollable you you don't know what's happening behind the scene or uh if something is buggy it will be difficult to debug and that's why you need control so what what I want to do is I want to say Hey you know this
10:31
is a noral not a normal class spring framework this is someone who who is a controller so you just say add controller remember in spring board we have used add component in the same way if you want to create a controller just say add controller will this do a job uh see not exactly see on the website
10:48
you'll be having multiple request we are requesting homepage maybe after sometime you want to have contact us about us add to card or maybe all those features right everything is a request request with with different URL what URL I'm talking about see here you get URLs
11:05
right like we can have about or maybe you can have contact so this is important at this point I'm not passing anything so that's blank we can only say slash enter so I want to do it for the slash so for every request you can create a different method which will respond so at this point I'm requesting
11:21
for this if someone request for the slash which is the homepage I want this to be getting called and to do that we have to use a me called request mapping okay so this is annotation not method so we have to use annotation which is request mapping and the back in the bracket of this we can say for which
11:38
request for The Clash request I'm I want to call this so that's request mapping now will this work see we we learned from errors right and we will get errors here let's see but what error you get I will just refresh this and it says okay it still says
11:54
404 um that's weird but if you see it is also saying something it says no ST Source welcome to theis something different okay so it says welcome to theis but it is not showing the text but it says no sta resource welcome to telescope see what happens is a
12:10
controller behind the scene and this is how spring framework was built before and depending upon your use case you can customize it so what it does is it says okay I'm going to call this and I'm calling this method okay the method is getting called to show you the proof
12:26
what I will also do is I'll say I'm here just to show you that this works I'll just restart the app and go back here say refresh is still not reloaded or done if I show you the console it says I'm here that means this method is getting called for sure but still it's
12:42
not working the reason it is not working is what your spring framework does or spring web does is it says when you call this method and when you return welcome to the Risco it will look for a file called Welcome to the Disco but why file remember when I talked about the old
12:59
days when a server used to send the layout with data and that layout is a page so you're searching for a page called welcome to lisore in our project we don't have that page and we don't want to return the page we want to return the text right and to achieve
13:15
this what you can do is instead of using a controller you can use something called a rest controller now rest API is a concept where you return the data from the from the server to client so rest stands for representational State transfer you basically transfer the
13:32
state the data from the server to client not the layout only the data and if you want to do that automatically you can use rest controller this is one way uh the other way is you can send here you can use one more anation by saying hey I'm not looking for the page I'm looking for the body the data so you can say
13:48
response body so what it will do is it will return the data now U I hope this will work if the anotations are correct go back here and and refresh and you got welcome to the go so basically what we
14:04
are doing is we are returning the data not the page but if you want to create the pages you can do that with the help of them leavea or uh JSP you can create those pages here and you can return them by mentioning the names here but we don't want to do that we don't want to return the page because the page is there in the react application what you
14:20
want to return is the data and I don't want to use response body from now what I'm going to use is rest controller because we just want to return data so we can say rest controller and even that works do I have to restart the application we'll do that quickly and
14:35
say refresh it works so that's how basically return for the slash what if you want to do something else maybe you want to return for the about page so if someone is requesting for let's say slash about you want to return something for that slash about will not work now
14:51
is because we're not handling it so if you want to handle that it's very simple you can say public return something and I will say about method and you can accept you can return we can use our tagline we don't teach we
15:09
educate okay so I want to return this this is tagline and then I'm going to say request mapping and this mapping is for the request about so you will say slash about here so for different request you specify different uh methods
15:24
and there's no compulsion that this about should be same as this one it can be different start the server reload and it says we don't teach we educate okay so that's how basically you can have different methods here and that's how you use spring web but then if you see this image we have multiple
15:40
layers here we got the service layer we got the repository layer how do we create those things of course we'll discuss that in the upcoming sessions but just to give you a hint for different layers we will create different classes okay so for service we have different class for for repository be with different class and they will
15:56
also have their annotations not controller but some nothing else next there's no compulsion that you should put all the request in one particular controller you can have multiple controllers here okay and that's the beauty so you can have multiple controllers and let me create one for you so maybe I will just say this
16:12
controller as um a login controller and in this I can handle the login request so request mapping not request request mapping I don't need this import request mapping for let's say login request and this has to be a controller AR
16:29
controller to be specific and here we can have a method so I'll say public it should return a string of course it should not be a string but at this point I'm saying string and I will return something because we're not focusing on logic here we just want to check how can
16:45
we have multiple controllers and I'm going to say login page okay nothing fancy just a login page uh and I forgot the anotation here okay now by doing this what what you what do you think will this work and why I think it should not work in general is we have multiple
17:02
controllers right and multiple controllers has different requests we got login here we got slash and we got about how your spring framework knows for which request you have to go to which controller don't you think that's a confusion we can have multiple controllers we can have 10 controllers
17:17
20 controllers we can have 100 controllers how your spring framework knows which one to call first of all let's check if this is working and then we'll discuss how it is working or I mean if it works maybe I'm not good with creating a suspense so I'll just restart this and about is working but what about
17:34
login even login is working so how it's working so what happens is spring MVC basically or spring web basically has something called a front controller now whenever you send a request from the client the request first goes to someone who is here so there's one more layer here which you can't see which spring
17:50
gives you and this layer here is your front controller and this is not something which you create spring will create this for you and this front controller sees all the request mappings so it creates request mapping for all
18:06
the controllers and it knows for which request it has to send the request to which controller and that's a job of your front controller okay and then the request goes to the controller so that's about the introduction can I say introduction yes because when we start
18:21
working on the project you will see more but that's how the controller Works maybe you have F questions what if you want to send data from the client to the server we have not done that we are sending data from this from the server to the client what about from client to server in that case we have to send data
18:37
here this is where you will accept that data how we'll discuss that in upcoming videos but yeah that's about your spring web MVC see you in the next video