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
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 ) ]
}
}
You are one of the luckiest victim of convention over configuration. ;)
An entry with key
format
is added toparams
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:format
gets populated by the type of content you are asking for. Which also means, a request parameter with nameformat
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 likeparam.blah
if request parameter hasblah=excel
.OR
modify url mapping and remove the optional
(.$format)?
if not required:since
format
is token by the Grails platform , find below Another method to fix this problem via addingmapExtensionFormat
varaible :Then :