Loading...

Wednesday, August 28, 2013

Grails Goodness: Use Services in GSP with g:set Tag

In Grails we can use the set tag to define a variable with a value on a GSP. But we can also use the same tag to access a Grails service on a GSP. We must use the bean attribute with the name of the Grails service. We can even pass the name of any Spring managed bean in the application context of our application.

In the following sample GSP we access a Grails service with the name BlogService. We invoke the method allTitles() and use the result.

<html>
<head>
    <meta content="main" name="layout"/>
</head>
<body>
%{--Use BlogService--}%
<g:set var="blog" bean="blogService"/>

<ul>
    <g:each in="${blog.allTitles()}" var="title">
        <li>${title}</li>
    </g:each>
</ul>

</body>
</html>

5 comments:

Jasen said...

No. Poor practice. Controller should call the Service and pass the data to the View. It's nice that Grails lets you do it, but it breaks the MVCS pattern.

Ummey Rezwana said...

Thanks for sharing such kind of nice and wonderful collection......Nice post Dude keep it up.
I have appreciate with getting lot of good and reliable and legislative information with your post......
Test and Tag brisbane
rcd testing services brisbane

Rezwana Rahman said...

Great Content.I have appreciate with getting lot of good and reliable information with your post.......
Thanks for sharing such kind of nice and wonderful collection......again, beautiful :) I love reading your posts. They make me happy . . I know something information, to know you can click here
Test and Tag brisbane
rcd testing services brisbane

Anonymous said...

You always reveal the most amazing nuggets.
Great framework - great share!
Thx Mr.Haki

testing3412 said...

It is truly a great and helpful piece of information.
I am satisfied that you simply shared this useful information with us.
Please stay us informed like this. Thanks for sharing. I know something information, to know you can click here
Test and Tag brisbane
rcd testing services brisbane

Post a Comment