Grails g:include 标记以包含控制器调用

发布于 2024-10-25 07:11:46 字数 664 浏览 4 评论 0原文

我问了这个问题这里我有一个新的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三五鸿雁 2024-11-01 07:11:46

我没有看到你的代码有什么重大问题。您的 NewsController 中缺少右大括号,但我怀疑这只是一个拼写错误。我刚刚测试了以下内容:

class HomeController {

   def showMessage = {
     println "Showing message"
     [message: 'this is a message']
   }
}

showMessage.gsp 中只有 ${message} 。然后在我的index.gsp 中

<g:include controller="home" action="showMessage" />

我得到了正确的响应。视图中的其他所有内容是否正确渲染,您确定包含的页面实际上被调用/渲染了吗?

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:

class HomeController {

   def showMessage = {
     println "Showing message"
     [message: 'this is a message']
   }
}

showMessage.gsp just has ${message} in it. And then in my index.gsp

<g:include controller="home" action="showMessage" />

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?

海风掠过北极光 2024-11-01 07:11:46

调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文