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

Grails Goodness: Binding Method Arguments in Controller Methods

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Mar. 02, 12 · Interview
Like (0)
Save
Tweet
Share
8.66K Views

Join the DZone community and get the full member experience.

Join For Free

Since Grails 2.0 we can use methods instead of closures to define actions for our controllers. We could already pass a command object to a method as argument, but we can also use primitive typed arguments in our method definition. The name of the argument is the name of the request parameter we pass to the controller. Grails will automatically convert the request parameter to the type we have used in our method definition. If the type conversion fails then the parameter will be null.

Let's create a method in a controller with three arguments: a String typed argument with the names author and book. And an argument with type Long with the name id.

// File: grails-app/controllers/sample/MethodSampleController.groovy
package sample

class MethodSampleController {
    /**
     * Sample method with 3 arguments.
     *
     * @param author Name of author
     * @param id Identifier for author
     * @param book Book title 
     */
    def sample(final String author, final Long id, final String book) {
        render "Params: author = $author, book= $book, id = $id"
    }

}

If we invoke our controller with http://localhost:8080/grails-samples/methodSample/sample?id=100&book=It&author=Stephen%20King we get the following output:

Params: name= Stephen King, book = It, id = 100

Suppose we don't provide a valid long value for the id parameter we see in the output id is null. We use the following URL http://localhost:8080/grails-samples/methodSample/sample?id=1a&book=The%20Stand&author=Stephen%20King.

Params: author = Stephen King, book = The Stand, id = null

After reading this blog post and looking at the Grails documentation I learned we can even change the name of the argument and map it to a request parameter name with the @RequestParameter annotation. So then the name of the argument and the request parameter don't have to be the same.

Let's change our sample method and see what the output is:

// File: grails-app/controllers/sample/MethodSampleController.groovy
package sample

import grails.web.RequestParameter

class MethodSampleController {
    /**
     * Sample method with 3 arguments.
     *
     * @param author Name of author
     * @param id Identifier for author
     * @param book Book title 
     */
    def sample(final String author, @RequestParameter('identifier') final Long id, @RequestParameter('bookTitle') final String book) {
        render "Params: author = $author, book = $book, id = $id"
    }

}

Now we need the following URL to see correct output: http://localhost:8080/controllers/author/sample?bookTitle=It&identifier=200&author=Stephen%20King.

Params: name= Stephen King, book = It, id = 200

 

From http://mrhaki.blogspot.com/2012/02/grails-goodness-binding-method.html

Grail (web browser) Binding (linguistics)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What’s New in Flutter 3.7?
  • An End-to-End Guide to Vue.js Testing
  • Collaborative Development of New Features With Microservices
  • Stateful Stream Processing With Memphis and Apache Iceberg

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: