简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM