05. Using @ModelAttribute and Binding Results

14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
Already have an account? Log In

Transcript

Inefficient, let us learn about our generate model attribute annotation and binding the self. accurate model attribute annotation can be added on methods or on method arguments. I have given two examples over here, the first one, the annotation has added a border method. In the second example, the annotation is added on a method argument. What is the purpose of address model attribute? It is used for binding the RS method argument.

Our method return type to one name that we viewed. In the previous example, when we want to give a name for the model object, we use model map r we can go in for model we just give more up I mean, we just added math.io attribute, the dummy name, employee comma the object. Instead of using model, we can go in for model attribute, we're binding this employee model to the GMP model attribute. This EMP can be retrieved in the view page. And in this example, we have got a call manager dot find account what is the return type and account object. This account object is bound to the model attribute.

Here we have not given any name. If we don't give any name camel case or the lower case of that class will be taken into consideration. So the model attribute name here is a code. So model attribute is used for binding the method arguments are the method return type to a name that reviewed you can retrieve the model of In the view page using this named attribute, let us discuss how this model attribute is used on method arguments. In the previous program, we were trying to retrieve the form field values using request Param. And these were assigned to the method parameters.

Once the values are available in method parameters, we created a model object employee object and the values were assigned to the instance variable, employee employee equal to then we were appending it to the instance variables here. That's what I'm saying. These are prom seals are saying to method parameters. From the method parameters we are assigning it to the model object. This is what we have done the same method here I'm using model attribute EMP employee when this method is getting called Sunday from from Sunday for paid the The value for this argument is taken from the model, the model is nothing but the employee class. Spring checks whether you are having a model with instance variables that are similar to the farm, if you are a farmer having four form fields, your model also should have the same number of instance variables with the same name.

So what actually it will do spring will create the objects and call all the setter methods. So, you don't have to go in for request for with a request model object that is in our case employee objects, and assign the values for the instance variables from the form field values. So this employee is loaded with the form field values. You don't have to use record parents and you don't have to call the getter setter method who's creating the object spring once object is created, now you need to send it to the Explain to the Wii U PE with a dummy name. So, for that we are using model attribute model attribute is equal to model that add attribute EMP comma employee. If you give this model attribute in that scenario, the model name or the dummy name will be employee only.

So, the number of lines of code that you have returned is completely reduced. This is our method argument. Similarly, you can add model attribute a border method. Again your answer you are just doing it only for the objects here the object is a code object. What is the dummy name here? Because you're not given any name.

The model name or the dummy name is lowercase r code when you're trying to use model attributes or border method, in that case, these methods will be called even before request mapping. That is dogs Already that the opponent object is ready with a name attribute key in this case it is a code. Depending upon your requirements, you can use model attributes either with about a method or on a method argument. Now, what we will do with will go back to eclipse, we will modify the same program that we have done earlier, I am an eclipse we are going to work on the same application only I have got this add employee method, we need to modify this method. I don't want to disturb this method. So, let me just copy the same method and I will comment it Ctrl V. Now I don't need any request Param and model map.

So, the method is without any parameters. Let me remove all these coding inside this add employee method. This add employee is just going to have employees parameter. Let me try to print the employee object Okay. Now, what is the flow first actually it is going to EMP form dot JSP when you click Submit button it'll enter into this particular method here you have got employee. So, it is a responsibility of spring framework to create the employee objects I mean take all the values of the form fields and call the setter methods so that the instance variables are populated.

When you try to print the employee object, it has to print the employee object properly with all the values how actually spring will create the employee objects only if the class is annotated with at the rate employee or the rate component. So I am just annotating this class with a direct component. Okay, now, let me run this application. I have not gone in for this model attribute because I have not used modeling attribute what will be the model name or the dummy name the same name as that of the method argument it will be employee only. Let me run spring MVC forms. Home dot JSP page has come let me click the link this is show from the JSP if we add the Data Click Add employee now I'm going to search from EMP success dot JSP here I am trying to print employee What is this?

