2008-08-26 131 views

回答

10

編輯UrlMappings.groovy

添加例如添加此規則,以處理與HomeController的根。

「/」(控制器: '家')

+1

工作對我來說,下Grails的1.3.7,與動作:鍵。 – jerseyboy 2012-04-05 01:45:59

53

在UrlMappings.groovy

 
"/" { 
    controller = "yourController" 
    action = "yourAction" 
} 

添加此通過配置URLMappings這樣,應用程序的主頁會yourWebApp/yourController/yourAction。

(剪切/從IntelliGrape Blog粘貼)

+0

也許該方法在後續版本的Grails中更改,因爲它在1.3.7上不起作用。使用daherman發佈的語法爲我工作。 – jerseyboy 2012-04-05 01:45:40

2

簡潔明快

  1. 轉到文件:在grails-app/conf目錄/ UrlMappings.groovy。

  2. 用 「/」(控制器:'home',action:「/ index」)替換該行:「/」(view:「/ index」)。

家是你的控制器運行(如在春季安全性,您可以使用「登錄」)和行動是Grails的查看與控制器相關的頁面(在Spring Security「/ AUTH」)。頁面按您的應用需求

加入重定向。

11

您可以嘗試作爲UrlMappings.groovy類是配置文件夾內如下

class UrlMappings { 

    static mappings = { 

     "/$controller/$action?/$id?"{ 
      constraints { 
       // apply constraints here 
      } 
     } 

     //"/"(view:"/index") 
     "/" (controller:'Item', action:'index') // Here i have changed the desired action to show the desired page while running the application 
     "500"(view:'/error') 
    } 
} 

希望這有助於
魯貝爾

0

所有的答案是正確的! 但讓我們來想象一個場景:

我映射路徑「/」與控制器:「家」和行動:「索引」,所以當我訪問「/應用程序名稱/」控制器家得到執行,但如果我輸入路徑「/ app-name/home/index」,它仍然會被執行!所以一個資源有兩條路徑。它會工作,直到有人找到「家/索引」的路徑。

另一件事是,如果我有一個表格沒有任何指定action屬性,因此默認情況下它會POST到同一個控制器和行動!所以如果表單映射到「/」路徑並且沒有指定action屬性,那麼它將被提交給同一個控制器,但是這次路徑在你的地址欄中是「home/index」,而不是「/」,因爲它被提交給控制器/操作而不是URI。

爲了解決這個問題,你所要做的,就是刪除或註釋掉這些行。

//  "/$controller/$action?/$id?(.$format)?"{ 
//   constraints { 
//    // apply constraints here 
//   } 
//  } 

所以,現在當你訪問「/」,將工作。但「家/索引」不會。但是存在一個缺陷,現在必須通過明確寫入URLMapping文件來手動將所有路徑映射到控制器。我想這會有所幫助!

2

使用控制器,查看和操作參數通過以下語法:

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