2012-11-19 14 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函数。问题与资源有关,请参阅我的编辑。 –