Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails - URL mapping/default action and flow

Tags:

grails

Question -

I've noticed that some applications I test with have calls to another view/controller from an action submit, but when that page is rendered, instead of seeing:

$controller/$page

I see:

$controller/index

Is this an issue with the URL mapping configuration? Default action? Just curious, because it just appears to be the URI mapping to a default instead of the actual action.

view code:

<table>
..
<g:actionSubmit class="stats" action="stats" value="View Stats"/>
..
</table

controller:

def stats() {

    def teamId = Team.get(params.id)
    def allPlayers = Player.withCriteria {
            eq('team', teamId)
            and {
                eq('isActive', true)
            }
    }
    [allPlayers:allPlayers, teamId:params.id]
}

UrlMapping:

class UrlMappings {

static mappings = {
    "/$controller/$action?/$id?"{
        constraints {
            // apply constraints here
    }
}
}

Edit

I actually figured out what it is. Which makes me even more confused.

The grails actionSubmit has an action tied to it. That form was just a normal form, without call:

<g:form>
<g:actionSubmit class="stats" action="stats" value="View Stats"/>
<g:actionSubmit class="schedule" action="schedule" value="View Schedule"/>
<g:form>

So by default, the form redirects the action to $controller/index. If you add an action call in the g:form tag, those two buttons will direct to the correct page, but the URI will now be $controller/$g:form_action.

I guess I don't get the point of the actionSubmit's action if the g:form is needed as a wrapper.

like image 245
user82302124 Avatar asked Apr 19 '12 14:04

user82302124


1 Answers

Yes, index is the default action for all controllers. So if you do not specify one, that is the page you will land on for the controller.

It is discussed in further detail on their website. Namely, the rules are:

  • If only one action is present the default URI for a controller maps to that action.
  • If you define an index action which is the action that handles requests when no action is specified in the URI /book
  • Alternatively you can set it explicitly with the defaultAction property:

    static defaultAction = "list"

like image 195
Igor Avatar answered Oct 27 '22 14:10

Igor