Grails g:include 标记以包含控制器调用
我问了这个问题这里我有一个新的grails应用程序,索引页面是由几个部分模板组成。我的每个部分模板都需要从不同的控制器加载数据,建议的解决方案是使用标签 - 我已经尝试过,但没有看到任何反应,并且在网络上搜索后,我找不到任何功能齐全的示例。
这是我的部分模板 _newsFeed.gsp:
<div id="news_feed">
<g:include controller="news" action="latestsNews" /></div>
和我的控制器 NewsController.groovy:
class NewsController {
def latestsNews = {
println "in controller"
[news: "News Headline!"]
}
在上面的示例中,我只是想确认控制器正在被调用(这就是 println 在那里的原因) - 但我什么也没得到。
有人可以向我指出一个可行的示例,或者解释一下我是否遗漏了某些内容吗?
谢谢
I asked this question here I have a new grails app and the index page is made up of several partial templates. Each of my partial templates need to load data from a different controller, the suggested solution was to use the tag - I have tried this but seeing nothing happen, and having searched the web I cant find any fully functioning examples of this in action.
Here is my partial template _newsFeed.gsp:
<div id="news_feed">
<g:include controller="news" action="latestsNews" /></div>
and my controller NewsController.groovy:
class NewsController {
def latestsNews = {
println "in controller"
[news: "News Headline!"]
}
In the above example, Im just trying to confirm that the controller is being called (which is why the println is there) - but im never getting anything.
Can someone point me towards a working example or explain if i am missing something?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有看到你的代码有什么重大问题。您的 NewsController 中缺少右大括号,但我怀疑这只是一个拼写错误。我刚刚测试了以下内容:
showMessage.gsp 中只有 ${message} 。然后在我的index.gsp 中
我得到了正确的响应。视图中的其他所有内容是否正确渲染,您确定包含的页面实际上被调用/渲染了吗?
I don't see anything major wrong with your code. You are missing a closing brace in your NewsController, but I suspect that was just a typo. I just tested the following:
showMessage.gsp just has ${message} in it. And then in my index.gsp
I get the correct response. Is everything else in your view rendering correctly, are you sure the page with the includes is actually getting called/rendered?
调用 http://localhost:8080/myapp/home/showMessage 时可以看到日志消息在你的控制台中?请先确认这一点。
标签的使用是正确的,并且我以前使用过很多次。
但是,不建议使用此标记,因为您会为每个请求执行许多控制器调用。您可以使用它来渲染模板,而无需调用第二个控制器。
When invoking http://localhost:8080/myapp/home/showMessage can you see the log messages in your console ? Please make sure of that first.
The use of the tag is correct and I used it many times ago.
However the use of this tag is not recommended because you perform many controller calls for each request. You can use instead to render a template without invoking a second controller.