Results 1 to 6 of 6

Thread: Spring Form : Cancel button should not set properties

  1. #1
    Join Date
    Jan 2012
    Posts
    22

    Question Spring Form : Cancel button should not set properties

    Hi,

    i have JSP page as below.
    Code:
    <form:form commandName="user" methd="POST">
       <form:input path="name" />
       <input type="submit" name="btnSave" value="Save"/>
       <input type="submit" name="btnCancel" value="Cancel"/>
    </form:form>
    Controller
    Code:
    public string saveForm(@ModelAttribute User user,Model map)
    {
          System.out.println("Name = "+user.getName());
          return "UserForm";
    }
    When i enter name and click "Save" button. "name" property of "User" bean is saved. But when i press "Cancel" button it shows same behavior and set "name" property with changes made.

    But as per my requirement "Cancel" button should not set properties and it just redirects to the mentioned page.

    Please help.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Then don't... The cancel button should invoke a different method which doesn't have a @ModelAttribute annotated parameter.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2012
    Posts
    22

    Smile

    Quote Originally Posted by Marten Deinum View Post
    Then don't... The cancel button should invoke a different method which doesn't have a @ModelAttribute annotated parameter.
    It means that if we specify the @ModelAttribute in controller's method then it would set the properties otherwise not.
    Please confirm.

    Thank you very much.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    If you put an arbitrary object in or annotate something with @ModelAttribute it will be used for binding. I suggest a read of the web chapter of the spring reference guide which explains all this.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jan 2012
    Posts
    22

    Default

    Sure i will... Thanks a lot Marten.

  6. #6
    Join Date
    Aug 2011
    Posts
    4

    Default

    I think the controller method "saveForm" is called because "Cancel" button is also of type "submit". So, It will submit the form to the controller. I think if you can change the type of "Cancel" button to "button" and add onclick listener, the current behavior can be modified.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •