简体   繁体   English

斯卡拉播放! 形式+动态路线

[英]Scala Play! form + dynamic routes

As for now i had my url look like this: /chart?phrase=xxx 至于现在,我的网址看起来像这样:/ chart?phrase = xxx

I wanted to prettify it, so i've created a route: 我想美化它,所以我创建了一条路线:

GET     /chart/:phrase          controllers.Application.chart(phrase: String)

Here's my old form: 这是我的旧表格:

@form(routes.Application.chart) {

    @inputText(tagForm("phrase"), 'placeholder -> "Phrase") 

    <input type="submit" value="Search for phrase">
}

But compiler returns errors about missing parameters in form(call). 但是编译器会返回有关form(call)中缺少参数的错误。 How can i make this work? 我该如何工作?

Please notice, that i'm using custom class "tagForm". 请注意,我正在使用自定义类“ tagForm”。

change your url to 将您的网址更改为

GET     /chart/:phrase          controllers.Application.chart(phrase: String)

and at your template 并在您的模板

 routes.Application.chart("phraseValue")

now url looks like 现在网址看起来像

/chart/pharseValue

Edit : It wasn't clear that you didn't want query parameters, so have a look at the Reverse routing section of the Play Documentation . 编辑 :尚不清楚您是否不需要查询参数,因此请查看Play文档Reverse routing部分。


Query parameters aren't included in the route URI. 查询参数未包含在路由URI中。 There are two ways you can use them. 有两种使用方式。 One requires a default value, and the other uses an Option , depending on what suits your needs. 一个需要默认值,另一个需要使用Option ,这取决于您的需求。

#  Defaults to an empty string if the query parameter is not in the URL.
GET     /chart         controllers.Application.chart(phrase: String ?= "")

#  Defaults to `None` if the query parameter is not in the URL.
GET     /chart          controllers.Application.chart(phrase: Option[String])

The reverse routes would work as such: 反向路由可以这样工作:

// Routes with default values
routes.Application.chart("test")    // Outputs /chart?phrase=test
routes.Application.chart()    // Outputs /chart

// Routes using `Option`
routes.Application.chart(Some("test"))    // Outputs /chart?phrase=test
routes.Application.chart(None)    // Outputs /chart

I've found a solution. 我找到了解决方案。 Maybe it's not perfect, but suits my needs. 也许它不是完美的,但是适合我的需求。

To have pretty URLs when submitting a form I created two Routes: 为了在提交表单时拥有漂亮的URL,我创建了两个路由:

GET     /chart/:phrase          controllers.Application.chart(phrase: String)
GET     /getResult              controllers.Application.getResult

And two Actions: 和两个动作:

def chart(query: String) = Action { implicit request =>
    tagForm.bind(Map("phrase"->query)).fold(error,success)
}

def getResult = Action { implicit request =>
tagForm.bindFromRequest.fold(
  errors => BadRequest(...),
    req => {
      ...
      Redirect(routes.Application.chart(string))
    }
  )
}

And in form: @form(routes.Application.getResult) 并采用以下形式: @form(routes.Application.getResult)

Form submits tagForm to getResult, where after validation it redirects to pretty url. 表单将tagForm提交到getResult,在验证之后,它将重定向到漂亮的url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM