sparkyspider
sparkyspider

Reputation: 13519

Grails: Setting URL mapping priorities

I sometimes come across a situation where I'm trying to set URLMappings as such:

    /** -> ContentController
    /static/$image/$imageNumber -> ResourcesController

Then when I visit /static/image/13 it will often hit the /** instead of the

/static/*/*
How do I tell Spring / Grails to rather try and match the other one first?

Upvotes: 4

Views: 1575

Answers (2)

sparkyspider
sparkyspider

Reputation: 13519

Turns out that Grails will go from more specific to least specific, as long as:

  1. You don't have syntax errors in your URLMappings and
  2. Sometimes you need to restart grails for it to correctly take effect.

    "/other-test/$testname" { // Fired for "/other-test/hi-there/"
        controller="test"
    }
    
    "/**" { // fired for "/something-else"
        controller="test"
    }
    

Upvotes: 1

doelleri
doelleri

Reputation: 19682

URL mappings are hit in the order they are declared, so put your catch all /** last.

EDIT: This answer tickled at the back of my mind, and I recalled something I read on the mailing list a while back. Back in Grails 1.1 or so, URLMappings were evaluated in the order declared. Now, however, URLMapping matching is slightly more complex. The URLMappings will try to return the best match by comparing the number of wildcards, static tokens, and finally number of constraints. You can see this in the source.

Since URL mapping order no longer matters, it must be something else (although I find listing them in rough order makes it easier to read through them). It looks like the second fragment should actually be a static token. I'd try /static/image/$imageNumber.

Upvotes: 3

Related Questions