Grails UrlMappings with .html

Posted by Glennn on Stack Overflow See other posts from Stack Overflow or by Glennn
Published on 2010-05-27T03:51:38Z Indexed on 2010/05/27 4:01 UTC
Read the original article Hit count: 345

Filed under:
|

I'm developing a Grails web application (mainly as a learning exercise). I have previously written some standard Grails apps, but in this case I wanted to try creating a controller that would intercept all requests (including static html) of the form:

<a href="/testApp/testJsp.jsp">test 1</a>
<a href="/testApp/testGsp.gsp">test 2</a>   
<a href="/testApp/testHtm.htm">test 3</a>
<a href="/testApp/testHtml.html">test 4</a> 

The intent is to do some simple business logic (auditing) each time a user clicks a link. I know I could do this using a Filter (or a range of other methods), however I thought this should work too and wanted to do this using a Grails framework.

I set up the Grail UrlMappings.groovy file to map all URLs of that form (/$myPathParam?) to a single controller:

class UrlMappings {
    static mappings = {
      "/$controller/$action?/$id?"{
          constraints {
          }
      }
      "/$path?" (controller: 'auditRecord', action: 'showPage')
      "500"(view:'/error')
    }
}

In that controller (in the appropriate "showPage" action) I've been printing out the path information, for example:

def showPage = {        
    println "params.path = " + params.path
    ...
    render(view: resultingView)
}

The results of the println in the showPage action for each of my four links are

testJsp.jsp
testGsp.gsp
testHtm.htm
testHtml

Why is the last one "testHtml", not "testHtml.html"?

In a previous (Stack Overflow query) Olexandr encountered this issue and was advised to simply concatenate the value of request.format - which, indeed, does return "html". However request.format also returns "html" for all four links.

I'm interested in gaining an understanding of what Grails is doing and why. Is there some way to configure Grails so the params.path variable in the controller shows "testHtml.html" rather than stripping off the "html" extension? It doesn't seem to remove the extension for any other file type (including .htm). Is there a good reason it's doing this? I know that it is a bit unusual to use a controller for static html, but still would like to understand what's going on.

© Stack Overflow or respective owner

Related posts about grails

Related posts about groovy