2016-09-29 69 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