PDA

View Full Version : Forms and Image Buttons


theMesser
Sep 28th, 2004, 10:40 PM
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

rrsIPOV
Sep 28th, 2004, 11:15 PM
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.

theMesser
Sep 28th, 2004, 11:58 PM
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

katentim
Sep 29th, 2004, 12:06 AM
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(submitType) {
document.forms["aForm"].elements["submitType"].value = submitType;
if (submitType=="Button2") {
//Do something extra
//To change controller handler you need to change the action
//e.g. document.forms["aForm"].action = new URL
}
document.forms["aForm"].submit();
}
//-->
</script>
</head>
<body>
...
<form name="aForm" method="POST">
<input type="hidden" name="submitType"/>
</form>
<a href="javascript:doSubmit('Button1')"><img ... border="0" /></a>
<a href="javascript:doSubmit('Button2')"><img ... border="0" /></a>

Colin Sampaleanu
Sep 29th, 2004, 05:50 PM
Also see my last post (as of now) in this thread:

http://forum.springframework.org/showthread.php?t=10520