DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Languages
  4. Griffon Front-End, Grails Back-End: Groovy End-to-End

Griffon Front-End, Grails Back-End: Groovy End-to-End

Mitch Pronschinske user avatar by
Mitch Pronschinske
·
Feb. 15, 10 · Interview
Like (1)
Save
Tweet
Share
16.73K Views

Join the DZone community and get the full member experience.

Join For Free

The development teams for various Groovy projects have done a great job making all of their frameworks work well together when building and deploying applications.  If you visit the Network section of Griffon plugins, you might notice that all of those plugins can be used to create Griffon front-ends for your web applications that can talk to a Grails back-end, or any other back-end that supports those technologies.  For example, the Wsclient plugin can create a service in Grails and export it via web services.  That service can then be accessed from the Griffon front-end.  With Grails' excellent support for exposing REST-like behavior and Griffon's extensive plugin architecture, Groovy becomes a formidable end-to-end solution for web applications and web services.

To configure a service on the Grails side simply create a new Grails application:

grails create-app example
Then change into the application's directory and install the Grails Xfire plugin:
grails install-plugin xfire
The Wsclient plugin, which adds a remoting client capable of communicating via SOAP, is compatible with Xfire plugin 0.8.1.

Next, create a MathService:
grails create-service MathService
grails-app/services/MathService.groovy

class MathService {
boolean transactional = false
static expose = ["xfire"]

double add(double arg0, double arg1){
println "add($arg0, $arg1)" // good old println() for quick debugging
return arg0 + arg1
}
}

Finally, run the application:

grails run-app


The next step is to build the Griffon application.  This one is named MathClient:

griffon create-app mathClient

And install the wsclient plugin:

griffon install-plugin wsclient

Modify the view script until it looks like this:

griffon-app/views/MathClientView.groovy

application(title: 'Wsclient Plugin Example',
pack: true,
locationByPlatform: true,
iconImage: imageIcon('/griffon-icon-48x48.png').image,
iconImages: [imageIcon('/griffon-icon-48x48.png').image,
imageIcon('/griffon-icon-32x32.png').image,
imageIcon('/griffon-icon-16x16.png').image]) {
gridLayout(cols: 2, rows: 4)
label("Num1:")
textField(columns: 20, text: bind(target: model, targetProperty: "num1"))
label("Num2:")
textField(columns: 20, text: bind(target: model, targetProperty: "num2"))
label("Result:")
label(text: bind{model.result})
button("Calculate", enabled: bind{model.enabled}, actionPerformed: controller.calculate)
}

Add required properties to the model:

griffon-app/models/MathClientModel.groovy

import groovy.beans.Bindable

class MathClientModel {
@Bindable String num1
@Bindable String num2
@Bindable String result
@Bindable boolean enabled = true
}

Here is the controller code.  There is minimal error handling in place so if the user types something that is not a number, the client will break:

griffon-app/controllers/MathClientController.groovy

class MathClientController {
def model

def calculate = { evt = null ->
double a = model.num1.toDouble()
double b = model.num2.toDouble()
model.enabled = false
doOutside {
try {
def result = withWs(wsdl: "http://localhost:8080/exporter/services/math?wsdl") {
add(a, b)
}
doLater { model.result = result.toString() }
} finally {
model.enabled = true
}
}
}
}

Finally, start the application:

griffon run-app

When running, the wsclient plugin will add dynamic methods to controllers (the withWs() method at line 12).  A new wsclient proxy will be created every time you call those methods unless you define an id: attribute.  You can access the client via regular property access or using the id: again.  Dynamic methods will be added to controllers by default, but you can change this setting by adding a config flag in Application.groovy:

griffon.wsclient.injectInto = ["controller", "service"]

Other plugins like hessian, RMI, or XML-RPC follow the same principles in this example.  

Grails works well as a back-end because it's good at outputting XML and easily ingesting it back.  Scott Davis's Mastering Grails series gives a detailed demonstration for exposing REST-like behavior with Grails.  For enabling the usage of HTTPBuilder on a Griffon or Grails application, there are REST plugins available.

Here's an example from HTTPBuilder's Simplified GET Request using the REST plugin for Grails or Griffon:

withHttp(uri: "http://www.google.com") {
def html = get(path : '/search', query : [q:'Groovy'])
assert html.HEAD.size() == 1
assert html.BODY.size() == 1
}

The current HTTPBuilder is set as the closure's delegate.  The same is true for the other dynamic methods

Here's an example from AsyncHTTPBuilder:

import static groovyx.net.http.ContentType.HTML

withAsyncHttp(poolSize : 4, uri : "http://hc.apache.org", contentType : HTML) {
def result = get(path:'/') { resp, html ->
println ' got async response!'
return html
}
assert result instanceof java.util.concurrent.Future

while (! result.done) {
println 'waiting...'
Thread.sleep(2000)
}

/* The Future instance contains whatever is returned from the response
closure above; in this case the parsed HTML data: */
def html = result.get()
assert html instanceof groovy.util.slurpersupport.GPathResult
}

Once again, all dynamic methods will create a new http client when invoked unless you define an id: attribute.

class FooController {
def loginAction = { evt = null ->
withRest(id: "twitter", uri: "http://twitter.com/statuses/") {
auth.basic model.username, model.password
}
}

def queryAction = { evt = null ->
doOutside {
withRest(id: "twitter") {
def response = get(path: "followers.json")
// ...
}
/* alternatively
def response twitter.get(path: "followers.json")
*/
}
}
}

 

For the entire Mastering Grails series, you can follow this link. 
Grail (web browser) Griffon (framework) application Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Deep Dive Into AIOps and MLOps
  • 5 Common Firewall Misconfigurations and How to Address Them
  • Spring Cloud
  • An End-to-End Guide to Vue.js Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: