简体   繁体   中英

Grails g:include tag to include controller call

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM