2012-11-19 50 views
1

我做了一個基於當前請求語言環境加載內容的腳本。像Grails資源 - 不能在佈局中添加r.script?

class ScriptsTagLib { 
    static namespace = "my" 
    def loadLangInfo = { attrs -> 
    Locale locale = RequestContextUtils.getLocale(request) 
    r.script() { 
     out << '$(function(){ loadLangInfo("'+locale.language+'") });' 
    } 
    } 
} 

如果我在我的佈局添加這個東西,頁面拋出一​​個錯誤:

Error evaluating expression [my.loadLangInfo()] on line [6]: Cannot add module [-page-fragments-] which requires disposition [defer] to this request - that disposition has already been rendered.

Error 2012-11-19 15:13:54,801 [http-bio-8080-exec-5] ERROR [Tomcat].[localhost] - Exception Processing ErrorPage[errorCode=500, location=/grails-errorhandler] Message: java.io.UnsupportedEncodingException: The character encoding [null] is not supported

但是,如果我在頁面中添加此標記,而不是佈局,呈現頁面成功。

它不能添加到r.script()佈局?

編輯:問題是真正與佈局資源。失敗另一個例子是:

<g:layoutHead/> 
<r:script> 
$(function(){ }); 
</r:script> 
<r:layoutResources /> 

編輯2:有關上下文更多信息

  • 的Grails 2.0.4
  • 資源1​​.2.RC2

而且,這是一個插件內的佈局,而不是一個應用程序。

Grails的2.1沒有測試,但是將這樣做。

編輯3

現在只是測試,並Grails 2.1.1Grails 2.0.4新新鮮插件項目,並在佈局腳本標記被忽略!

./views/layout/test.gsp - >腳本忽略

<!doctype html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title><g:layoutTitle default="Insoft4 UI Plugin"/></title> 
     <g:layoutHead/> 

     <r:layoutResources /> 
    </head> 
    <body> 
     <g:layoutBody/> 
     <r:script disposition="defer"> 
      alert('layout!'); 
     </r:script> 
     <r:layoutResources /> 
    </body> 
</html> 

./views/index.gsp - >腳本OK

<!doctype html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Teste</title> 
     <meta name="layout" content="teste" /> 
       <r:script disposition="defer"> 
      alert('index!'); 
     </r:script> 
     <r:layoutResources /> 
    </head> 
    <body> 
     <h1>Testing this index!</h1> 
     <r:layoutResources /> 
    </body> 
</html> 

回答

4

我發現,當你宣佈雙方<r:layoutResources />出現問題,佈局和頁面並嘗試在佈局中添加腳本。

要糾正我從各個視圖中刪除的layoutResources,只留下在佈局。

0

嘗試最後的R之前添加標籤:佈局上的layoutResources。

+0

我已經這樣做了,仍然沒有運氣。 –

+0

你能發佈loadLangInfo的完整源代碼嗎? –

+0

loadLangInfo只是一個javascript函數。問題與資源有關,請參閱我的編輯。 –