Grails“渲染”渲染模板
在我的 Grails 控制器中,我正在响应 AJAX 调用并使用 render
返回文本:
def ajaxRandomPersonName = {
def person = get a random person ...
render "Name: ${person.name}"
}
问题是 render
呈现整个模板。因此,它不只是渲染“Name:John”,而是渲染模板中定义的所有图标、导航等。如何让 render
在没有模板的情况下进行渲染?
我基本上使用 Grails 1.1.1 来遵循“Grails in Action”(第 28 页)的第一章。
跟进: 根据 Rhysyngsun 的建议返回 false 没有任何影响。我还尝试将模板设置为 null,但它仍然渲染模板:
def ajaxRandomPersonName = {
def person = get a random person ...
render (template:null, text:"Name: ${person.name}")
}
无论我做什么,render
都致力于通过模板渲染它。
跟进2:grails 用户邮件列表上的并行讨论。
后续3:示例代码: 我将代码配对到最低限度,但它仍然显示出不需要的模板渲染。
controllers/PersonController.groovy:
class PersonController {
def index = { }
def home = { [message:"Hello"] }
def ajaxTest = {
println "ajaxTest called"
render text: "ajax message"
}
}
views/person/home.gsp(主页方法的视图页面)
<html>
<head>
<title>Home View</title>
<g:javascript library="prototype" />
</head>
<body>
<p>
<g:remoteLink action="ajaxTest" update="test1">ajax call</g:remoteLink>
</p>
<p>Message = ${message}</p>
<p id="test1">Blank</p>
</body>
</html>
views/layouts/person.gsp(人员控制器的布局模板)
<html>
<head>
<title>Test App - <g:layoutTitle/></title>
<g:layoutHead/>
</head>
<body>
<h1>Test App</h1>
<g:layoutBody/>
</body>
</html>
我使用主页视图访问人员控制器: http://localhost:8080/test/person/home
页面呈现为: 测试应用程序 ajax 调用(超链接) 留言 = 你好 空白的
“测试应用程序”来自模板。当我单击“ajax 调用”时,它会异步调用 PersonController 的 ajaxTest 方法(使用 println 验证)。 ajaxTest 所做的只是 println 并呈现静态文本。结果如下:
Test App
ajax call
Message = Hello
Test App
ajax message
请注意,模板正在“test1”
中呈现,这会产生第二个“Test App”。
我正在运行 Grails 1.1.1。有什么想法吗?代码看起来很简单。我下载了 Grails 源代码并查看了 RenderDynamicMethod.java。除非模板位于参数列表中,否则它不会进行任何模板渲染,但事实并非如此。所以我唯一的猜测是正在再次渲染模板。
In my Grails controller I'm responding to an AJAX call and using render
to return the text:
def ajaxRandomPersonName = {
def person = get a random person ...
render "Name: ${person.name}"
}
The problem is that render
renders the whole template. So instead of just rendering "Name: John" it renders all the icons, navigation, etc defined in the template. How do I get render
to just render without the template?
I'm pretty much following Chapter 1 of "Grails in Action" (page 28) using Grails 1.1.1.
Follow up:
Returning false per Rhysyngsun's suggestion has no impact. I also tried setting the template to null but it still renders the template:
def ajaxRandomPersonName = {
def person = get a random person ...
render (template:null, text:"Name: ${person.name}")
}
render
has its heart bent on rendering it through the template no matter what I do.
Follow up 2: Parallel discussion on grails-user mailing list.
Follow up 3: Sample code:
I paired down my code the bare minimum and it still exhibits the undesired template rendering.
controllers/PersonController.groovy:
class PersonController {
def index = { }
def home = { [message:"Hello"] }
def ajaxTest = {
println "ajaxTest called"
render text: "ajax message"
}
}
views/person/home.gsp (view page for home method)
<html>
<head>
<title>Home View</title>
<g:javascript library="prototype" />
</head>
<body>
<p>
<g:remoteLink action="ajaxTest" update="test1">ajax call</g:remoteLink>
</p>
<p>Message = ${message}</p>
<p id="test1">Blank</p>
</body>
</html>
views/layouts/person.gsp (layout template for person controller)
<html>
<head>
<title>Test App - <g:layoutTitle/></title>
<g:layoutHead/>
</head>
<body>
<h1>Test App</h1>
<g:layoutBody/>
</body>
</html>
I access person controller with the home view:
http://localhost:8080/test/person/home
the page renders as:
Test App
ajax call (hyperlink)
Message = Hello
Blank
"Test App" is from the template. When I click "ajax call" it makes an asynchronous call to PersonController's ajaxTest method (verified with println). All ajaxTest does is println and render static text. This resultant in the following:
Test App
ajax call
Message = Hello
Test App
ajax message
Note that the template is being rendered within "test1" <p>
which results in the second "Test App".
I'm running Grails 1.1.1. Any ideas? The code seems straightforward. I downloaded the Grails source and looked at RenderDynamicMethod.java. It doesn't do any template rendering unless template is in the argument list, which it isn't. So my only guess is something up steam is rendering the template again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
已解决:添加 contentType 会导致模板未呈现:
Resolved: Adding contentType results in the template not being rendered:
让您的客户端 JavaScript 代码处理 JSON 响应并使用以下方式呈现您的响应:
Make your client side javascript code handle a JSON respond and render your response with:
您可能会对 Grails 中的“按约定布局”功能感到困惑。例如,如果您的布局名称与控制器名称前缀匹配,Grails 会将布局应用到该控制器管理的每个视图。不幸的是,它甚至适用于文本和模板。目前有一些与此相关的 JIRA 记录(请参阅 http://jira.grails.org/browse/例如 GRAILS-7624)。
我今天被这个烫伤了。我通过简单地重命名我的布局 gsp 使其与任何控制器名称不匹配来解决它。我的布局最初命名为“storefront.gsp”,并且我有一个名为 StorefrontController 的控制器。我将布局重命名为“public.gsp”。
You might be getting burnt by the 'layout-by-convention' feature in Grails. If your layout name matches the controller name prefix, for example, Grails will apply the layout to every view managed by that controller. Unfortunately, it even applies to text and templates. There are currently a few JIRAs logged regarding this (see http://jira.grails.org/browse/GRAILS-7624 for example).
I got burnt by this today. I resolved it by simply renaming my layout gsp such that it doesn't match any controller name. My layout was initially named 'storefront.gsp' and I have a controller named StorefrontController. I renamed the layout to 'public.gsp'.
我们发现从操作中显式返回 false 可以解决此问题。
我相信将 render foo 作为 JSON 隐式返回 false。
We've found that explicitly returning false from the action fixes this.
I believe doing render foo as JSON returns false implicitly.