我制作了一个脚本,根据当前的请求语言环境加载内容。就像是
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:有关上下文的更多信息
- 圣杯 2.0.4
- 资源 1.2.RC2
此外,它是插件内部的布局,而不是一个应用程序。
未在 Grails 2.1 中测试,但会这样做。
编辑 3:
刚刚测试,Grails 2.1.1
新Grails 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 -> 脚本确定
<!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>