2016-09-29 32 views
0

我有一些显示.gsp文件的问题,我不太确定原因。我有以下代码:Grails webapp不显示gsp页面

class UrlMappings{ 
    static mappings = { 
     "/"(controller: 'index', action: 'index') 
    } 
} 

class IndexController{ 
    def index(){ 
     render(view: "index") 
    } 
} 

然后在的grails-app /视图/索引我有index.gsp中:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Hello World</title> 
    </head> 
    <body> 
     Hello World 
    </body> 
</html> 

当我打http://localhost:8080/我得到一个500个状态码错误。但是,如果我将IndexController更改为

render "Hello World" 

它将显示“Hello World”,因此该应用似乎正在启动。

有谁知道发生了什么事?堆栈跟踪的一部分:

17:09:40.677 [http-nio-8080-exec-1] ERROR o.a.c.c.C.[.[.[.[grailsDispatcherServlet] - Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Could not resolve view with name '/index/index' in servlet with name 'grailsDispatcherServlet'] with root cause 
javax.servlet.ServletException: Could not resolve view with name '/index/index' in servlet with name 'grailsDispatcherServlet' 
+0

似乎很奇怪。只要确保你已经运行了经典的'grails clean'和Grails运行时重启。 – Yuri

+0

避免使用框架中具有特定含义的名称。如果您将索引更改为其他内容,是否会得到相同的错误? – Armaiti

+1

也不会'http:// host/index/index'看起来有点不对?无论如何,“/ index”(controller:'aha',action:“nice”)然后将'/ index'重定向到其他一些控制器动作,你可以为'/ index/index'编写它,但是认为它看起来像一个小奇怪的人开始质疑开发者的技能:) – Vahid

回答

0

你所得到的错误是因为Grails无法找出你的视图的位置。

那么避免在框架中有一些预定义上下文的名称(只是在你的情况下建议不是问题)。

正如你所使用的index它控制人变更为其他

所以你的情况时,你会打URLhttp://localhost:8080/URLMapping将其重定向至控制器index行动,它会呈现相应的视图。

像下面

class UrlMappings{ 
    static mappings = { 
     "/"(controller: 'provision', action: 'index') 
    } 
} 

class ProvisionController{ 

    def index(){ 
     // You don't really need to render it grails will render 
     // it automatically as our view has same name as action 
     render(view: "index") 
    } 
} 

然后在grails-app/views/provision/创建index.gsp

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Hello World</title> 
    </head> 
    <body> 
     Hello World 
    </body> 
</html> 

你被添加在错误的位置grails-app/views/index.gsp移动视图它grails-app/views/provision/index.gsp

更名ÿ在上面的例子中,我们的IndexControllerProvisionController

+0

你好普拉卡什,谢谢你的回答。我设法弄清了它为什么表现得如此。我不相信我把它放在错误的位置,因为我在grails-app/views/index /中有index.gsp。我认为它不工作的原因是因为在build.gradle中,我设置了配置文件“org.grails.profiles:rest-api”而不是:web(当我第一次开始我的小项目时,我只对API功能感兴趣,并不担心前端) – Martin