0

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"
    }

}
Community
  • 1
  • 1
S.P.
  • 2,274
  • 4
  • 26
  • 57

1 Answers1

1

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"/>
tylerwal
  • 1,858
  • 2
  • 15
  • 20
  • 2
    Don't use `remoteLink` or any of the `remote` tags. They are old, they are no longer being supported, they are going away in modern versions of Grails. It's a bad practice and you should stop using them. – Joshua Moore Dec 01 '15 at 13:54
  • I add that you told me:
  • ${myService.calculate()}
  • ${LicenceTypeService.recalculate()}
  • But I still have same problem – S.P. Dec 01 '15 at 13:59
  • @JoshuaMoore, thanks for the info. Is there a modern tag for doing Ajax calls? I use angular and its $http provider, so I'm not familiar with the strict grails way of doing things. – tylerwal Dec 01 '15 at 14:06
  • @D.O., if you are referring to your initial question of remotely calling a service method, then yes, the `` syntax will not help, it's just a better way of getting a reference to a service. – tylerwal Dec 01 '15 at 14:10
  • 1
    @tylerwal any modern javascript framework (e.g. jQuery) has their own means to standardize AJAX calls. There is no Grails tag library that I am aware of that attempts to reproduce the same functionality of the `remote` tags. Since the idea was flawed. – Joshua Moore Dec 01 '15 at 14:10