Announcement Announcement Module
No announcement yet.
Forms and Image Buttons Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.


  • #2
    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:
    <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

      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?



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

        <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
        <form name="aForm" method="POST">
          <input type="hidden" name="submitType"/>
        <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
          Also see my last post (as of now) in this thread:

          Last edited by robyn; May 14th, 2006, 05:47 PM.