Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails UrlMappings with .html
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;a href="/testApp/testJsp.jsp"&gt;test 1&lt;/a&gt; &lt;a href="/testApp/testGsp.gsp"&gt;test 2&lt;/a&gt; &lt;a href="/testApp/testHtm.htm"&gt;test 3&lt;/a&gt; &lt;a href="/testApp/testHtml.html"&gt;test 4&lt;/a&gt; </code></pre> <p>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.</p> <p>I set up the Grail UrlMappings.groovy file to map all URLs of that form (/$myPathParam?) to a single controller:</p> <pre><code>class UrlMappings { static mappings = { "/$controller/$action?/$id?"{ constraints { } } "/$path?" (controller: 'auditRecord', action: 'showPage') "500"(view:'/error') } } </code></pre> <p>In that controller (in the appropriate "showPage" action) I've been printing out the path information, for example:</p> <pre><code>def showPage = { println "params.path = " + params.path ... render(view: resultingView) } </code></pre> <p>The results of the println in the showPage action for each of my four links are</p> <pre><code>testJsp.jsp testGsp.gsp testHtm.htm testHtml </code></pre> <p>Why is the last one "testHtml", not "testHtml.html"?</p> <p>In a previous (<a href="https://stackoverflow.com/questions/2552215/grails-url-mapping-how-to-pass-file-extension">Stack Overflow query</a>) 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.</p> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload