加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 在Play Framework 2.0模板中使用选项助手

发布时间:2020-12-16 09:43:33 所属栏目:安全 来源:网络整理
导读:我试图使用views.html.helper.select(documentation here)。我不知道scala,所以我使用java。我需要将类型为Seq [(String)(String)]的对象传递给模板吗?就像是: @(fooForm:Form[Foo])(optionValues:Seq[(String)(String)])@import helper._@form(routes.fo
我试图使用views.html.helper.select(documentation here)。我不知道scala,所以我使用java。我需要将类型为Seq [(String)(String)]的对象传递给模板吗?就像是:

@(fooForm:Form[Foo])(optionValues:Seq[(String)(String)])

@import helper._

@form(routes.foo){
  @select(field=myForm("selectField"),options=optionValues)
}

我不知道如何在java中创建Seq [(String)(String)]。我需要从我的枚举类中填入这个集合(id,title)。

有人可以向我展示如何使用选择帮助器吗?

我在用户组中发现了this,但Kevin的回答并没有帮助我。

解决方法

正确的类型是:Seq [(String,String)]。这意味着一串String对。在Scala中有一种使用箭头定义成对的方法:a-> b ==(a,b)。所以你可以写下如:

@select(field = myForm("selectField"),options = Seq("foo"->"Foo","bar"->"Bar"))

但是,如文档所示,还有另一个帮助器,构建选择选项的顺序:选项,所以可以将上述代码重写为:

@select(myForm("selectField"),options("foo"->"Foo","bar"->"Bar"))

在您的选项值与其标签相同的情况下,您甚至可以将代码缩短为:

@select(myForm("selectField"),options(List("Foo","Bar")))

(注意:在Play 2.0.4选项(List(“Foo”,“Bar”))不编译,所以你可以尝试这个选项(Seq(“Foo”,“Bar”)))

为了从Java代码中填充选项,更方便的方法是使用重载的options函数,使用java.util.List< String>作为参数(在这种情况下,选项值将与其标签相同)或使用java.util.Map< String,String>的重载函数。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读