Grails 2.3.x: get the value of URL parameters

Based on URL

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

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

In the controller:

`println params.format

Grails Documentation Link

But params.format 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 ) ] } } 
+6
source share
2 answers

You are one of the most successful victims of the agreement compared to the configuration .;)

An entry with the format key is added to params , as indicated in the default URL, which represents the type of response that is expected (usually whether xml / json) will 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 populated with the type of content you request. It also means that a query parameter named format will be overridden and lost.

You can rename the request parameter to a value other than format , then it should be available in the controller, for example param.blah , if the request parameter has blah=excel .

OR

change url and remove optional (.$format)? if it is not required:

 "/$controller/$action?/$id?(.$format)?"{ constraints { // apply constraints here } } 
+5
source

since format is a Grails platform token, find below. Another way to fix this problem is by 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] } 
0
source

All Articles