Why method annotated with @ModelAttribute could not pick up model attributes added through view










0















Why didn't the model.asMap() in method annotated with @ModelAttribute (i.e AM1 here) didn't return any model data but in the @RequestMapping(i.e HM1 here) it does.



View:



<form:form action="SignupValidate.htm" modelAttribute="ma_CustomerTO">
CustName:<form:input path="CustName" />
CustSpouse:<form:input path="CustSpouse" />


I have submitted the form with fields values CustName=abc CustSpouse=xyz



Contoller:



@Controller
@RequestMapping("/Customer")
public class CustomerController
//-------------------------------------//
@ModelAttribute("ma_CustomerTO")
public CustomerTO AM1(Model model)
logger.debug(model.asMap()); //-----> does not print the model attributes from view???
CustomerTO customer=new CustomerTO();
customer.setCustName("Sheldon");
customer.setCustSpouse("Amy");
logger.debug(model.asMap());
return customer;

//-------------------------------------//
@RequestMapping("/SignupValidate.htm")
public ModelAndView HM1(Model model,@ModelAttribute("ma_CustomerTO") CustomerTO customer)
logger.debug(model.asMap());//-----> but this one prints











share|improve this question




























    0















    Why didn't the model.asMap() in method annotated with @ModelAttribute (i.e AM1 here) didn't return any model data but in the @RequestMapping(i.e HM1 here) it does.



    View:



    <form:form action="SignupValidate.htm" modelAttribute="ma_CustomerTO">
    CustName:<form:input path="CustName" />
    CustSpouse:<form:input path="CustSpouse" />


    I have submitted the form with fields values CustName=abc CustSpouse=xyz



    Contoller:



    @Controller
    @RequestMapping("/Customer")
    public class CustomerController
    //-------------------------------------//
    @ModelAttribute("ma_CustomerTO")
    public CustomerTO AM1(Model model)
    logger.debug(model.asMap()); //-----> does not print the model attributes from view???
    CustomerTO customer=new CustomerTO();
    customer.setCustName("Sheldon");
    customer.setCustSpouse("Amy");
    logger.debug(model.asMap());
    return customer;

    //-------------------------------------//
    @RequestMapping("/SignupValidate.htm")
    public ModelAndView HM1(Model model,@ModelAttribute("ma_CustomerTO") CustomerTO customer)
    logger.debug(model.asMap());//-----> but this one prints











    share|improve this question


























      0












      0








      0








      Why didn't the model.asMap() in method annotated with @ModelAttribute (i.e AM1 here) didn't return any model data but in the @RequestMapping(i.e HM1 here) it does.



      View:



      <form:form action="SignupValidate.htm" modelAttribute="ma_CustomerTO">
      CustName:<form:input path="CustName" />
      CustSpouse:<form:input path="CustSpouse" />


      I have submitted the form with fields values CustName=abc CustSpouse=xyz



      Contoller:



      @Controller
      @RequestMapping("/Customer")
      public class CustomerController
      //-------------------------------------//
      @ModelAttribute("ma_CustomerTO")
      public CustomerTO AM1(Model model)
      logger.debug(model.asMap()); //-----> does not print the model attributes from view???
      CustomerTO customer=new CustomerTO();
      customer.setCustName("Sheldon");
      customer.setCustSpouse("Amy");
      logger.debug(model.asMap());
      return customer;

      //-------------------------------------//
      @RequestMapping("/SignupValidate.htm")
      public ModelAndView HM1(Model model,@ModelAttribute("ma_CustomerTO") CustomerTO customer)
      logger.debug(model.asMap());//-----> but this one prints











      share|improve this question
















      Why didn't the model.asMap() in method annotated with @ModelAttribute (i.e AM1 here) didn't return any model data but in the @RequestMapping(i.e HM1 here) it does.



      View:



      <form:form action="SignupValidate.htm" modelAttribute="ma_CustomerTO">
      CustName:<form:input path="CustName" />
      CustSpouse:<form:input path="CustSpouse" />


      I have submitted the form with fields values CustName=abc CustSpouse=xyz



      Contoller:



      @Controller
      @RequestMapping("/Customer")
      public class CustomerController
      //-------------------------------------//
      @ModelAttribute("ma_CustomerTO")
      public CustomerTO AM1(Model model)
      logger.debug(model.asMap()); //-----> does not print the model attributes from view???
      CustomerTO customer=new CustomerTO();
      customer.setCustName("Sheldon");
      customer.setCustSpouse("Amy");
      logger.debug(model.asMap());
      return customer;

      //-------------------------------------//
      @RequestMapping("/SignupValidate.htm")
      public ModelAndView HM1(Model model,@ModelAttribute("ma_CustomerTO") CustomerTO customer)
      logger.debug(model.asMap());//-----> but this one prints








      spring-mvc model modelattribute spring-form






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 9:12









      nguyentaijs

      908




      908










      asked Oct 20 '18 at 11:16









      sql_dummysql_dummy

      237213




      237213






















          2 Answers
          2






          active

          oldest

          votes


















          0














          I have think possibly this should be happening. The @ModelAttribute on method would not create a attribute for model until the end of the method.






          share|improve this answer






























            0














            Every method with @ModelAttribute annotated will be called before receiving request from client in order to initialize model for your view. That's the use of method leveled @ModelAttribute.



            The model in your @ModelAttribute method doesn't know about ma_CustomerTO because it is called before your SignupValidate.htm method is processed






            share|improve this answer






















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52905049%2fwhy-method-annotated-with-modelattribute-could-not-pick-up-model-attributes-add%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I have think possibly this should be happening. The @ModelAttribute on method would not create a attribute for model until the end of the method.






              share|improve this answer



























                0














                I have think possibly this should be happening. The @ModelAttribute on method would not create a attribute for model until the end of the method.






                share|improve this answer

























                  0












                  0








                  0







                  I have think possibly this should be happening. The @ModelAttribute on method would not create a attribute for model until the end of the method.






                  share|improve this answer













                  I have think possibly this should be happening. The @ModelAttribute on method would not create a attribute for model until the end of the method.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 20 '18 at 19:39









                  sql_dummysql_dummy

                  237213




                  237213























                      0














                      Every method with @ModelAttribute annotated will be called before receiving request from client in order to initialize model for your view. That's the use of method leveled @ModelAttribute.



                      The model in your @ModelAttribute method doesn't know about ma_CustomerTO because it is called before your SignupValidate.htm method is processed






                      share|improve this answer



























                        0














                        Every method with @ModelAttribute annotated will be called before receiving request from client in order to initialize model for your view. That's the use of method leveled @ModelAttribute.



                        The model in your @ModelAttribute method doesn't know about ma_CustomerTO because it is called before your SignupValidate.htm method is processed






                        share|improve this answer

























                          0












                          0








                          0







                          Every method with @ModelAttribute annotated will be called before receiving request from client in order to initialize model for your view. That's the use of method leveled @ModelAttribute.



                          The model in your @ModelAttribute method doesn't know about ma_CustomerTO because it is called before your SignupValidate.htm method is processed






                          share|improve this answer













                          Every method with @ModelAttribute annotated will be called before receiving request from client in order to initialize model for your view. That's the use of method leveled @ModelAttribute.



                          The model in your @ModelAttribute method doesn't know about ma_CustomerTO because it is called before your SignupValidate.htm method is processed







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 8:18









                          nguyentaijsnguyentaijs

                          908




                          908



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52905049%2fwhy-method-annotated-with-modelattribute-could-not-pick-up-model-attributes-add%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3

                              Museum of Modern and Contemporary Art of Trento and Rovereto