How to read query parameters in Revel using the 'routes' file?

huangapple go评论73阅读模式
英文:

How to read query parameters in Revel using the 'routes' file?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对Revel和golang还不熟悉。我需要帮助,了解如何在Revel中访问查询参数并在路由中进行配置。

例如:localhost:9000/company?name=ABC

我发送了上述的GET请求,从数据库中获取公司名称为ABC的公司。我不知道如何在routes文件中进行配置。

我的操作方法如下:

func (c APP) ShowByName(name string){..}

以下是我设置的路由:

GET /company?name:name      APP.ShowByName
英文:

I am new to Revel and golang. I need help as to how can I access query paramters in Revel and configure it in routes.

Example: localhost:9000/company?name=ABC.

I am sending above get request to fetch the company by name from database. I don't know how can I configure it in the routes file.

My action has

func (c APP) ShowByName(name string){..}

This is how I have set the routes:

GET /company?name:name      APP.ShowByName

答案1

得分: 0

根据revel文档,所有的请求参数都被收集到一个名为Params的对象中,其中包括:

  • URL的/:path参数
  • URL的?query参数
  • 提交的表单值
  • 文件的多部分上传

提取的参数可以通过Action方法的parameters来获取。

type Params struct {
	url.Values
	Files map[string][]*multipart.FileHeader
}

通过检查Params结构,我们可以观察到它使用了url.Values

根据上述信息,查询参数的URL路由可以设置为:

GET    /company/:name?name            APP.ShowByName

控制器的动作可以定义为:

func (c APP) ShowByName(name string) revel.Result {
    ...
}
英文:

According to revel documentation, all request parameters are collected into the single Params object which includes:

The URL /:path parameters
The URL ?query parameters
Submitted Form values
File multipart uploads

Extracted parameters are available via Action method parameters.

type Params struct {
	url.Values
	Files map[string][]*multipart.FileHeader
}

Checking the Params struct we can observe that it use url.Values

According to above information the query param url route can be set as:

GET    /company/:name?name            APP.ShowByName

And the controller action:

func (c APP) ShowByName(name string) revel.Result {
    ...
}

答案2

得分: 0

我找到了解决方案。

在操作中,我放置了以下代码:

c.Params.Query=c.Request.URL.Query()
var limit int
c.Params.Bind(&limit,"limit")

并且它适用于以下URL:

localhost:9000?limit=21

这是完整的代码:
revel读取查询参数

英文:

I found solution to this.

In action I placed the below code :

c.Params.Query=c.Request.URL.Query()
var limit int
c.Params.Bind(&limit,"limit")

and it work for the below URL:

localhost:9000?limit=21

Here is the complete code for that
revel read query parameter

答案3

得分: 0

URL查询参数会自动传递给控制器。

在你的routes文件中,只需指定URL路径(忽略查询字符串)。
对于问题中给出的示例localhost:9000/company?name=ABC,你只需写入以下内容:

(routes文件)

GET /company      SomeController.ShowByName

然后在控制器中,只需添加函数参数。Revel会将它们绑定到相应的查询字符串值:

func (c SomeController) ShowByName(name string) revel.Result {
  if name == "" {
    // 处理缺失或为空的情况
  }
  // ...
}
英文:

URL query parameters are automatically passed to controllers.

In your routes file, just specify the URL path (ignoring the query string).
For the example localhost:9000/company?name=ABC given in the question, you would just have:

(routes file)

GET /company      SomeController.ShowByName

Then within the controller, just add function parameters. Revel will bind these to the appropriate query string values:

func (c SomeController) ShowByName(name string) revel.Result {
  if name == "" {
    // handle missing or empty
  }
  // ...
}

huangapple
  • 本文由 发表于 2016年2月16日 21:31:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/35433979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定