Grails Cookbook - A collection of tutorials and examples

Grails actionSubmit Tag Example

The actionSubmit tag (<g:actionSubmit>) lets you have multiple action targets on the same form. For example, if you have a calculation form and you want to have a different controller action handling each submit button:
fx01
Such that clicking Sum will give you the answer:
fx02
Clicking Difference will give you the answer:
fx03
Clicking Product will give you the answer:
fx04
Clicking Quotient will give you the answer:
fx05

Controller Code


Here is the code of the controller
package asia.grails.test
class CalcController {
	def displayForm() {
	}
	def calculateSum(int a, int b) {
		render (view:'result', model:[answer:a+b])
	}
	def calculateDiff(int a, int b) {
		render (view:'result', model:[answer:a-b])
	}
	def calculateProduct(int a, int b) {
		render (view:'result', model:[answer:a*b])
	}
	def calculateQuotient(int a, int b) {
		render (view:'result', model:[answer:a/b])
	}
}

View Code

Here is how you use the actionSubmit tag in displayForm.gsp
<html xmlns="http://www.w3.org/1999/html">
<head>
	<meta name="layout" content="main"/>
	<title>Calc</title>
</head>
<body>
	<g:form>
		First Number: <g:textField name="a"/><br/>
		Second Number: <g:textField name="b"/><br/>
		<g:actionSubmit value="Sum" action="calculateSum"/>
		<g:actionSubmit value="Difference" action="calculateDiff"/>
		<g:actionSubmit value="Product" action="calculateProduct"/>
		<g:actionSubmit value="Quotient" action="calculateQuotient"/>
	</g:form>
</body>
</html>
As shown above, we can submit the form to different actions of the same controller, by having as many actionSubmit tag as needed.

And this is the code of result.gsp

<html xmlns="http://www.w3.org/1999/html">
<head>
	<meta name="layout" content="main"/>
	<title>Calc</title>
</head>
<body>
	The answer is ${answer}
</body>
</html>


Other Grails Tag Examples


Tags: actionSubmit, Grails Tag