Grails: Setting URL Mapping Priorities

I sometimes come across a situation where I try to set URLMappings as such:

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

Then, when I visit /static/image/13 , it often ends up in /** instead

  / static / * / * 
. How can I tell Spring / Grails to rather try and match another first?
+4
source share
2 answers

It turns out that Grails will go from more specific to least specific if:

  • You have no syntax errors in URLMappings and
  • Sometimes you need to restart grails so that it can take effect.

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

The URL mappings fall in the order in which they are declared, so add your track /** last.

EDIT: This answer tickled my mind and I again remembered something that I read on the mailing list. Back in Grails 1.1 or so, URLMappings were evaluated in declared order. However, now URLMapping mapping is a bit more complicated. URLMappings will attempt to return the best match by comparing the number of wildcards, static tokens, and finally the number of restrictions. You can see it in the source .

Since the order of matching URLs no longer matters, it should be something else (although I believe that listing them in a rough order makes them easier to read). It seems that the second fragment should be a static marker. I would try /static/image/$imageNumber .

+2
source

Source: https://habr.com/ru/post/1441624/


All Articles