Search code examples
grailsgrails-2.0grails-controller

How to render a static file in Grails?


I am pretty new to Grails and trying out stuff.

I have created a controller HelloController and placed it in the grails-app/controllers directory

here is the code in the HelloController class

import org.springframework.http.HttpStatus.*;
import grails.transaction.Transactional;

public class HelloController {              

    def index() {           


        render("hello world") 
    }
}

This works fine when i do grails run-app and then open the page http://localhost:8080/AppName/hello

As a next step I want to move the hello world to a .gsp file and then render the contents of the file

so i have created the folder grails-app/views/hello and created an index.gsp file in the folder with the content

<html>
    <body>
        <h1>Hello World</h1>
    </body>
</html>

What should be my next step to make sure that content from this file is rendered ?

Tried to read on the respond method but that does not seem to help


Solution

  • You do not have any data to send from the controller so return nothing:

    def index() {           
    }
    

    Grails will display your index.gsp file correctly, simply because the file name and the action name match.

    Doc on controllers: http://grails.github.io/grails-doc/latest/guide/theWebLayer.html#controllers