Results 1 to 5 of 5

Thread: Forms and Image Buttons

  1. #1
    Join Date
    Sep 2004
    Location
    West Palm Beach, FL, USA
    Posts
    13

    Default Forms and Image Buttons

    This seems like someone should have asked this question before, but I couldn't find anything like it in the forum so forgive me if I'm asking a redundant question.

    I'm using SimpleFormController as a controller for a form with a few fields. The only problem is, I have two image buttons. One is supposed to act as the "submit" button, and the other button does something else. In SimpleFormController, there is a onSubmit method which gets called when the "submit" button is hit on the form.

    How can I use the image button to simulate a submit button? Or if I click the other button, how does that map to any function in my SimpleFormController? In other words, how can I detect which image button was clicked in the onSubmit method of SimpleFormController or if that's not possible, in SimpleFormController itself.

    --JS

  2. #2
    Join Date
    Aug 2004
    Location
    Auburn, AL, USA.
    Posts
    106

    Default

    First, unless you customize a button of type image it will act as a submit button.

    If the second button is supposed to trigger something server-side (that is you are submitting the form but doing something other than the default submit processing) then look at the org.springframework.web.servlet.mvc.multiaction package. I would probably name the two buttons the same, but give them different values; like:
    Code:
    <input type="image" src="image1.jpg" name="action" value="doSomethingSpecial"/>
    <input type="image" src="image2.jpg" name="action" value="default"/>
    On the other hand, if the second button is doing something client-side (like reseting the form, or running some javascript) then you will need to add an onclick event handler to the second input, just return false from the handler to cancle the submit.

  3. #3
    Join Date
    Sep 2004
    Location
    West Palm Beach, FL, USA
    Posts
    13

    Default Thanks.

    Ok, I see. Thanks for the reply. Now...if I use a MultiActionController as opposed to a SimpleFormController, do I have to forfeit all of the functionality that the SimpleFormController provides for me, eg. like formBackingObject, etc. Is there a strategy to combine the two?

    --JS

  4. #4
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    An alternative approach (PS while I don't recommend much use of Javascript, I've found this approach good - and testable with HttpUnit):

    Code:
    <script language="Javascript">
    <!--
    function doSubmit&#40;submitType&#41; &#123;
      document.forms&#91;"aForm"&#93;.elements&#91;"submitType"&#93;.value = submitType;
      if &#40;submitType=="Button2"&#41; &#123;
        //Do something extra
        //To change controller handler you need to change the action
        //e.g. document.forms&#91;"aForm"&#93;.action = new URL
      &#125;
      document.forms&#91;"aForm"&#93;.submit&#40;&#41;;
    &#125;
    //-->
    </script>
    </head>
    <body>
    ...
    <form name="aForm" method="POST">
      <input type="hidden" name="submitType"/>
    </form>
    <a href="javascript&#58;doSubmit&#40;'Button1'&#41;"><img ... border="0" /></a>
    <a href="javascript&#58;doSubmit&#40;'Button2'&#41;"><img ... border="0" /></a>

  5. #5
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Also see my last post (as of now) in this thread:

    http://forum.springframework.org/showthread.php?t=10520
    Last edited by robyn; May 14th, 2006 at 04:47 PM.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Building complex forms
    By Scott Tavares in forum Swing
    Replies: 10
    Last Post: Jun 7th, 2005, 09:32 AM

Posting Permissions

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