我做了一个基于当前请求语言环境加载内容的脚本。像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.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 - >脚本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>
我已经这样做了,仍然没有运气。 –
你能发布loadLangInfo的完整源代码吗? –
loadLangInfo只是一个javascript函数。问题与资源有关,请参阅我的编辑。 –