Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data to "View" from "Controller"

Tags:

grails

groovy

In my controller class have the following code

class MyController {

    def flickrService

    def index = {
        def data = flickrService.search {
            tags 'tag,tag2,tag3'
            page 3
            perPage 14 // Look ma!
        }
        [urls:data.urls,page:data.page,pages:data.pages]
    }

}

I have also created an index.gsp file. As I am new to groovy grails - i could not figure it out how to access data returned by flickrservice in the view. Can I just access "data" defined above in the index view or I need to set it in the controller before I can loop through the returned data? Any help would be highly appreciated. Thanks

like image 884
Kay Avatar asked Apr 21 '26 17:04

Kay


1 Answers

Yes, now you can access data from the view, for example,in index.gsp:

<html><head>Test</head><body>${urls} <br/> ${page} </body></html>

Generally saying, grails return the last value in function by default, so if you want to access many data, you can do like this:

class MyController {

    def flickrService

    def index = {
        def data = ...
        def data1 = ...
        def data2 = ...

        // Here's the return result:
            [view_data:data,view_data1:data1, view_data2:data2]
    }

}

Then you can access ${view_data},${view_data1},${view_data2} in view.

like image 136
Hoàng Long Avatar answered Apr 24 '26 06:04

Hoàng Long



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!