6

Given the URL

  http://localhost:9000/Estrategia/book/index?format=excel&extension=xls

I want to get the format value (in this case is excel)

In the controller:

`println params.format

Grails docs reference

But params.format is always null, any idea?

Grails 2.3.5

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class BookController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def exportService // Export service provided by Export plugin
    def grailsApplication  //inject GrailsApplication

   def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)

    if(!params.max) 
    params.max = 10

    println params?.format
    [ bookInstanceList: Book.list( params ) ]
  }
}
Alberici
  • 357
  • 2
  • 19
  • 1
    Can you add your controller to question? – dmahapatro Apr 05 '14 at 15:22
  • Where is `foo`? How is `/hello?foo=bar` mapped to this controller? were you referring to `params?.format`? What is the actual url you are trying to hit? *A problem well stated is a problem half solved* - I think you would agree to that. :) – dmahapatro Apr 05 '14 at 16:12

2 Answers2

6

You are one of the luckiest victim of convention over configuration. ;)

An entry with key format is added to params as referred by default url mapping which represents the type of response that is expected (generally, whether xml/json) will be also be used for content negotiation which means, as an example, if you use:

http://localhost:9000/Estrategia/book/index.xml
//params -- [action:index, format:xml, controller:book]

http://localhost:9000/Estrategia/book/index.json
//params -- [action:index, format:json, controller:book]

http://localhost:9000/Estrategia/book/index.json?format=excel&extension=xls
//params -- [action:index, format:json, extension:xls, controller:book]

http://localhost:9000/Estrategia/book/index?format=excel&extension=xls
//params -- [action:index, format:null, extension:xls, controller:book]

format gets populated by the type of content you are asking for. Which also means, a request parameter with name format will get overridden and will be lost.

You can rename the request parameter to something other than format then it should be available in controller like param.blah if request parameter has blah=excel.

OR

modify url mapping and remove the optional (.$format)? if not required:

"/$controller/$action?/$id?(.$format)?"{
     constraints {
         // apply constraints here
     }
}
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Thank you for all your patience and very good explained answers. This seems weird to me, because I was using a Grails Plugin [Grails Export plugin](http://grails.org/plugin/export) – Alberici Apr 05 '14 at 21:41
  • Yes I see that `params.format` is used in the plugin. Plugin is too old. Right now, if possible modify the mapping, I would work on a PR for the plugin to adapt to newer versions of Grails. – dmahapatro Apr 05 '14 at 21:49
  • 1
    Another StackOverflow page that could be of interest for anybody: http://stackoverflow.com/questions/19961339/grails-export-plugin-dont-download-any-file – Alberici Apr 05 '14 at 21:53
  • LOL. It is [fixed in export plugin version 1.5](https://github.com/nwwells/grails-export/issues/5). Upgrade the plugin and use `exportFormat` instead of `format`. From the fix, I see a [SO answer related](http://stackoverflow.com/questions/19961339/grails-export-plugin-dont-download-any-file/19970759#19970759). – dmahapatro Apr 05 '14 at 21:53
  • Worth upgrading the README file in github with those changes and refer the github repo from the plugin page. – dmahapatro Apr 05 '14 at 21:58
  • @Alberici Also note the PR is not merged yet. – dmahapatro Apr 05 '14 at 22:02
  • in this case I could also do a: "renaming params.format to params.formatt" [the other SO question](http://stackoverflow.com/questions/19961339/grails-export-plugin-dont-download-any-file), for doing that renaming I need to enter to the plugin taglib and make the rename there right? – Alberici Apr 06 '14 at 21:46
0

since format is token by the Grails platform , find below Another method to fix this problem via adding mapExtensionFormat varaible :

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
static mapExtensionFormat=['pdf':'pdf','xls':'excel','csv':'csv','rtf':'rtf']
def exportService // Export service provided by Export plugin
def grailsApplication  //inject GrailsApplication

Then :

   def index(Integer max) {
        // ...
        String format=mapExtensionFormat[params?.extension]

  }
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254