Restricting all URLs to end with .html in a Grails Application

03 / Apr / 2009 by Aman Aggarwal 1 comments

While going through the grails-users mailing list, I found a question about restricting all URLs to end with ‘.html’ & returning Error 404 for the default mappings. After spending some time, I was able to build a sample CRUD application restricting URLs to end with .html.

I did the following steps for the same:

1. Create a new grails application with name ‘Urlmapping’: grails create-app Urlmapping
2. Create domain class Urltest: grails create-domain-class Urltest
3. Add a String variable ‘test’ in domain class Urltest:

class Urltest {
    String test
    static constraints = {
    }
}

4. Generate controllers & views for Urltest: grails generate-all Urltest
5. Changed the UrlMappings in UrlMappings.groovy:

class UrlMappings {
    static mappings = {
        "/$controller/$action/$id.$suffix"
        {
            constraints {
                suffix(matches: 'html')
            }
        }
        "/$controller/$action.$suffix"
        {
            constraints {
                suffix(matches: 'html')
            }
        }
        "/"(view: "/index")
        "500"(view: '/error')
    }
}

6. Removed the Entry “html: ['text/html','application/xhtml+xml'],” from map grails.mime.type in Config.groovy

7. Created initial data in BootStrap.groovy:

class BootStrap {
    def init = {
        servletContext - & gt;
        Urltest urltest
        (1..5).each {
            urltest = new Urltest(test: 'Test-' + it)
            urltest.save()
        }
    }
    def destroy = {
    }
}

Run the application: grails run-app

Now, I had to use following urls for CRUD functionality:

# http://localhost:8080/mailing/urltest/index.html
# http://localhost:8080/mailing/urltest/list.html
# http://localhost:8080/mailing/urltest/create.html
# http://localhost:8080/mailing/urltest/show/1.html
# http://localhost:8080/mailing/urltest/edit/1.html

(Scaffolding generated links http://localhost:8080/mailing/urltest/index, http://localhost:8080/mailing/urltest/edit/1 & http://localhost:8080/mailing/urltest/show/1 etc. won’t work since default associated UrlMapping "/$controller/$action/$id?" has been removed from UrlMappings.groovy)

To convert the links into desired format, you needs to pass suffix=’html’ as params to g:link & g:form tags on all views & redirects in Controller.

I have attached the source code for reference.

This was my first attempt for editing default Url mappings. I am sure I will be able to find better & less tedious ways of doing the same over a period of time.

Also, I would like to mention that I found broken links generated by g:paginate tag.
A JIRA issue exists for the same.


~Aman Aggarwal
aman@intelligrape.com

http://www.IntelliGrape.com/

FOUND THIS USEFUL? SHARE IT

comments (1 “Restricting all URLs to end with .html in a Grails Application”)

  1. Craig

    Hi Aman,

    Great post, very informative. One thing I found to make this less tedious is to have the urlmappings defined as

    “/$controller/$action/${id}.html”
    {}
    “/$controller/${action}.html”
    {}

    That way you don’t need to pass any suffix in the g:link and g:form tags.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *