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

Trending

  • Enriching Kafka Applications With Contextual Data
  • Develop Hands-Free Weather Alerts To Ensure Safe Backpacking
  • Creating Scalable OpenAI GPT Applications in Java
  • How To Integrate Microsoft Team With Cypress Cloud

Grails Goodness: Get Request Parameters with Default Values

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Nov. 25, 13 · Interview
Like (0)
Save
Tweet
Share
9.51K Views

Join the DZone community and get the full member experience.

Join For Free

In Grails we can convert a request parameter to a type directly. We must then use the int(), short(), byte(), long(), double(),float(), boolean() or list() methods that are added to the params object available in our controllers.

Since Grails 2.3 we can also pass a default value, which is used when the request parameter is not set. In the following controller we use thedouble() method and define a default value of 42.0.

// File: grails-app/controllers/com/mrhaki/grails/SampleController.groovy
package com.mrhaki.grails

class SampleController {

    def index() {
        // Get request parameter named v.
        // Use default value 42.0 if not set.
        final double value = params.double('v', 42.0)
        [value: value]
    }

}

The following test shows that the default value is returned if the request parameter is not set, otherwise the value of the request parameter is returned:

// File: test/unit/com/mrhaki/grails/SampleControllerSpec.groovy
package com.mrhaki.grails

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(SampleController)
class SampleControllerSpec extends Specification {

    def "request parameter v must return default value if not set"() {
        expect:
        controller.index().value == 42.0
    }

    def "request parameter v must return value set"() {
        given:
        params.v = '10.1'

        expect:
        controller.index().value == 10.1
    }

}

We can use the same methods now also to get attribute values in a tag library. So we can do a type conversion and provide a default value if we want to. In the following tag library we use this in the tag sample:

// File: grails-app/taglib/com/mrhaki/grails/SampleTagLib.groovy
package com.mrhaki.grails

class SampleTagLib {

    static namespace = 'sample'

    static returnObjectForTags = ['sample']

    def sample = { attributes, body ->
        final double value = attributes.double('v', 42.0)
        value
    }
    
}

With the following Spock specification we can see that the default value 42.0 is used if the attribute v is not set. Otherwise the value of the attribute is returned:

// File: test/unit/com/mrhaki/grails/SampleTagLibSpec.groovy
package com.mrhaki.grails

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(SampleTagLib)
class SampleTagLibSpec extends Specification {

    def "attribute v returns default value if attribute is not set"() {
        expect:
        applyTemplate('<sample:sample/>') == '42.0'
    }

    def "attribute v returns value if attribute v if set"() {
        expect:
        applyTemplate('<sample:sample v="${v}"/>', [v: 10.1]) == '10.1'
    }

}

Code written with Grails 2.3.



Grail (web browser) Requests

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Enriching Kafka Applications With Contextual Data
  • Develop Hands-Free Weather Alerts To Ensure Safe Backpacking
  • Creating Scalable OpenAI GPT Applications in Java
  • How To Integrate Microsoft Team With Cypress Cloud

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

Let's be friends: