Oct 11, 2012

Show GSP directly in Grails 2

Posted Oct 11, 2012
Starting Grails 2.0, you cannot access GSP's directly when you type it in the url without defining it in url mappings. It has been disallowed since it's considered a security flaw of Grails 1.x. The side-effect is for simple static pages, it's a hassle to create mappings everytime.

Fortunately, Grails allows a simple workaround by using the UrlMappings.groovy. You just need to define one controller and make its actions accessible in url mappings configuration.

These are the steps I followed to quickly see all my root pages:
  1. Create a StaticViewController
  2. My UrlMappings.groovy contains:
     "/"(controller:"staticView")
     "/$action"(controller:"staticView")

Creating a controller is my preferred way, it's an additional step but my views are more manageable this way. 

For this example, I defined the following in my StaticViewController:
def index() {
}

def hello() {
}

Then added the ff gsps:
views/staticView/index.gsp
views/staticView/hello.gsp

I can now access the following in my browser:
http://localhost:8080/[app]/
http://localhost:8080/[app]/hello

To make your urls more search engine friendly, you may also want to map urls that ends in "html" to your controller, just add this line in UrlMappings.groovy :
"/*.html"(controller:"staticView")

In this case, I'll be able to access:
http://localhost:8080/[app]/hello.html

---
src: http://grails.1312388.n4.nabble.com/Direct-linking-to-gsp-in-Grails-2-0-td4228929.html

1 comment:

  1. A very good post ,I like it very much ,hope you will give another post asap Great info Thanks!
    Access Control Solutions

    ReplyDelete