unfettered
unfettered

Reputation: 351

Grails URL mapping for resources that don't exist

I have a fizzbuzz.gsp, and I have a FizzBuzzController with an index() method that renders this GSP:

class FizzBuzzController {
    def index() {
        render(view: "fizzbuzz", model: getModel())
    }

    def getModel() { ... }
}

Normally, to get the HTML associated with the fiizbuzz.gsp file, I would make a call to http://myapp.example.com/fizzbuzz.

I have a unique situation where I now need my Grails app to serve requests for HTML file URLs like so http://myapp.example/fizzbuzz.html.

Does Grails provide any way to map incoming request for http://myapp.example/fizzbuzz.html to http://myapp.example.com/fizzbuzz? That way, the client-side could request an HTML file, but that would still serve back the correct GSP/HTML file from the server-side. Any ideas?

Upvotes: 0

Views: 418

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

You can do that is different ways. If you are using Grails 2.3 or above you can use redirect in UrlMapping.groovy as:

"/fizzbuzz.html"(redirect: "/fizzBuzz")

which dictates to "If you see fizzbuzz.html in url, then redirect to the index action of fizzBuzz controller"

or you can also use it explicitly as

//index is the default action therefore specifying action in the map is optional
"/fizzbuzz.html"(redirect: [ controller: "fizzBuzz", action: ''index' ]) 

or you can duplicate the mapping as

"/fizzbuzz.html"(controller: "fizzBuzz") 

In either of the case it would invoke the action method instead of looking for any static resource.

Upvotes: 1

Related Questions