How to invoke a service directly from GSP view (Grails)

Posted: Fri, Mar 11 01:40 PM (PST)

In your GSP view page you can use below code, but I don't recommend you use this solution

<%@ page import="my.project.MyService" %>
<% def myService = grailsApplication.classLoader.loadClass('my.project.MyService').newInstance() %>

And then you just call ${myService.method()} in your gsp view

While this works, it would be far more elegant to either inject the service into the view via model or to create a taglib for the service and use this from the gsp view.

Tags: