如何在Grails中呈现静态文件?

时间:2015-03-31 02:59:59

标签: grails grails-2.0 grails-controller

我是Grails的新手并尝试了一些东西。

我创建了一个控制器HelloController并将其放在grails-app / controllers目录中

这是HelloController类中的代码

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

public class HelloController {              

    def index() {           


        render("hello world") 
    }
}

当我执行grails run-app然后打开页面http://localhost:8080/AppName/hello

时,此功能正常

作为下一步,我想将hello world移动到.gsp文件,然后呈现文件的内容

所以我创建了文件夹grails-app/views/hello并在包含内容的文件夹中创建了一个index.gsp文件

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

我的下一步应该是什么,以确保呈现此文件中的内容?

试图阅读respond方法,但似乎没有帮助

1 个答案:

答案 0 :(得分:3)

您没有任何数据要从控制器发送,因此不返回任何内容:

def index() {           
}

Grails将正确显示您的index.gsp文件,因为文件名和操作名称匹配。

控制器上的文档:http://grails.github.io/grails-doc/latest/guide/theWebLayer.html#controllers