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]) } }
<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>