Announcement Announcement Module
Collapse
No announcement yet.
Forms and Image Buttons Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.

    --JS

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

    Comment


    • #3
      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

      Comment


      • #4
        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>

        Comment


        • #5
          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, 04:47 PM.

          Comment

          Working...
          X