Question re. ch13-bankapp example and @ModelAttribute annotated methods

16 views
Skip to first unread message

Suresh M

unread,
Dec 6, 2016, 7:14:01 AM12/6/16
to Getting started with Spring Framework
Hi,

Hope someone can help with this:

In chapter 13 Validation and Data Binding in Spring MVC, in example ch13-bankapp if I run the following test:

1. see the list of deposits

2. Click "Create New Fixed Deposit"

3. Enter deposit details and click "Save"

then I get the following console messages:

A - getNewFixedDepositDetails() method: Returning a new instance of FixedDepositDetails
B- openFixedDeposit() method: Fixed deposit details successfully saved. Redirecting to show the list of fixed deposits
... 2 more messages

Console messages A and B show that on clicking "Save", the getNewFixedDepositDetails() method populates an object called "newFixedDepositDetails" into the Model object, and then immediately afterwards the openFixedDeposit() method runs and
uses the @ModelAttribute argument annotation to get the "newFixedDepositDetails" object from the Model and put it into an object called fixedDepositDetails.

Question: why doesn't the "newFixedDepositDetails" object retrieved from the Model by openFixedDeposit() contain the dummy data "newFixedDepositDetails" created by the previously called getNewFixedDepositDetails() ?

I would imagine that when you click "Save", the browser returns data to the server, and the instance of "newFixedDepositDetails" Model attribute that contains user-entered data is overwritten by "newFixedDepositDetails" written by getNewFixedDepositDetails() ?

Clearly the software works as intended, so I have to assume that getNewFixedDepositDetails() does not write "newFixedDepositDetails" to the Model if the Model already contains an attribute called "newFixedDepositDetails". Is this correct?

Thank you.

Suresh M

unread,
Dec 6, 2016, 11:24:29 AM12/6/16
to Getting started with Spring Framework
ok, I think what's happening (?) is:

when you click "Save" on the New Deposit web page:

  • the getNewFixedDepositDetails() method runs and puts an object called "newFixedDepositDetails" with initial data ("enter a valid email" etc) into the Model object,
  • the openFixedDeposit() method runs and binds request parameters to the fields of the "newFixedDepositDetails" Model attribute created in getNewFixedDepositDetails().


Ashish Sarin

unread,
Dec 6, 2016, 10:20:36 PM12/6/16
to Suresh M, Getting started with Spring Framework
Your understanding is right. The openFixedDeposit method is called immediately after the getNewFixedDepositDetails method. 

--
You received this message because you are subscribed to the Google Groups "Getting started with Spring Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to getting-started-with-spring-framework+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages