HOW to handle PHONE/ CREDIT CARD user input? Spring MVC / BOOT Formatter | Explore with debugging.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back and in this video we are going to talk about the spring MVC formatter so before we go ahead look at the content that we are going to cover today okay so these are the contents that we are going to learn today as a part of spring MVC formatter so first I'll tell you how to deal with the national objects whenever you will be working with a spring web application and then I'll give you a problem and we'll understand why we need a formatter in our web application once we are done with that I'll give you a deep dive on the print and the parse method and these are the methods that we'll be using whenever we will be writing a formatter of our own cool so the next thing that I'm going to cover here is going to be the problem solving scale okay so I'll give you a lot of scenarios okay I'll give you different scenarios in this particular video and your job will be figuring out the solution for that Denari I'm going to help you for this but in the end of the video I'll give you an assignment okay so as I said I'll be creating the situation or scenarios and we're going to solve the scenarios by using a formatter and we'll be developing or writing our own formatter in this particular video and in the end I'll give you an assignment okay I'll give you a problem I'll give you a situation and you need to solve that out and you need to send your assignment to me okay and one of the most important thing is that we are going to start using the Eclipse debugger from this particular video for the next few videos we'll be covering through different concept it is pretty important for us to understand the flow of execution how our program flow is going through and we'll be tracking all these things by using the Eclipse debugger so I'll definitely encourage you whenever you are following this particular video you also start debugging your program okay so if you're already an experienced programmer then I do not need to tell you that but yes if you are a fresher then this is the time to start using the Eclipse debugger okay and I'm going to help you I'm going to have some dedicated video on this as well but right now let's keep things simple alright so before we go ahead I know that you have a lot of complaints against me right I was actually going through my mail box yesterday and I found a lot of Mel's avila this has been for weeks I probably three weeks you've written and that I haven't posted a video let me tell you this guys right now I am working in three different projects and if you if you include my one one then it would be for projects right and apart from that I need to record the videos for my training batches and I also need to recur the video for youtube and also this is the log down period I do not have anybody around me who can help me during the editing or during the video making process so it is getting really really tough for me to shoot a video but anyhow I have recorded five hours of content for you guys right now so here it goes one and half hour and obviously you know you'll be getting a lot more contents in the upcoming days so you can expect to three videos more in this month let me tell you that two or three videos in this month okay that's what I can do for you but the content that you are going to get it would be more than five or six are okay that's my promise to you cool so I believe that you guys are going to understand me I know you you are also a developer you also do a whole lot of work so you can relate to raided to my words right and also I lost my tripod I mean whenever I was coming back to my hometown before the lockdown started I just lost the tripod at airport I just forgot to bring it I just lost it near the KFC in the Bangalore Airport so right now my camera is sitting you know on a broken tripod so if it is going to fall then from tomorrow onwards I cannot shoot video so a lot of problems around me this lockdown is just ruining everything but hopefully everything will be back to normal you know pretty pretty soon cool so without wasting any time now let's get started okay so in this section we are going to understand how to deal with the nature objects in spring MVC so before we go into the formatter concept let us set up the environment let us create some scenario where we can implement the formatter concept okay so today you are going to learn so much if you are refresher if you do not have any experience in industry in IT industry then this video is going to be going to be beneficial for you guys for sure I only designed this particular video keeping the freshers in my mind okay so you know from today onwards we are going to start debugging we are going to do a lot of stuff and slowly slowly we are going to progress but I know that you guys have a good hold right now or to understand a little bit about one thing so let's get started with all these things but before you go ahead with this particular video if you can revise whatever the things you have done so far we are in the episode 9 right so whatever the things that you have done till the episode 8 let's have a look on that right just try to go with your code first you know just revise all these things and then you can start watching this particular video or if you remember every thing okay if you haven't forget anything for the last three weeks then that's good let's get started with the video okay and let us understand about the next eight objects and how to deal with the nested objects in spring MVC you okay so right now I am going to give you a new requirement so this is the registration page that we have built a couple of episodes before so now I want to include some new fields to this registration page so let's say now I want to include the communication channel right here so I need an email input and a phone input right here inside the registration form now you may ask me that here will ask what's the big deal the way we have created these elements we can similarly create two more right so it's not a big deal so I I definitely agree with you but hold on there is a catch with it and with this thing we are going to start today's tutorial so for now let me go to my Eclipse and let me open the backend code for this particular page okay so right now this is the particular project that I am working on spring love calculator and I have deployed this particular project in my server already you can see it's already started so if I go to my Safari browser right now this is my home page right so now let me say I'll go to my registration paste by hitting's class register so this is my registration page whatever I built so far so the task is I'm going to create two new text boxes just right here called communication channel best right but before that I need to make sure that what is the back end of this particular page Oh what is the DT or this particular page is using in the back end okay so what is the URL for this specific page so if I go to the URL and department copy this class register so slash register is the URL this particular page is using so I'll go to my Eclipse right now and then hit command shift L or control sit L and I'm going to paste it okay so what this command is going to do is it is going to search for this class registered text across your application so right now in my entire application in two different places I have used this word called slash register so it is just searching the right so this is cool right so even if you don't know where you are actually using this URL you can simply type this URL here or you can type in anything right here and it is going to find the appropriate match for you so I want to go to the controller so you can see there is a match found called slash register inside the registration controller so let me go there and it is going to land me inside the registration controller right now okay so this is the handler method that we have used for our slash register URL pattern which is matches with our page URL pattern just right here okay so this particular page is handled by this URL mapping okay so now I want to find out what is the DP of this particular place user registration page which is this one what is the DT of this particular page is using this is this DT o UJ registration DT or if I'll go into this DT or you can see all our fields are here that we have it in our registration form so as I said you in the requirement I am going to add two more text field here so now you can say me that here balázs we can create the email field okay the email field and phone field just right here and we are going to make changes on the JSP or the view which is user registration page if I go into this particular page let me go into this once I come over here you can ask me to add two more particular field right here after these teens for phone and email and that is done you are basically right but hold on I'm going to show you a scenario right here which is going to come really really handy whenever you are working in a real-time application okay so here is the scenario let's imagine that this is the registration page and here we have our email and phone and also we have a contact or space okay and in this page also we need the email and phone just think like you know if somebody want to contact us right we can say them okay these are websites our Facebook page or Instagram handle and you can connect to us with this and also if you want a call from us then why don't you give your email and phone okay so it an extra user to enter the email and phone in the contact or space okay so now here is your scenario that we really need to care about because we have the same field in our registration page and also we have the same field in our contactor space and what about if we have about Ursus what about we have some different tests which will also need the email and phone input so at that time if you are going to add the email and phone inside the registration DTO that doesn't make sense now what about if I am going to include this to particular field inside a new DTO just look at here if I go to Java resources source me in Java and if I go to the API here I'm keeping all my DT or so let me create a new DT or here let me create a new class let's say communication DT oh ok so let me do finish ok so now I'll have a new DT or here so let's say I'm going to keep my string here string email and let me make it private and let me hit command one right here and let me create the getter and setter for this email field now this is a separate DTO that I have created for my communication so I can add the facts to form the website everything that I need for my user related to the communication channel just right here inside the DTO and whatever the pace that need a communication dt or just like a contact us pairs about a space i can use this particular detail inside that particular page so what I mean by that for an example right now in our registration video we have the requirement that we need the email and phone field so what I'm going to do here here instead of creating something called private string email directly here what I can do I can write private communication DTO and I'll write communication DTO and that's it and I'm going to create the getter and setter for this particular fill select create getter and setter and I will create the getter and setter after the gender and I will do okay [Music] you [Music] cool so right now we have a getter and setter ready and now we can go to our registration page which is the view that you are seeing right here and here we want to enter two more thing so what I can do here I can create a new deals and inside this team I can give a header let's say a h2 or h3 here and let me say this communication channel or let me say communication for this now and let me align this deep to a center because that's the thing that we are using across the platform and after this I am going to create a label for my email okay so let me say email and also I'm going to use the form input and the path equal to so here is a big deal so previously that we did okay so the gender the particle to gender okay for the for the country the particle to country name which is exactly matching with the property name right for the country the path is country name so the hobbies the path is hobbies for the gender the path is gender which is exactly matching to the form element property name just look at here for gender the path is gender itself okay so if you go into the flow our part should be communication DTO because inside the communication DTI people do control click inside the communication detail we have the email field okay so what I'll do here the particular - alright communication dto okay cool so right now let's test this out so our communication dt or is exactly messing with the registration DT or property name which is communication DT oh so if I go to my web browser right now and if I do refresh this particular page let's see what is going to happen cool the page is loading fine and we get our email field right here so now let me go here so let's say we have some default email entries less the selenium X face at the rate gmail.com okay command s and let me check that whether the data binding is happening fine and whether I am getting this particular email in my text box whenever I'm loading this particular page so right now let me go ahead and deface this page and see here nothing is happening right I am NOT getting my email just right here but my email my default email that I have given is the selenium expressed at the wise email comm so what is the problem which is happening right here so it will go to the user registration dot JSP here I did a mistake so here in the space I'm writing communication DT or here so the meaning of that is if we see here the model attribute for this particular form is user registration and inside the user registration which is user registration DT or we have this communication did here but inside this communication detail we have our email right so the way the syntax we should write there is the communication DTO which is my property name and inside this communication detail we have the email field so let me go to my user registration - page and right now let me write here dot email so inside this object find out the email property which is this property okay so now let me save and let me wait till my server reload the changes so that I can paste this application okay so now let me do a refresh here and boom now the data binding happened right and we get our selenium Express at the way gmail.com which is the default value of her email right here in our page so now the data binding work and you you have found a new syntax here right and that's how you have to write if you have a nested object we have an object inside the object we have the email object which is a string object inside the communication d2 you're upset so what will happen wherever the page load spring will call the get communication DTO method and over the gate communication it will call the get email method so the syntax will be just like this get communication TPO and over this get email okay so this is the syntax the spring is going to run internally cool so right now let me remove it and let me do a command s and now let me bring in the phone field which is going to be really really interesting right now so I'll go to the communication DT or and here let me remove the default value this is just to show you and right now I'm going to add a new field here called private a stream horn okay but hold on this form is not going to be a string type right now so if you see a phone number right in real-time the phone number should be just like this nine one something one two three four five six seven eight nine one ten ten digits right or for us one something okay some numbers just right here okay so now let's analyze by looking into this number what do you think okay we have two parts of a phone number the first one is the country code which is nine one for India and one for USA and the actual phone number of the user so the thorn is basically consists of two parts so instead of making it a string I can create a new class here called phone and insert this particular phone class I can include two different property called country code and the user phone number okay so let me delete it and let me create a new class let me hit create class called form so my Eclipse is going to create a class I'm going to keep this class inside the API project that's where I'm keeping mine almighty TOS I'll click on finish okay so inside my phone class right now I'm going to have two different string here right at string country code and another one private string user number okay and let me generate the getter and setter for this so right now I also want to generate it to string for this I'll write to string here control space and override the tostring method and right now I'm going to return here the get country core plus the get user number okay in the to string method okay so right now our form class is ready and now let me go to the communication DTO and here we have the email and phone and now let me generate the getter and setter for this particular phone cool so right now all that classes are ready so this is what we have created so far we have a registration DTO inside the registration detail we have a communication dt or the communication dt or contents to field email and phone so the email is a string field and the phone is has its own type which is called phone and we have created this phone class and inside this phone we have the country code and the user number has this properties you okay so that's good so right now if I go to the user registration page so here I do need to create one more text input for the phone field just like the email okay so before we create one more text box here for fun what I'm going to do here let me taste this out let's say if I'm going to enter some email just right here and if I'm hitting register button I need to carry that email and populate right here in the registration successful page okay so what I'm going to do here this particular page is ready is where I'll go to my registration controller you can see this is the handler method which is going to show us the registration Sox spades so even we'll go to the registration subspace if I'm going to do a ctrl shift R and if I'm going double click on registration Subspace here we go we do have our page right here so now let me give another break and let me write email here and further email I need to copy this line and it will purchase right here and over here in the user registration object if I go to the user registration DTO inside the user registration detail we have the communication date here so let me copy this property communication DTO and let me paste it right here and inside the communication date you are if I'll go to the communication DTO right now inside this communication dto we have the email field so let me copy the email and let me paste it right here cool so right now my expectation is if I go here and let me do a refresh okay so right now let me enter some email let's that test at the write test mellow calm and depelter register I should be able to see this particular property just right here that means my data binding is happening fine cool so this is working good but what about the phone okay so let me add the form right here in the user registration place so let me copy the email and let me paste it right here and let me change the label to phone let me delete the path so for the email the path is communication DT or dot email so what is going to be the path for the phone right so inside the user registration DT oh okay we have the communication DTO again so let me do command C here and let me go to the registration page let me do command V dot and inside the communication DT or we have the pawn right so this is the variable let me copy this and let me paste it right here and that's it you so now you might be asking me her balázs if you go to the user registration dto if you'll go to the communication DT or if you'll go to the phone this is the reference type this is not a string tight because inside the phone itself we have two different string country code and username so you might be asking me to do something just like this communication DT ordered for not country code okay you might be thinking to do something just like this but hold on inside the porn if I go to go to the phone here here we we do have country code and the user number we do have two different string but I don't want two different text boxes for the phone I want only one text box for an example we'll go here I'll just remove the country code just right here so the data is going to be binded with the porn property and if we are going to see here the phone is a class so I want to bind the data with the phone object not to a particular string so right now I'm going to do something like this communication DT or dot phone and if I'm going to do the same thing in the registration stocks of Spades less there right now if I'll give it another break let's let me give another break right here and it means save phone and let me copy this let me paste it right here and let me say porn okay so right now if I go to registration page and if I do it phrase right now I should be getting the phone field I want exactly something just like this I want only one text box here I do not want two text boxes one for the country code one for the number no I want only one right so what I want right now keep on going to enter some email address and if I'm going to add some phone number let's say I want my user to odd in this format let's say nine one this is my country code and let's say my phone number is two 2 three 3 four 4 five 5 six 6 seven seven okay now let's say if I'm going to do a regice I want my user to see this data that he has entered this as his phone number and this as his email so if I do a register right now see what is going to happen and boom we get a bad request now let's solve this error so why we're getting this error if I'll go to my console here and if I'm gonna check my console you are going to see a new error and this is going to be crazy okay so we're getting a field error for the user registration object and we're getting this error for the field called communication DTO and inside the communication detail we have a field called phone and for this phone the data binding is not happening and it has a rejected value which is whatever the number that user entered and it says type mismatch okay a type mismatch that user registration that communication dt or that phone so this is the error that spring MVC is giving us and letting us know that hey I realize this is a very simple thing I'm not able to convert this data so whatever the data that user is entering here in the registration page let's say he's entering a string here right this is simply a string whatever the data that user is entering here entering here all the data that the user is entering these are just trained right so whenever the user is giving a string and whenever we are trying to do the data binding in the software and if I go to the user registration controller and right now I already told you that so whenever the user will fill in all the details then this is the handler method which is going to be get called and obviously the data binding will happen here so all the data that user entered that data will be binded to the user registration DTO pairs so right now when the data binding has happened we have already seen that all the value that we have inserted here for the user value that will be going to bind it with this one and for the user name that will be going to bind it with the user name and similarly for password this is going to be a string that the user is entering but spring is automatically converting the string to a character array let's say if we have the age text box here okay let me show you this thing let's say we have something here called private int edge let me generate the getter and setter so I have generated the getter and setter for the edge and it will go to the user registration - page and let's say if I'm going to develop the edge field right here let's say if I'm going to create a new label here for the Aged ok so right now here I've created another text field for the AIDS and Mark that this edge path inside the user registration DT or the data type that I have taken is int okay so if I go to my website and determine to rephrase this particular page so now we can see that a is filled right here we're getting the default value 0 here that's because the data type we have taken as int ok in default value is 0 so that's why were getting 0 here if you do not want anybody right here this change it to integer type okay integer data type integer wrapper class I'm talking about right so the objective all types will be null so you won't be get any default value right here if you are going to change it to integer that's not a big deal right here but the thing that I want to explain to you here is ok let me go here for now let me remove this porn code from here otherwise I'll get a bed request error let me remove that and let me do a refresh right now so the fun will go away let's say I'm giving 30 as urghhh and if I'm going to do register here so I'll get 30 right here okay as I have not displayed let me go to the registration stocks space okay so now if I go here and if I'll do a refresh and now if I fill in the value of ages 30 and it'll do a enter now you can see ages 30 but the interesting thing which is happening in the behind if you see here right when the data binding happens if I go to the registration controller whenever the data binding happens here so what basically happening the user is entering a string okay but the data which is getting binded inside the registration DT or the Earth's type is integer so 30 is binding to the age property and is automatically getting converted to int type right so this is basically happening behind the scene by the spring by using the default data binder but what's wrong is happening with the some types of political communication DT or if you see here the phone type is a object itself it's a class is a reference type or is a special type right so if I go to my user registration page and if I create the text field for corn so now the problem that we are having here if I'll do retrace this page now this porn whatever the data that user is entering less than nine one eight eight eight eight Alisa this is the phone number whenever user is doing a register now that's the string that user is entering right but the problem is the spring internally is not able to bind the data with this particular field called porn that's because this porn field is not a string type is the object type this is a special object so the binding is not happening by the spring framework itself and spring is saying hey this is a special format that you are receiving the phone you are giving country code 91 then - then eight eight eight eight this is the country code and this is the number so how come I am going to assume that what is the country code what is the number then you have to explicitly separate this thing less the 91's country code eight eight eight eight is a phone number and you have to tell me that inside the communication data or inside the phone class I have to bind the country code with the value called nine one and I have to bind the user number with the value called 8 eight you have to explicitly define me that and then only I am able to bind the data I am NOT going to bind the data automatically because I do not know the data type that you are using and how you want to store the data and for this and for this kind of pattern just like a phone number or a credit card number we have to write some kind of special for matter okay and inside it formatter we have to define that hey this is our format right so I'm going to extract the country code from this format whatever user enter and I'm going to extract the number from this and this number I am going to store it with the country code and with the user number and this object is going to be stored in my database so the user is going to enter the phone number as a string so we have to receive the string we have to come that number to a object of porn and we have to set these respective properties and we have to store the object the porn object not the string object we have to store the phone object in to our DB or into our database so we have to do some kind of formatting right now and now let's understand how to do all these things and how to write a formatter which is going to house praying for the data binding whenever we are dealing with some special kind of objects just like phone or the credit card number and how we are going to get the data binding done in this particular case let's go for it okay so now it's the break time okay take a break do not jump to the next section and before you go ahead make sure you have practiced you have done you have coded whatever I taught you for the last 30 minutes that is going to help you that is going to help you for sure because you will have a setup in your mind right I am going to give you some assignments at the end of the video so in order to do those assignments you have to have a proper knowledge on the formatting concept okay or you you have to understand whatever I have said in this particular video so pause this video right now and practice whatever I have said and then you can resume this video so take a break [Music] [Music] right now let me give you a step-by-step walkthrough on the format or concept of the spring framework or the spring MVC or the spring both whatever you can say so let's learn the four model concept the way we can create a formatter in spring framework by utilizing an interface called formatter interface to see that interface it can hit command 60 and you can search for formato okay so you can see there are multiple formatters available here but the one we are interested for is the formatter interface coming from all that Spring Framework that format cool so let me double click this okay so now if you see this interface so this interface doesn't have any method but it extends to another two interfaces one is printer interface and one is partial interface so if you see the description of this particular interface it says that a formatter is both a printer and a parser okay so what do you mean by a printer and parser so if you go for the parser interface right now in our scenarios we are going to use the partial interface and we are going to utilize this method the parse method here to convert the data in our situation we are going to use this pass interface right now to convert our phone number which is a string type to a object type called porn okay so we are going to use the paths method here the parts means conversion okay so by using the passed method here we can actually convert the data type of our object so now let's use the passed method here to solve this particular problem so let's capture this data and let's use the parts method here of the parcel interface and let's convert this in beta to an object type in our case this is going to be the phone type so let me type in phone we are going to convert this particular object which is a string object to a phone object okay so let's do that but before that let me come back to the partial interface so how I came into the % of s let me go back so right now as I said in the format interface extends to both partial interface which is going to be used for the conversion of an object and also is exchanged to the printer interface so the printer interface does what it is just display this works in the other way of the parser right don't be get confused I am going to give you an example then it will be pretty clear for you so first of all right now the thing is if I am going to implement the formatter interface then I have to override the method which are there inside the printer interface in our example right now the printer interface has only one method called print and also we have to avoid the methods which are there inside the parser interface and inside the percent of s we have the passed method so let me go back and let me close this formatter class parser class phone class and the printer class now let me create a new package here okay so let me do a right-click new let me directly create a class let's say right now the class name will be the for number formatter because I'm going to format this phone number so let me say here for number formatter so we may have different types of formatter for our project so let me keep all my formatter into a special package called come dot selenium express dot formatter cool so let me hit finish right now so I will get my phone number formatter class so now to create a phone number formatter as I said first of all this class needs to implement an interface lesyk implements now we can implement the format face of the spring thermal all we can implement the parser interface or you can implement the printer interface right is up to your church but I'm going to implement the formatter interface because this is going to give me both the parser and the printer makers so now I can override both the parts and the print method right here ok so now we are getting some error here that's because we have not defined the type here so the phone number formatter by using this particular class actually what we are trying to format obviously if I have already told you if I go to the phone number I am actually trying to enough format the phone number that I am getting right here and I'm going to convert it to the phone type so the type here I'm going to define it as the phone command s and the error should go away no ok let me delete this methods once again and let me again click this link here ad unimplemented methods and right now we get our print method and the pass method now let me design my past method here and I am going to start by writing some comment here and first of all let me write their swiss out here just to add a debug purpose line and just to make sure that my past method is getting executed so I'm going to say here inside the parse method of off the phone number formatter okay so now let me write the logic to convert this particular string into our porn object so okay so before I write my logic here let me run this program once so that you guys can see our flow is coming to this pass method whenever we are submitting this particular phone why the flow will come to this particular method that's because we are writing a formatter for our phone object okay so now let me deploy this particular project into the sulfur and let me see my pass method is getting executed or not right so it may pass method they get executed this particular line will be there in my console right otherwise I can put a debug point here and I can start you know debugging my program and I can see that my flow is coming to this past method or not but hold on one more thing to make this one formatter work right we need to tell spring and BC that hey we are using the formatter and the way we're going to tell spring MVC that we have written some kind of formatter and you have to execute our formatter class where we are going to inform spring MVC by going to our configuration file so what is a configuration file so this is the configuration file that we have for application let me open that file and here actually we need to add the formatter so the way that we are going to tell spring MVC that hey we are using a formatter thus by implementing I can implement an interface here so my love calculator app config class I'm going to implement it with a interface called web MVC whether misconfigure okay that's better interface so right now if I go into this Web MD second single interface you can see there are a lot of methods these are the default method because I'm using Java 8 and the interface can have a method with a body so no problem with that and this is a Java 8 feature but this is the method that I'm looking for right now default void add formatter so this takes the formatter registry as arguments and using this registry interface we having to order formatter to see how I'm going to do it so let me close this interface and here this make sure that you have implements the wave in the second figure interface in your application country class now let me override the default method so let me override the method here called add formatter so let me hit command space and it is saying here override the method coming from the web MBC configure and I'll say hey overwrite this one okay so now let me delete it and let me say Here I am going to add the formatter with this registry reference right so I'll say what registry and dot add formatter and here I'm going to define my formatter so what is the formatter I'm going to add to my spring MVC application so this is the formatter class that I have created right phone number formatter so I know what here I'll just create the instance of phone number formatter and that's it you are done so right now whenever the spring MVC is going to read your configuration file it is going to find that you have defined some kind of for matter which is going to help you deem that data binding so when the spring is going to do the data binding and suppose it falls in some scenarios like this whenever it is going to fun to bind the data for the phone type at that time it is going to look for the phone number formatter and it is going to go into this particular class and it is going to use this particular past method and whatever the logic that we are going to write here according to that logic it is going to convert this particular object into the phone type or whatever the type that we want to convert the object to okay so before I write the logic here I have already given a breakpoint here and also what I will do here I'll go to my last calculator ask one take class and I'll give another sis out here just to see the flow when my debug line is getting appear in my console and I'll say inside add formatters method okay I'll go command S and now we can start our application and can check how the flow is going through and before that I'll go to my registration controller also so star registration controller so let me let me open my registration controller class and also here I am going to give the boppers line just to see when my sword registration phase method is getting called and when my process user registration method is getting called and also I'm going to remove this default data that I've set previously right previously I have said this defaults data right here right so I don't need it right now so let me remove it and here I'm just going to define SS out and I'll say inside store registration page method okay and also let me copy this and let me paste it here so this method will be executed whenever the data will be processed so let me do command free and let me say inside the process registration method and also I can give a break point here and I also give a break point here just to see the flow that when this particular method is getting executed when this particular method is getting executed and also whenever the passed method is getting executed okay so now let me do one work I'll make sure to start my server in debug mode so I'll go here and I'll stop my server and I will start my server in debug mode right so I can hit this button here so let me start my server cool so right now you can already see something in the console right so if you see the console right now or whenever the dispatcher servlet is gettin initialized you can see that I'm already getting this inside add for matters method so this is making sure that whenever my dispatcher servlet is getting initialized it is reading my configuration file and in my configuration file I have already defined this particular format called for number formatter so this is getting registered with my dispatcher servlet and this particular line here confirms that this particular methods has been executed and that's why we're getting this particular line in our console so now let me go to my application and let me refresh this particular page cool so something happened so right now I have started my server in debug perspective so I will do yes where the flow is going right now so the flow came into the so registration page because it is trying to reload this particular phase it is going to show my registration phase again because I have click the reload option here so you can see the page is still loading so that's why it is trying to show this particular user registration phase because we have fired us class register URL cool so right now let me resume my app and let me reload my particular pace and my pace got appeared here you can see there is no loading sign the page is already loaded now let's say if I'm going to enter my phone number is 9 1 8 8 8 8 8 8 and I will do register let's see where the flow is going and you can see that you can see that where is your flow right now so a program flow right now here inside the past method so before before the data binding happens so before the registration controller this particular methods get info before the data binding happens here user registration DT or DT of these things before the objects get created and before the data binding happens you can see that my flow is right now inside the phone number for water class and my passed method is getting executed so I have given a breakpoint here in the line number 21 inside the passed method so here my program flow stops but that's a good sign before the data binding happens here before this particular method executes my flow goes to my passed metal des confirms that depend write my logic here my program will work fine but right now I have not written any logic so let me do a resume okay so that it will go to the next breakpoint so let me do f6 and you can see now my program got terminated and I got some error right here the user registration field has some rejected value for the phone number and even even though I have a breakpoint here inside my registration controller process in the discretion method this particular method didn't happen the data binding also didn't happen right here that's because the default data binder first to bind our phone data with the respective type and we also got a very request 400 error in our web page okay so let me go here again and let me observe the console here so you can see the program flow so far first of all inside the slow registration method gets called which is this particular method this method is belongs to the registration controller class and whenever we fire the slash register method this particular output we got in our console because this particular page got loaded right and the next thing is whenever I click register so it goes to the pass method it went to this method and this particular line printed in the console right then the pass method executed but as we have not written any kind of logic here inside a pass method you know our data binding didn't happen and we got a full of bed requests okay so now let's go ahead and let's fix this bed request error so first of all let me minimize this particular thing and let me clear my console so that there will be nothing right here and we can firstly design our parse method right here and we can write our logic right now so before I write my logic here what I'm going to do here again I go to my previous space and I'll hit the register button and you know the moment I'm going to hit the register button my program flow will go to the pass method and see my program flowed I click em to my pass method and I already got a breakpoint here so the program stops here so the thing that I want to show you if I put my crosshair here you can see the parse method does a two argument one is text and one is local so basically for the conversion of the object we will be needing this text object which is basically the string object so if I put my cursor here you can see the text is actually the number that the user is entering so now the user has entered 9 1 - 8 8 8 8 whatever and that number I am getting here now let me do one work from this tree from this particular string let me separate this 91 and let me put it inside the respective property of my phone class and let me separate this 8 8 8 8 and let me put this particular value inside the respective property of my phone class right now you might be asking me that what is this local here so this local is basically used for the localization or the internationalization and we'll be learning the local stuff at that particular time whenever we are going to learn the internationalization stuff but for now you can understand like this locale is going to give you the feature to do the translation let's say if you want to show the text to the user in Chinese language or in French language then you can use this local object to convert our language currency whatever into a specific geographical location so this local is basically coming from Java dot util you can see and you can actually practice this locale class and you can actually do some resource on the local class in your free time but right now we don't care about it I only want to populate my text in English language my concern is here to convert this particular string so now let me do one okay first of all let me resume my program and I will be getting the error no problem let me write the logic for that right now so for now my first job is going to split this particular string that I am getting from my user so split the string received from the user okay so let me change this text to complete phone number okay so that it is going to be more readable so the next step is once I'm going to split this particular phone number so I will write extract the country code and set it to the one class country code property okay okay so first of all let me split the string here so this is the string that I will be getting from the user so the complete phone number dot split and I'm going to split my stream by using a hyphen because that's how the user is entering the data cool so once the splitting has been done I'll do a command one here and I will say assign the statement to a new local variable so the split method is basically returns a string array and let me say it the porns number array okay so before I write the logic here let me examine this particular array that how I am getting my values splitted and stored inside this particular array so I've already started my server in debugging mode so what I'll do here I'll go to this particular page and I'll again heat register okay so now my flow is again inside the past matters so first of all let me hit the step over okay now my flow comes here obviously this particular line is already there in my console okay so now again I'll do what again I do f6 here or I can do a step of her and now if I put my cursor here now see I've already got a string array here you can see I already get a stream array who says two indexes 0 & 1 in the Geo indexes I got 9 1 which is my country code and in 1 indexes I got my phone number so this is the string array we have two different objects whose got stored inside the string array so right now our job is we are going to extract the country code from the 0th index and we are going to extract the phone number from the first index and once we extract that we are going to set this particular string in our porn object so now what I'm going to do here I'm going to resume my program by hitting f8 okay first of all I'm going to create my foreign object okay and once my phone of this gets created I'm going to set the country code to my phone number so what is the country code obviously the country code is there inside this are a a command C I'll put command be here and as you have seen previously the country code is they are inside the 0th index right and similarly I'll do what I will again do fun dot set user number and where is my user number is getting stored again inside this particular array and I'll place there are a reference here and my user number is inside the first index so let me do command s so very simple logic so far right so first of all we are getting the user input which is the string and after that we are creating our own object and once the splitting is done here we are getting the data from this particular array one by one and we are setting it to our form object reference so in simple words we have converted the string object to a porn object and at last I am going to return this phone object right here so I have written this one object right here okay so now let's see our stuff is working or not so let me again start my server in debug mode and let's see what is the flow and are we getting our expected output right now and are we able to solve the bed request error okay my server is started now in debugging mode let me go to my console tab here and right now go again to my web browser and I will do register and let's see how the flow is going right now let me switch ok so again I came to the past method so let me do f6 or step over now the splitting will happen now I can see I already got my data in my array which is called for number array and right now I'm creating my phone class object because this is the class which is going to store my phone number now let me do f6 again and now actually I'm trying to set the country code by extracting the country code from the phone number array which is this one and I'm getting the 0th index from this particular array so if you want to see the value I can hit command shift I or control shift I and you can see the phone number rh 0th index value is nine one right so that will be get set to my phone object right now and again let me do f6 so it'll put my crosshair here you can see in my phone object I already got country code set here which is nine one now the usual number we need to set so let me execute this particular line again let me do f6 so you can see right now if I put my cursor here I got my user number set to my phone object as well which is eight eight eight eight whatever the user entered good so right now I have created my phone object I set my value now let me return my phone object here so let me do step over or f6 again and now let me resume this particular program and now see my program flow comes into the registration success URL because my formatter works good that's why the spring MVC is able to convert my string data to the phone data and right now the data binding happens fine so if you put the crosser inside this to the registration DT or right now let me put my cursor here now is this saying that I will not have any problem to bind the phone data with the communication details so inside the communication DT or you can open this you can see I have an object phone associated with this field called phone and they can open this you can see with this phone I have already got my data set to this particular property okay so the data binding happens to it obviously the email is empty this because we haven't provided the email venue right here that's why the DPOs email property is empty okay so right now if I will do F six okay so now you can see my inside process registration method get call and if I return my program or I'll press f8 right now you can see the data are there in the next page cool so I got my phone number right here but why I didn't get the dash right here this because if I'll go to my phone class right now if I'll go to the to string method see I have mr. dash here so I'll give a - I'll hit command s ok and now let me test this again so basically let me go to the first phase right now and here only first of all let me do a refresh I do not want the debug mode right now so I want to disable all these breakpoints so I will hit this button here that will skip all the breakpoints and I'll resume my program so now you can see inside the so registration method line is there inside the console and I got this particular page and right now let me say a nine one - two - three 3 four 4 five 5 six six and let me hit register and you can see this is there right 91 - and whatever the number that user has entered and if you see the console here let me go with the other perceptive and if I open the console here now you can see the console the program flow first our formatter method get called which is there inside our configuration file and then the so registration page method coil and then the past method of the for number formatter method got called and then the process user registration method which is this method which is actually showing this particular place that method got called and this lines are proving it ok so I believe that you have completed understanding the formatter parse method it's pretty useful is in it I believe that you guys are enjoying it and you know you guys already get started with the debugging stuff so in the next video we are going to you know bring in some advanced debugging concept right but before that follow along with me try to debug whatever I have debug so far you know follow along with me you know do it by your own and obviously I am going to teach you a lot of advanced stuff in the coming tutorials but this is the best right now you can create here a four part method you can also do debugging to understand the flow whatever I have explained if you are getting all these things in your local as well which you can replicate all these things okay let's have a look on that so take a break do practice and once you are done with the coding then who is stopping you to learn the print method so pause the video and once you resume it we are going to understand about the print method okay guys so I'll show you the jet plus security that I have for my camera see this tripod is broken okay this is a broken tripod if I will remove this bottle from here this is going to fell down and my camera will go so now this this tripod is actually not for this camera I lost my tripod see my jet plus security here if my camera is going to fall down this is going to fall on this pillow or this is going to fall on this comforter and this is locked down man I cannot go outside and cannot buy a tripod okay so please please please do it just with me for now okay and if it is going to fall down I am going to make all my courses paid and I'm going to recover the camera amount from you guys you guys need to buy me a camera okay now demand me to shoot a new video take care ok so now I understood how to create a formatter just like a phone number formatter and obviously I'm going to give you an assignment by the end of this tutorial but before that if you see here if you are implementing the formatter interface we will have both the parse method and the print method now you know that in we scenario you are going to use the parse method the parse method is actually going to help you to convert one datatype into another okay but what about the pink method so let us understand about that scenario okay in which scenario you are going to use this particular method for example now think that the user is basically submitted the form and this is the final output let's say let's imagine that he has filled in everything and he hits the submit button he did fill in everything right here so we get all his data then the formatting stuff happened the conversion happened and if you go to the registration controller the flow will obviously come to this particular page and here actually the data binding is going to be happened and just imagine that here we are going to serve the registration DT or object whatever the data that user has fully we're going to save DT or object into our database right imagine that I'm not going to write the code for this right now for the database code we are going to learn the springs are ABC soon but imagine that I have written some kind of database code right here and I am going to serve this DT or object you know in our database cool so now imagine that now if we're trying to give that feature to the user that he can actually edit his form okay so if the user has already registered with us and if he's coming to our registration pace we are going to pre populate all the data that he has served earlier so what we need to do we are going to load all the data that this particular user already has entered in the previous time so previously the user has fill in all the data and he did register with us so whenever we're going to show this particular form to our user here actually whenever the plus register requests the executor at the time what we need to do we need to load the save user data from the database got it so now let's just imagine that okay I have written some kind of code here which is going to fetch the set data big user and whenever the user registration pays will be shown whenever this particular page will be shown all the data we are going to retrieve from our database and we're going to pre-populate it right here so now just understand the situation right previously whenever the user has entered the phone number he has given us a string right and then we have used the call number formatter and we have converted the string to the for an object and we have served the phone object okay to our database and now if I were to see the registration controller here whenever this particular page will be lowered but willing to do willing to face all the data sent by the user previously the phone object will also be retrieved from the database right so for example right now I am NOT doing any database operation so let me manually the phone number and let's assume that that phone number has been retrieved from our database so what I'm going to do here that I'm going to create a foreign object let's have fun on equal to a new phone okay and imagine that this particular phone has a country code let me set the country code manually here 9:1 and let me set the phone number manually here less something let's say the user has entered to be three three four four whatever okay let me say two 2 three 3 four 4 five 5 six six okay this is the number that the user has entered previously and now let me add this one object to this DTO object okay so what I will do here DT or whose refers to my user registration DT or and inside the user registration DT oh I have the communication DT or and inside the communication DT or I have the poor okay so what I can do here I can come to the registration controller and I'll say DT or dot get communication DT oh and I want to set the phone number manually here I'm going to provide this for an object right here whatever I have created obviously the phone inside the detail was null previously so I have manually created a foreign object and I have said that particular foreign object no communication details okay to this particular DT or phone property okay so this particular phone I'm setting it manually right now now you can imagine that this particular thing happened through a database operation and we pasted this particular thing from the database and after that we have set it to our DT object okay so now let's taste it so whether we will be getting this particular number whenever our page loads right here or not so let me delete this particular number and let me refresh this particular place and let's see what is going to happen okay I have stopped my server so let me start my server I can start it in debug mode okay so we will not waste time so in the first shot only we are going to see everything in debug mode so let's see you know how the flow is going and whether I am able to see this particular thing in my phone number text box whenever the page loads so now what I'm going to do here you can see all my breakpoints are disabled so let me another display points here and now let me go to my solver and let me power this register URL okay so first of all let me switch the perceptive x' and you can see the flow came to my class register method now let me do step over and obviously in this particular stage if you have them to check my dt or object obviously there will not be any pending for my communication before you can see this is null but imagine that some kind of database operation happens and we have pasted the number from the database and the phone object got created the country code we have set so let me do f6 and here I'm going to set my phone number to my communication DTO right but but but I am going to get a null for an exception because I did some silly mistake so as you can see the user registration dto which is actually our DP or offset here the communication DTR has no value and it is M null value so this particular phrase it is going to give us null and over now if you are going to perform any operation obviously we'll get a null pointer exception okay so if I'll do command 6 I'll get a exception so you can see I have already come to the invocation target exception and I already came to exception so if I'll do f8 and or if I do resume this particular program I will be getting some exception that's because I have not set the communication dto object so I'll be getting a null pointer exception so you can see in the exception lock that I am getting a null pointer exception as the root cause okay so let me get to it at this particular error so let me go to the spring perceptive and I'll go to the controller and before I set my phone number what I will prove I will create a communication video object communication DT or communication DT or equal to new communication dt o and inside the communication DT or I have the phone number so I will do a set phone and I'm going to set this particular phone object right here and after that I am going to do here finally I'm going to set my communication DT or object to my registration DT or object so this DT oh okay which is my user registration DT or hits the set communication DT or here actually I am going to copy and paste my communication DT or object okay so now my communication DT or object has the phone number now I can expect that now this particular line should execute and I should not get a nullpointerexception so let my code gets deployed into my server and now let me go to the debug perceptive again and now let me go here and I will hit my URL here cool so right now I came again to this last register URL handler mapping and I'll do f6 so my foreign object god created my communication DT of the created my phone got set to my communication DT or and now if I do f6 here you can see there is no error and we got rid of this particular null pointer exception error so put my cursor here in my DTO which is my user registration DT or upset so you can see the user registers and ETO object is getting created here right we do not need to manually create our user registration DT offset spring will do user registration DT or DT or equal to new is your registration DT oh so the user registration DT or object get created by spring and then I have manually created the communication DTO object right here okay and after that if I will expand let me expand this and after that if I expand the communication DT or you can see I got my phone number set here so this particular phone number I had manually set it just right here and I have added that particular phone number to the communication DTO and then the communication DT or I have added it to my user registration DT oh so my values are set okay my default values are set with this DT or object cool so now if I do f6 again so now my flow is in the line number 33 and if I will execute this particular line I should be able to see my user registration page and I should be able to see this particular phone number in my phone number text box as a default value so let me resume this particular program and let's see if I will be able to see my registration page so let me so let me resume this and let me go over here and see here we've got a registration page but there is no phone number can as a default venue so now again the data binding didn't happen this because we cannot see the phone number just right here so the reason why the data binding didn't happen is if you see our controller in a controller we are creating a pod object this is the object right but here in the page we need to convert that particular object to a string and to do that and to display that particular upset as a string we need the print method so for that what I can do here I can go to my phone number formatter class and let me do something here let me check something here let me do s is out and let me say that inside the print method of the phone number formatter class right let me check when this particular method is getting executed and let me give a breakpoint here and let me again hit the particularly URL ok the floor came here let me stay and no this time so I'm inside this class register handler method let me do step of her and this gets printed inside so registration method which is this debug line now let me put a debug line right here and let me do resume so to come to the net debug line okay let me switch to the debug perceptive and now here we are setting the phone so let me do f6 okay now the communication d2 is set and now we are going to show the user this particular pace now again let me do it at 6 and okay so the source is not found let me do resume here and look at this where my flow came so now the full spring is going to show this particular place as it is not going to convert this particular object to a string object and the default data binding dint work the spring actually looked for the formatter and as we have add the phone number formatter it came into the print method so right now if I will do f6 you can see this particular line gets executed and if you see our console right now you can see inside the peek method of the phone number formatter so right now here inside the print method we need to write some logic to convert the phone object to the string object so that we can view it to the user right here so if I do f6 again and or resume again you can see the page got completely loaded and right now the phone number didn't appear here so now what I can do here I just can come here to the print method and right here I'll get my phone object right here okay so whatever the object that I am setting right here in my registration controller this particular phone object this particular object I'll be getting just right here and once I get that object what I can do here let me save porn okay and what I can do here I can say porn dot get country code Plus and I will keep a - here and I will do plus porn dot get user number and I am going to concatenate these two strings and I'm going to return it right let me remove this five spaces and let me return this particular thing okay so now if I'm going to deploy it again in my salsa now I'm expecting that my flow will again come to the print method so this particular thing will be executed and I will be getting the fully formatted phone number loaded to this particular phone number xbox cool so let me go here again let me do a refresh let me switch and now let me do f6 f6 f6 that we do step over step over step over step over and now this particular page will try to load data binding false and it will go to the phone number formatter let me do step over okay so for this it went to some particular class I am NOT interested about it so I'll just resume my program so that it will go to the next breakpoint okay so the next black point is here inside the phone number formatter so let me do f6 so now this particular line gets printed in the console whatever I have written here so if you can see here the phone number that we are getting has a country called nine one and the user number whatever we have set it in our controller so the get country code and the get user name will be executed this will be concatenated and this particular string will be returned to the view so let me do again f6 now actually I can dessous my program okay now let's see here and boom we got our paint method executed and it had bus to display whatever the default value that we have in our form object and we have actually converted the phone object to a string object okay and that's how the things work so if I will actually give a different number right now to two let's say three 3 four 4 five 5 six let me do register and boom so I get my phone number right here so it works in either way right now it is able to bind the data while it is getting loaded and whenever we are doing register it is actually able to convert the data from a string to a object type and it had us to guess the data binding done okay so this is why we're going to use the print method and this is why we are going to use the parse method okay [Music] okay so so far the code that I have written is a little buggy so this is going to break in most of the scenario okay so let's fix some bulk the code that I have written is not 100% correct the logic that I have written inside the parse method is not hundred percent correct so you are going to help me to improve the logic okay so here is just scenario that I'm going to tell you and I'm going to tell you how to fix that then I'm going to give you a different scenario and you have to fix the bug for that okay I try to utilize your you know enough brain power right now and less develop some small small logic and gradually we'll be better on that okay so I'm going to fix a bug right now and I'm going to give you another requirement so you are going to solve that one okay but it's my turn right now so let's pick some bug and let's improve power logic inside the [Music] okay so now let's go ahead and let's break this system okay so whatever the code that we have written inside the posh method of our formatter class let's break it okay because this code is not that stable so let me show you an example so if I'll go to my browser right now let me go back and here let's say the user is not providing the country code okay so if he is not providing the country code and directly trying to register then again we are getting a bad request okay so see here if I go back if I provide my country code and Appell to register the binding is happening fine but by mistake if the user is not providing the country code again our program breaks and why it is breaking poorly you guys have already guessed the thing okay so right now if you see the console here again you are getting a rejected value for the phone number okay and again it's saying type mismatch okay so when it is actually failing so to test this out let me give a breakpoint here inside the past method again of the for number formatter class and let me make sure that my server has started with the debugging mode that's pretty fine so now let me stop my program right here inside this particular method in the line number 23 and let's check it out where actually my program breaks and obviously something would have wrong here right I know where it is wrong but I just want to show because I never try a debugging in front of you so you guys will be familiar how to find box and how to fix it so it's a good time okay so now let me go ahead and let me go to the previous space and let me do register here cool so let me switch so my program flow came to the past method and let me do a f6 or a step-over right now okay so right now the splitting happened but here if you see once the splitting happened we have only one index okay we have only the 0th index and the 0th index has the complete phone number so in the line number 30 we are creating the phone object and if I will do a step over again if you'll see here we are setting the country code by fetching it from the phone number array of the G both index and if you know already the 0th index got this values not the country code is the phone number so right now my logic cells here isn't it so if you are going to execute this particular line you can do command shift I to see the value okay something wrong is there so what I can do here I can do right click and I can click on okay inspect is working okay inspect is not working for me but you can try inspecting do a control surprised so that you can see the value whatever the value this particular phone number is returning so the problem will start from the line number 31 right because here I am setting the country code and I'm getting the country code wrong right now if the user is not given the phone number in a valid format because right now we are getting the actual number not the country code as the first index now here my first of all my business logic fails here right but not the course so if I'll do f6 right now this line will execute fine that's because okay we already have something right here in the 0th index but wait now whenever we are actually trying to face the user number from the first index right now this audit doesn't have a first index so if I do f6 right now I should be going to the exception that's my expectation cool so as you can see right now we came to formatting conversion this particular class written by spring from out we don't care about it but we came to the cash blog and here they are actually throwing an illegal argument exception so if I'll do f6 again I'll go into this exception f6 again now again they keep throwing this they keep throwing this exception so if I resume my program at the end I'll be getting that particular exception right here in my console as you can see it has a rejected value so what I can do I can close this and let me switch to this okay let me do it work right now before I split the things okay I need to match sure the user has provided the country code okay and if the user is not providing the country code let me add some kind of default country code let's say the user is not given the country code here so the default country code will be 91 automatically ok the 91 will be automatically included before the phone number if the user is not providing the country code so how to do that so let me do one thing let me go to my code and here before I perform all these things I'll check for one thing so first of all here I'll check whether the number consists of - so if the number consists of a - I can be sure that okay this is a valid form the user is entering okay so there is no problem so I can execute this particular code safely but if the dash is not there if the hyphen is not found then r91 as the default country code okay so what we can be right now first of all how one will be check whether the number does contain a dash or not so to do that what I can do I can take this particular string command C and I can search for the index of the character - or - okay so right now you know that what is the return type of this index of method the index of method returns an int so if the string contains a dash character then the index of method is going to tell us the index the exact index where this particular - exists right but what if if the dash or the character is not present inside this particular number then the index of method is going to return me a minus one so if I'm going to see the description of this you can see it returns a minus one if the character does not occur let me do command one right here and assign it to a local variable let me tell it as index okay and now what I will do I'll have a check here if and I'll right here give the index equal to equal to minus one okay then here I have to do what I have to perform this thing quite like me to command X command V here so if our program is going to come into this a block then we will be sure that the dash is not found in the user input and we are going to arc nine one default country code to our phone number okay so let's do that okay so what I'm going to do here I am going to keep all this thing inside the s-block let's say else here and let me do close it right here okay so now we've got an another error here that's because the compiler doesn't have the access to this phone that's because we have defined this particular phone inside the else block so what I'm going to do here right now I can take this object I'll do command X and I can put it outside up the if/else block right so what I can do here I will test it just right here okay cool so right now there is no error here so if the index is -1 what I will do here let me copy this thing command C and let me do command V here and let me say set country code is 9 1 because that's the default country code that I'm going to set if a user doesn't have this - in his code and for the user number I can change it to phone number array of 0 but I am getting an error because it doesn't have access to this particular phone number because I am keeping it inside the else block so let me do one thing let me - command X okay and let me paste the splitting stuff right here outside of the if and else block and now the things will work fine okay so now let me do format this particular code because it's looking really bad right now that's nice right so here is the complete parts method it can right now have a look obviously we're going to make some more changes to it but this is what we have it right now cool so now let's go ahead and test this application so now let me assume that if the user is not giving the country core and hitting the register now let's see how my code is behaving so now let me switch to the debug or step tips again my flow came to the pass method let me do f6 now I can keep hitting f6 in my keyboard right you know that I'm hitting this step over button so let me do f6 so my program flow came to the line number 25 the foreign object got created now then the splitting happened and you can see right now as the user has not provided the country code we have only one index right here now let's go to the line number 30 here we are checking for the index of slash so if I go to f6 again and here you can see once this particular line got executed I got the indexes minus 1 that's because we do not have this particular character in our phone number string okay so now the code will go to the if lock and the country code will be set the user number will be set and if you see the phone object right now you can see we have 91 that is because we have set it manually because the user has not entered it and whatever the number that user enter we got it right here now if we do f6 again the else block will be skipped because the if objected and the phone number will be returned so now let me resume this program by clicking f8 and you can see right now there is no error and as the user has not entered the country code he got it by default okay so let me go back let me check with the country code let me get 9 2 here and let me do register let me disable my breath point let me do resume and you can see he got 92 right the problem will come whenever he will not have it whenever he will not have it we are going to add it manually okay so here is the result so I believe that you guys are practicing with me and I believe that you are 100% clear whatever I'm doing and you are trying to replicate all these things in your local so it's time to give you some assignment so I am going to give you two different assignment right now okay I am going to give you a bug a defect two picks and then I'm going to give you a story a user story to develop from scratch okay so now I'm going to give you a bug right now to fix let's fix a defect and here is the requirement okay so this is going to be the assignment number one okay and in this assignment we are going to fix a another defect and what about the bug fixes we have done so far that is pretty good but there will be one more scenario when we'll have a problem so see right here okay right now we have just done a bug fixes if there would be a number less there will be a number and if somebody is doing register without giving a country code this will work fine this is going to add a nine one for us cool but let me go back and here right now let me give something like this - and then I'll give my number okay let's say if the user is entering just like this right now let's see what is going to happen and right now let's see whether the nine one is going to be preceded before our number okay let me do a register and you can see I got the numbers just like this there is no nine one proceeded before this number so you have to fix this bug you have to fix this defect so the requirement is if the user is trying to trigger application and if he is not giving a country code and if his number started with that - in this scenario also we have to add in nine one right here whenever he is submitting his form right his number should be nine one there should be by default and then he should get this number okay so you have to fix the chord okay you have to pick something inside the parse method so what he can do is you can put a breakpoint here in the line 23 go ahead and try debugging okay but you have to do code send this here inside the parse method and let me tell you that I am NOT going to tell you the solution right now how you are going to fix the spark okay if the user is not entering the country code okay I should get the country code in the next page right here cool so right now you can pause this video and you can look for a solution and try debugging just see what is wrong here inside the parse method and how you are going to fix it now let me give you the solution I am going to fix this code right now but pause this video do not see the solution try it by yourself and in case if you are not able to do it see the solution see the sample code but I am damn sure you will be able to solve this thing so this is the challenge for you solve the defect you okay so now this is gonna be the solution for the scenario that I have given to you okay I have tried to make this code as simple as I can and also I have written some comment for your understanding but I am NOT going to explain to you this code so see this code understand this and you just make your changes in case you were not able to do it by yourself so the only thing that I did right here I did include this thing this phrase in my if check okay so I just use the string class start with method in couple of places just to check whether our number is getting started with with a - okay and if so then I have just written my logic right here okay so I think you can understand this it's pretty straightforward come on give it a try and go ahead and make the changes and make it work by seeing the solutions but do not try to copy it debug the thing understand it and in case if you can make it more better and in case if you can make it more stable then you're welcome go ahead and make change this and make this code better cool so make sure you understand the requirement and right now you are going to fix the bug and once you fix it I'm going to give you some requirements and you are going to develop the requirement from the scratch so this is going to be the assignment - and this is not a defect this is a story okay so listen to the user story and do the development for the same okay right now is your turn to practice an assignment so you have to basically design this particular screen so as you can see this is a bill screen it will have a card number a credit card number and you will accept the credit card number in this format okay and also there will be a amount text box so if you see the credit card number you will get the credit card number in this format so the user has to enter the credit card number with a dash and this should be 16 digits and in the amount here to define 10 euro or 10 USD or 10 pound whatever with a space here once you fill in all the detail and if we click pay bill it should go to the next page and here we'll be getting a message called bill against the credit number this whatever the user entered for an amount this has been generated but if you see here distain euro this is not the string euro this is the Euro symbol right so if you see the amount the user has entered here is in string but we are converting this euro string to this okay so here is an another example so the user will enter the credit card number and the amount let's say he has entered 10 USD and he click on pay bill so this is the format that he will be seeing the message in the next form or in the submission form okay so this is the screen number 2 cool so you have to write two different formatter here the first formatter will be the credit card for mater and the next formatter will be the currency formatter and also I have written locale here and why I've written locale here because we are going to use the locale class to do the internationalization or to convert the currency type into this particular form while displaying the message okay so I'll give you in another example but here is the thing we'll write a credit card class and we'll write an amount class if you see the credit card class has first four digits second four days a third four digit and the last four digit so this is how my data's are going to be binded and for the amount we'll have the bill amount whatever the amount that user is going to enter and the next one will be the locale definition so if he is entering USD we need to bind the USD with this locale definition or if he's entering the pound then we need to bind the pound in string format with this field okay so this will be our main class called class bill and this bill will have two dependencies called credit card and the amount which is going to be this and this so this is going be your class hierarchy or this is gonna be your project architecture the new project that you are going to build for the assignment cool so right now here is the currency for mater that you are going to write I know the crater formatter is pretty simple and you have a in the example during the time I have developed the phone for matter but here the currency for matter the way you are going to write it you'll have the print class and you'll have the first class and you are going to use this locale class right here to display or to convert the UST to the symbol dollar right or to form either 10 euro to this in euro you will be needing the locale class and using the local class you can format this USD euro to this particular type and that is the thing that you are going to research by yourself okay so for now you'll be write a formatter for the credit card you'll be write a formatter for the amount this will be pretty straightforward and for this you need to research a little how to use the local class if you need help you can ask me in selenium Express support Facebook group or you can paste your code right there if you're facing an issue and will be solving there but you go ahead and do some research on the locale class and this is the assignment you have to develop and send this assignment to selenium Express at the rate gmail.com and I'll be checking your assignment right there and if you have successfully retained the format afore the credit card in the amount then please paste your score screenshot in the selenium Express Facebook support group so that other people will be also get benefitted by saying your core and they'll have some ideas but before seeing anybody score I'll definitely appreciate you to try this exercise by yourself which is going to give you a good hands-on on this concept now let's go ahead ok so I believe all is well with you and right now before we wrap up this section you know let's wrap it off with a twist okay there is one thing you still didn't experience it and right now let's see a scenario and this is going to blow your mind and remember I have a epic you included with this section so don't forget to comment the take you don't forget to answer my question in the comment section so let's go for it right now [Music] okay so before we move to the next section I want to show you one last thing and I want to give you a puzzle right now okay just look at here let me go to my registration URL if I do enter I'll be getting my registration page and as you can see there are some log in the the first line coming from the handler method which is handling this class register URL pattern and the second line here confirms that my preet method of my phone number formatter gets called that's because there are some default value attached to my phone number and it has to be displayed in the right format okay that's cool so right now whenever we'll be doing a register there will be two more methods we get called right here as you can see the parse method of the phone number formatter get called and this log is coming from my user registration controller handler method which is handling this slash registration success URL cool so here is a question for you if I go back and if I do not provide any phone number right here obviously right now the value is null for this particular phone number field and if I'm going to do register then what is going to happen will my core break so the reason why I'm asking you this as you can see in the log the process user registration method the handler method if I do command cept our star controller I'll go to the registration controller as you can see this method right inside process user registration method right this particular log confirms that before my flow comes to this particular method the flow is going to the phone number let me do is last formatter the phone formatter dot Java let me open this the flow is going to the parse method of the phone number formatter class so here is the question so if I am NOT entering any value right here let's say the user is not entering any value right here and he is clicking register then obviously the flow will go to the phone number formatter parse method and here you can see this complete phone number will be null and if this will be null here in the line number twenty seven I am doing some silly kind of thing right I am using complete phone number dot split and this value will be null right and over nollie while doing any kind of operation I should be getting a null pointer exception I have not given any null check for this particular line which is bad so my code should break in the line number twenty seven and I should be getting a null pointer exception because of the null value of the complete phone number so let's see whether this is happening let me clear my console here and right now let me go to the browser and I will not be giving any value right here and I'll be hitting register and what happened so there is two thing my core didn't break okay and you can see something right here inside process user registration method this is kind of surprising isn't it why the log which is there inside the parts method of my phone number formatter which is this inside the parse method of the phone number formatter this log I cannot see here in my console now which confirms that this particular method did get executed so if I go back let me go back to this and let me go back again and let me clear the console and let me hear something 9/2 and just let me give a random phone number right here and let me do a register and see here I got my phone number right here which is completely formatted and also the law confirms that first my passed method gets executed and then the handler method gets executed correct but hold on if I am NOT giving any value right here and if I'm doing register mark here that the inside pass method of the phone number formatter didn't get called whenever I hit the submit button isn't it so here is a takeaway if you are not providing any value right here inside the phone number text box and if you're hitting register one number formatter parse method here the flow is not going okay this thing this pass method or this print method will only come to picture whenever there is a need to format something but in this case if I am NOT giving any value right here then there is no need of a formatter that's because there is no value then why should under Cerie are the formatter paint method or pass method will be get called so remember that only when there is some value inside the phone number at that time the formatter will be get caught you okay so now let's go ahead and let's validate this particular phone number field what if I have a business rule if there will be no value inside the phone number text box then the user should not able to register the form in this case I am going to the next page and there is no error message right here so I want to validate my phone number field and I want to do some form validation and in case there is no number right here I should give some error message right here that hey the phone number cannot be blank so let's do it okay so congratulation for completing this session and right now it's my turn I need to edit the video and I need to create a new one okay and I'll be back pretty pretty soon okay I'll be back within a week nobody buy the next five days or buy the next seven days for sure I'm going to post a new video and we are going to write our custom validation annotation okay our custom annotation to validate the form okay you remember the home validation we have used at not blank and size and all this annotation how to create one of the annotation by yourself okay we will create some custom validation constraint by ourself and that is going to be pretty much important because in the interview they're going to ask you about okay so keep waiting because in the next video we will create our own annotation our custom annotation which is pretty much important for a real-time project will be creating them in the next video bye artha okay now help from anybody so I'll see you in the next video till then bye bye take care and happy coding well let me tell you that don't forget to write your doubt in the selenium express support group if you have any second thing you can follow me on the instagram promoting my Instagram account because I have just created it okay you can follow me on instagram Abhilash at funk right no no selenium underscore express that's my inst ID go there and follow it and you are going to get all this courses pretty soon live selenium express.com so their side is going to be back soon I'm working pretty hard for that okay you can experience all these courses right there with quizzes and materials cool so that's what I want to say hopefully you guys will have a good day take care bye bye see you in the next video [Music]
Info
Channel: Selenium Express
Views: 14,661
Rating: undefined out of 5
Keywords: nested objects spring, spring mvc course, spring mvc by selenium express, spring mvc formatter, type mismatch spring mvc, data binding spring mvc, spring mvc object converter, spring mvc converter, spring mvc printer, spring mvc parser, spring by selenium express, eclipse debugger tutorial, debugging spring boot application in sts, spring boot by selenium express, spring formatter vs converter, spring converter, field formatter in spring, spring boot
Id: q9hjjeJA7Yw
Channel Id: undefined
Length: 114min 21sec (6861 seconds)
Published: Sat May 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.