2011-02-07 178 views
0

我在處理grails 1.3.6中的404 HTTP響應時出現了一個奇怪的問題(同樣的錯誤行爲在1.3.5中)。它有時可以工作,但大部分時間沒有。我認爲這是一個Grails錯誤,但沒有發現任何錯誤在Grails的jira .. 每當我請求錯誤的URL我收到默認的Tomcat 404頁面。 我的配置/ UrlMappings.groovy樣子:404的Grails URLMappings無法正常工作

class UrlMappings { 

    static mappings = { 
     "404" { 
      controller = 'customError' 
      action = 'index' 
      code = 404 
     } 
     "500" { 
      controller = 'customError' 
      action = 'index' 
      code = 500 
     } 

     "/"(controller: "home", action: "index") 
     "/$controller/$action?/$id?"{ 
      constraints { 
      // id has to be a number 
       id(matches: /\d+/) 
      } 
     } 
    } 
} 

隱而不宣有人知道如何解決它:-)

最佳, 刁

回答

1

我認爲這個問題是您正在使用大括號{? }而不是使用括號()。這就是應該如何使用customError控制器列出的示例。

靜態映射= {

「404」(控制器: 「customError」,動作: 「索引」)

「500」(控制器: 「customError」,動作: 「索引」)

...

}

請參閱[6.4.4 Grails文檔中] [1]以獲取更多信息。

[1]:http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.4.4映射到響應代碼

+0

有了這個括號{}它有時也可以。使用正常的括號,它似乎每次都有效。問題是我必須將代碼參數發送到控制器中。你知道如何使用正常的括號發送它。 「params:[...]」聲明不起作用...... – kuceram 2011-02-07 21:31:59

0

嘗試沒有空間。有在舊版本的Grails,其中的空間會導致錯誤映射沒有被拾起的一個錯誤是由於一些地方正則表達式的錯誤:

static mappings = { 
    "404"{ 
      controller = 'customError' 
      action = 'index' 
     code = 404 
    } 
    "500"{ 
     controller = 'customError' 
     action = 'index' 
     code = 500 
    }