Grails 2.3.x:获取URL参数的值 [英] Grails 2.3.x: get the value of URL parameters

查看:96
本文介绍了Grails 2.3.x:获取URL参数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定URL

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

我想获得格式值(在这种情况下是Excel)

在控制器中:

`println params.format

Grails文档参考



但是 params.format 总是为空,有什么想法?

Grails 2.3.5

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

@Transactional(readOnly = true)
类BookController {

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

def exportService //导出由导出插件提供的服务
def grailsApplication //注入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)]
}
}


解决方案

你是Convention over Config中最幸运的受害者之一。 ;)



格式的条目被添加到 params 默认情况下,url映射表示预期的响应类型(通常是xml / json)是否也将用于内容协商,例如,如果您使用:

  http:// localhost:9000 / Estrategia / book / index.xml 
// params - [action:index,format:xml ,控制器: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,格式:null,扩展名:xls,控制器:book]

格式会被您要求的内容类型填充。这也意味着,名为 format 的请求参数将被覆盖并丢失。



您可以重命名如果请求参数有格式,那么它应该在控制器中可用,如 param.blah c $ c> blah = excel





修改网址映射和如果不需要,删除可选的(。$ format)?

  / $控制器/ $动作?/ $ id?(。$格式)?{
约束{
//在这里应用约束
}
}


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 ) ]
  }
}

解决方案

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
     }
}

这篇关于Grails 2.3.x:获取URL参数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