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 ) ]
}
}
For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.
A URL parameter is a way to pass data in a link to a web page. You can use URL parameters to track different metrics in services like Google Analytics or Google Ads, and in Unbounce to pre-fill your form.
URL parameter is a way to pass information about a click through its URL. You can insert URL parameters into your URLs so that your URLs track information about a click. URL parameters are made of a key and a value separated by an equals sign (=) and joined by an ampersand (&).
How Do You Identify a URL Parameter? To identify a parameter in a URL, you need to look for a question mark and an equals symbol within a URL. In this case, the “?” denotes the start of the parameter. The term “productid” is in of itself the parameter and in this case is designated as a product ID number.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With