【问题标题】:Grails Resources - cannot add r.script in layout?Grails 资源 - 无法在布局中添加 r.script?
【发布时间】:2012-11-19 19:28:45
【问题描述】:

我制作了一个脚本,它根据当前请求区域设置加载内容。类似的东西

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

如果我在布局中添加这个,页面会抛出错误:

在第 [6] 行评估表达式 [my.loadLangInfo()] 时出错:不能 添加需要处置 [defer] 的模块 [-page-fragments-] 此请求 - 已呈现该处置。

错误 2012-11-19 15:13:54,801 [http-bio-8080-exec-5] 错误 [Tomcat].[localhost] - 异常处理 ErrorPage[errorCode=500, location=/grails-errorhandler] 消息: java.io.UnsupportedEncodingException:字符编码 [null] 是 不支持

但是如果我在我的页面而不是布局中添加这个标签,页面就会成功呈现。

无法将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新的插件项目,布局中的script标签被忽略了!

./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 -> 脚本确定

<!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>

【问题讨论】:

    标签: grails grails-2.0


    【解决方案1】:

    我发现当您在布局和页面中都声明 &lt;r:layoutResources /&gt; 并尝试在布局中添加脚本时会出现问题。

    为了更正,我从每个视图中删除了layoutResources,只保留在布局中。

    【讨论】:

      【解决方案2】:

      尝试在布局的最后一个 r:layoutResources 之前添加标签。

      【讨论】:

      • loadLangInfo 只是一个 javascript 函数。问题在于资源,请参阅我的编辑。
      • 那么“page-fragments”模块在哪里以及如何定义/添加?
      • page-fragments 似乎是您在r:script 中添加的脚本代码资源使用的名称,我没有任何以此命名的模块。还用我的测试更新了问题,似乎插件布局中的脚本被资源忽略了......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      相关资源
      最近更新 更多