How to call a Grails service in a view?

fabien7474 picture fabien7474 · Oct 22, 2009 · Viewed 8.9k times · Source

Simple question : I have a service class (let's say helpersService) and a method def constructURI(params). How can I call this method from a template view.

I have tried the following code without success

<% def helpersService  = new HelpersService() // or def helpersService
%>
<img src="${helpersService. constructURI(params)}"/>

But I get the following result:

No signature of method: com.HelpersService. constructURI() is applicable for argument types...

or (in case I use def helpersService)

Cannot invoke method constructURI() on null object 

Any ideas?

Answer

Siegfried Puchbauer picture Siegfried Puchbauer · Oct 22, 2009

Services are not intended to be used inside views. You could create a TagLib where you can get a reference to the service via dependency injection.