Search code examples
grailsgsp

Methods in service doesn't work ok in the gsp file


I have a list with two buttons and I want that everytime that I press a button, work the code that I have associated to this button. This code is in a service, and the gsp file is a default list.gsp

To call the service from the gsp. I took this post:(How do I call a Grails service from a gsp?) to do it.

The problem is that I have two methods in the service and I don't know why, but always that I enter in the list.gsp and always that I press the button I can see display in the console of grails that the two methods are working at the same time.

This is that I can see in the console everytime I push button A or B:

2015-12-01 12:51:47,565 [http-bio-8090-exec-5] 
hello
world

That I want is that if I press button A show this:

2015-12-01 12:51:47,565 [http-bio-8090-exec-5] 
    hello

and if I press button B show this:

2015-12-01 12:51:47,565 [http-bio-8090-exec-5] 
    world

And this is that I can see in the console everytime that I run and enter in the gsp.file and I want that don't run the code unless I press the button:

2015-12-01 12:51:47,565 [http-bio-8090-exec-5] 
hello
world

Thanks in advance

This is my code in gsp:

<%@ page import="com.app.test" %>
<%@ page import="mypackage.myService" %>
<!DOCTYPE html>
<html>
    <head>
        <meta name="layout" content="main">
        <g:set var="entityName" value="${message(code: 'licenseType.label', default: 'LicenceType')}" />
        <title><g:message code="default.list.label" args="[entityName]" /></title>
    </head>
    <body>
        <a href="#list-licenseType" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
        <div class="nav" role="navigation">
            <ul>
                <li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>

                <% def LicenceTypeService = 
                grailsApplication.classLoader.loadClass('mypackage.myService').newInstance()%>

                <li><g:link action="list"><g:message code="Calculate Licence Type" args="[entityName]" /><span>${myService.calculate()}</span></g:link></li>
                <li><g:link action="list"><g:message code="Re-Calculate Licence Type" args="[entityName]" /><span>${myService.recalculate()}</span></g:link></li>       
            </ul>
        </div>

In the service:

class MyService {

    def calculate(){
        println "hello"
    }

    def recalculate(VoiceUser vUser){
        println "world"
    }

}

Solution

  • I'm fairly certain this is impossible; what you are asking is to send an Ajax call directly to a grails service. If you think about it, once the generated HTML has been received by the client, there is not a direct connection between the browser and back-end (grails application) so there can't be any grails magic that allows this to happen.

    There is a grails tag g:remoteLink that will allow Ajax calls to a controller action, the controller is necessary at this point because all new interactions once the generated html is in place must be through standard HTTP requests and requests are handled through grails controllers.

    Your best bet would be to create a controller that wraps the 2 service methods and use the actions with g:remoteLink or another Ajax request.


    As to using a service within a gsp, instead of instantiating a new service, I would argue in favor of using the existing spring bean.

    Replace:

    <%@ page import="mypackage.myService" %>
    ...
    <% def LicenceTypeService = grailsApplication.classLoader.loadClass('mypackage.myService').newInstance()%>
    

    With:

    <g:set var="myService" bean="myService"/>