Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Let's assume we want to use the temperature conversion service from grails. In order to make the project minimal, we will modify the default view of the project, add a controller proxying to the service and generate a minimal view to display the result. Here we go:

Code Block
grails create-app hotstuff
cd hotstuff
vi grails-app/views/index.gsp

Modify the default view to

Code Block
xml
xml
<html>
  <head>
    <title>Welcome to Grails</title>
    <meta name="layout" content="main" />
  </head>
  <body>
    <g:form name="temperature" controller="temperature" action="conversion">
      <p>
        <g:set var="units" value="${['Fahrenheit', 'Celsius']}" />
        Convert <g:textField name="value" value="10.0" /> <g:select name="from" from="${units}" /> in <g:select name="to" from="${units}"/>
      </p>
      <g:submitButton name="submit" value="Convert" />
    </g:form>
  </body>
</html>
Code Block
grails create-controller temperature

Here is the controller code:

Code Block
import groovyx.net.ws.WSClient

class TemperatureController {

    def index = { }

    def conversion = {
        def proxy = new WSClient("http://www.w3schools.com/webservices/tempconvert.asmx?WSDL", this.class.classLoader)
        proxy.initialize()

        def message

        if (!params?.value || params.from.equals(params.to)) {
            message = "Units are probably the same. Don't aske me :)"
        } else {
            def result
            if ("Celsius".equals(params.from)) {
                result = proxy.CelsiusToFahrenheit(params.value)
            } else {
                result = proxy.FahrenheitToCelsius(params.value)
            }
            message = "${params.value} degrees ${params.from} are ${result} degrees ${params.to}"
        }

        flash.message = message
    }

}

Then, we create the view to display the result

Code Block
mkdir grails-app/views/temperature
vi grails-app/views/temperature/conversion.gsp

Here is the simplistic view:

Code Block
xml
xml
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="layout" content="main" />
        <title>Here we are</title>
    </head>
    <body>
        <div class="body">
            <g:if test="${flash.message}">
            <div class="message">${flash.message}</div>
            </g:if>
        </div>
    </body>
</html>

(error) Currently, the Grab annotation cannot be used without interfering with native grails jars, so you have to add the following jars into your application lib directory

Code Block
$ ls -lrt lib/
total 31960
-rw-r--r--  1 alleon  alleon    26072 Apr 15 11:01 groovyws-0.5.0-SNAPSHOT.jar
-rw-r--r--  1 alleon  alleon  4201229 Apr 15 11:03 cxf-bundle-2.1.4.jar
-rw-r--r--  1 alleon  alleon   520969 Apr 15 12:48 wstx-asl-3.2.7.jar
-rw-r--r--  1 alleon  alleon    60686 Apr 15 13:31 commons-logging-1.1.1.jar
-rw-r--r--  1 alleon  alleon  1485863 Apr 15 13:31 bcprov-jdk15-140.jar
-rw-r--r--  1 alleon  alleon   143806 Apr 15 13:31 XmlSchema-1.4.4.jar
-rw-r--r--  1 alleon  alleon   291779 Apr 15 13:31 FastInfoset-1.2.2.jar
-rw-r--r--  1 alleon  alleon   226915 Apr 15 13:31 jaxen-1.1.1.jar
-rw-r--r--  1 alleon  alleon  3127729 Apr 15 13:31 jaxb-xjc-2.1.9.jar
-rw-r--r--  1 alleon  alleon   856752 Apr 15 13:31 jaxb-impl-2.1.9.jar
-rw-r--r--  1 alleon  alleon    89967 Apr 15 13:31 jaxb-api-2.1.jar
-rw-r--r--  1 alleon  alleon    14611 Apr 15 13:31 geronimo-ws-metadata_2.0_spec-1.1.2.jar
-rw-r--r--  1 alleon  alleon    28804 Apr 15 13:31 geronimo-stax-api_1.0_spec-1.0.1.jar
-rw-r--r--  1 alleon  alleon    47817 Apr 15 13:31 geronimo-jaxws_2.1_spec-1.0.jar
-rw-r--r--  1 alleon  alleon   204851 Apr 15 13:31 geronimo-javamail_1.4_spec-1.5.jar
-rw-r--r--  1 alleon  alleon    12452 Apr 15 13:31 geronimo-annotation_1.0_spec-1.1.1.jar
-rw-r--r--  1 alleon  alleon    34126 Apr 15 13:31 geronimo-activation_1.1_spec-1.0.2.jar
-rw-r--r--  1 alleon  alleon   444503 Apr 15 13:31 xmlsec-1.4.2.jar
-rw-r--r--  1 alleon  alleon    84091 Apr 15 13:31 xml-resolver-1.2.jar
-rw-r--r--  1 alleon  alleon  3176148 Apr 15 13:31 xalan-2.7.1.jar
-rw-r--r--  1 alleon  alleon   313982 Apr 15 13:31 wss4j-1.5.6.jar
-rw-r--r--  1 alleon  alleon   148429 Apr 15 13:31 wsdl4j-1.6.2.jar
-rw-r--r--  1 alleon  alleon   278281 Apr 15 13:31 serializer-2.7.1.jar
-rw-r--r--  1 alleon  alleon   278145 Apr 15 13:31 saaj-impl-1.3.2.jar
-rw-r--r--  1 alleon  alleon    18817 Apr 15 13:31 saaj-api-1.3.jar
-rw-r--r--  1 alleon  alleon    32900 Apr 15 13:31 neethi-2.0.4.jar
-rw-r--r--  1 alleon  alleon   153115 Apr 15 13:31 jdom-1.1.jar

(warning) If you have been running our very first example all these jars are probably in your

Code Block
~/.groovy/grapes/

directory

BuildConfig.groovy

If you reference groovyws in your grails app then you will need a set of exclusions to prevent version conflicts ( from list Nabble - Ronny Løvtangen ):

Code Block
compile("org.codehaus.groovy.modules:groovyws:0.5.2") {
    excludes 'geronimo-servlet_2.5_spec', 'servlet-api', 'jaxb-xjc', 'jaxb-impl', 'xml-apis', 'saaj-impl', 'junit', 'slf4j-jdk14', 'xmlParserAPIs', 'jaxb-api', 'saaj-api', 'xmlbeans', 'jaxen', 'geronimo-stax-api_1.0_spec', 'geronimo-activation_1.0.2_spec', 'abdera-client', 'geronimo-activation_1.1_spec'
}