Search code examples
grailsgsp

g:form doesn't seem to have a "controller" attribute


Recently I began to study Grails, and I noticed that the file create.groovy is generated by a dynamic scaffold. The form tag doesn't have an attribute called controller, only action.

So my question is, how does the Grails framework discover the controller that should be called?

<g:form action="save">
            <fieldset class="form">
                <f:field bean="categoria" property="nome" class="form-control" />
            </fieldset>
            <fieldset class="buttons">
                <g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
            </fieldset>
        </g:form>

Thanks, and sorry for my bad english!


Solution

  • Grails uses "convention over configuration" so whenever you use a form without controller, grails will use the default convention.

    Since your view is put in a folder named after a controller, grails will assume that the default controller for any view inside that folder is the one that matches with the folder name. Example

    BookController is the default controller for any gsp inside the grails-app/views/book folder.

    *This convention also applies to things like links.