{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

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

Published by

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.

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

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



Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB.

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks