On a form I have two submit buttons:
<g:submitButton name="开发者_运维问答add" value="Add"/>
<g:submitButton name="addAnother" value="Add and Create Another"/>
I need to take slightly different action based on which button was used to submit the form. How to I get the name of the clicked button in my controller?
The name of the clicked button ends up in params:
if(params["addAnother"])
doThis()
else
doThat()
From the tags docs:
actionSubmit
Generates a submit button that maps to a specific action, which lets you have multiple submit buttons in a single form. (...) When you use a normal submit button inside a form, it is the form itself that determines what URL the request is sent to, and therefore what action is executed. However, this tag overrides that behaviour and determines which action is executed. Note that it is still the form that determines the target controller.
<g:form controller="book">
<g:actionSubmit value="click to update" action="update" />
<g:actionSubmit value="click to delete" action="delete" />
</g:form>
Although you mention a "slight diference" between the actions from each button, among grails tags I would suggest actionSubmit as the preferable way to deliver form data to different behaviors (actions) in the controller. Even if you have mostly common behavior between them, think about refactoring you controller code instead of testing request parameters inside your action.
精彩评论