This is a name which am getting some EMP controller This is the model name. We have not used model attributes labor we have used model map or model and view our model automatically it is done by spring. So, spring creates the employee object and cause the setter method to populate the instance variable so the form field values Now I will show you the difference. Let me add a threat model attribute I will give it the file name EMP. Let me save this I'll rerun the application we are trying to retrieve only employees but what is the model name? We you have changed the model name to EMP it will not give any output let us check that out in a status.

Let me click the link father data to default let me click Add employee. Now I am not getting the output Why? Because in EMP controller I have given the model name of EMP, this is equal to map dot add attribute EMP comma employee for the employee object or for the employee model object you are giving the model dummy name of EMP, so how will you retrieve it? You have to retrieve it using this EMP only now I will just change it. Save this. I'll come back to you Let me refresh.

Now I'm getting the output can you understand? So, our direct model attribute is used to bind this method argument to a named attribute in this case employee object was bound to this EMP domain name, this is about model attributes. Now, let us talk about binding research before moving to binding the cell. Let us also talk about data binding. What is data binding we are whatever we have done till now is called as data binding that is, spring is trying to create a model object at bind the form field values to the instance variables. This concept is called as data binding data binding can be done it can be done in different ways.

It can be done using session attributes using model attribute are using the default constructor. So, what we are seeing is using the defaults on sticker spring is creating an object and calling the setter method it is not retrieving the object from the model attributes are it is not retrieving the objects from session attributes going forward we will be seeing how to retrieve it from the session attributes and obtain values to that particular object. So, data binding is the main concept in spring wherein, the model object is assigned values from the form field values, the request parameters are bound to the instance variables of the model. Now, let us talk about binding which says what is binding this well binding the site is used to hold the result of a validation that is, once data binding is done by spring validation happens, what kind of validation checking in for the type conversion if it is not happening in that scenario, it is an error.

These kind of validation errors are taken chat by this binding reseller going forward, we will be doing validation of this form, what kind of validation checking in for the length of the name checking in for the password pattern checking in for the age limit, then the length of the phone number checking whether it is a proper email all these are valid validations if the user has not entered these data properly, in that scenario validation errors occur all these kind of validation errors or take out all these the sense of validation is taken care by this binding the cells binding the cells will hold the result of the validation. Now, to start with let us see a simple type conversion. We have salary in care in salary you we are trying to enter a string instead of a number Type conversion error occurs This is taken care by binding result.

Let me show you that I am working on the same application the same answer I am going to use the same method. Let me copy this and commented when you're going to use binding resell binding the cells should be added next to the model attribute parameters. So, in our case, it was the employee table. Now, whenever we wanted to add parameters to the method, we added it in whichever way we want, but if you want to use binding results, it has to be added next to the parameter that is annotated with model attributes. The reason is bind will be called only after the data binding is completed. Now let me add this binding result as parameters.

This is the second parameter. Here I can check you Result dot has yours you are checking whether there are errors if there are errors are there automatically it will be added to this binding result objects. Here I am trying to use half errors method I am checking whether the binding result is having the result of validation it will have results only if there are errors. So, I'm just using this method it returns a Boolean value. If it is true then it'll enter into this leaf loop. Now what I need to do if something goes wrong I have to stay back in the same page.

What is that page EMP form? So I am just going to give return EMP form. Just understand the logic you are moving from EMP form to add employee. Okay, so if something goes wrong, it has to stay back in EMP form dot JSP only if everything is perfect, proceed for To EMP success dot JSP. Now let me save this. Let me run the application interesting for all the three year type conversion error occurs, let me click Add employees it is staying back in the same page.

It is because of the year. If you want to still check on this, what it can do, we can just try to get whatever errors have occurred. It is result dot file errors. Let me save this. I'll go back and I will refresh that string add employee. I'll open this console.

Here you can see the error part. Can you see this. So with binding results, you can get them you can capture the validation errors and make the user steps stay back on the same page only. So this is about model attribute and binding result. Thank you

Sign Up

Share

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