Playframework 多个可选参数-如何使用管线和模板

Playframework 多个可选参数-如何使用管线和模板,playframework,playframework-2.1,Playframework,Playframework 2.1,因为我从Play framework(2.1.3)开始 这对我来说似乎是一个简单的问题,但我还没有找到解决办法 也许有一种完全不同的方法可以让它工作,这就是为什么我没有找到这方面的例子 我想显示一个基于三个可选筛选值的值列表 我的控制器接受三个参数 public static Result index(Integer filterA, String filterB, String filterC) 向其发送请求的路由 GET / controllers.Teachers.index(filte

因为我从Play framework(2.1.3)开始 这对我来说似乎是一个简单的问题,但我还没有找到解决办法

也许有一种完全不同的方法可以让它工作,这就是为什么我没有找到这方面的例子

我想显示一个基于三个可选筛选值的值列表

我的控制器接受三个参数

public static Result index(Integer filterA, String filterB, String filterC)
向其发送请求的路由

GET / controllers.Teachers.index(filterA :Integer = null, filterB = null, filterC = null)
这将接受URL,如
localhost:9000/?filterA=10&filterB=20&filterC=test

只需单击一个值,就可以通过三个列表选择过滤器,因此我在模板中的链接如下所示

<a href="@routes.Teachers.index()?filterA=10">Value for filter A</a>
<a href="@routes.Teachers.index()?filterB=20">Value for filter B</a>
<a href="@routes.Teachers.index()?filterC=test">Value for filter C</a>

我认为这不是一种“播放方式”,因为我在生成URL时无法控制播放。此外,当我想同时设置两个过滤器时,我必须将所选过滤器传递给我的模板(参数或会话),并具有如下复杂链接:

<a href="@routes.Teachers.index()?filterA=10&filterB=@selectedB&filterC=@selectedC">Value for filter A</a>
<a href="@routes.Teachers.index()?filterA=@selectedA&filterB=20&filterC=@selectedC">Value for filter B</a>
<a href="@routes.Teachers.index()?filterA=@selectedA&filterB=@selectedB&filterC=test">Value for filter C</a>


因此,在我看来,这是一个非常常见的用例,但我还没有弄清楚如何在play中轻松地做到这一点:)

您必须通过在模板中向动作传递参数来调用动作:

<a href="@routes.Teachers.index(10, null, null)">Value for filter A</a>
<a href="@routes.Teachers.index(null, 20, null)">Value for filter B</a>
<a href="@routes.Teachers.index(null, null, "test")">Value for filter C</a>
或者,如果您想让它们成为可选的,您可以调用它们,如果您的模板具有:

<a href="@routes.Teachers.index(filterA = 10)">Value for filter A</a>
<a href="@routes.Teachers.index(filterB = 20)">Value for filter B</a>
<a href="@routes.Teachers.index(filterC = "test")">Value for filter C</a>
注意,Java的
Integer
,应该是路由文件中Scala的
Int

public static Result index(Integer filterA, String filterB, String filterC) {
    if (filterA == 0 && filterB == null && filterC == null) {
        return badRequest("No filter data...");
    }
    return ok("some filtering");
}

必须通过在模板中向操作传递参数来调用该操作:

<a href="@routes.Teachers.index(10, null, null)">Value for filter A</a>
<a href="@routes.Teachers.index(null, 20, null)">Value for filter B</a>
<a href="@routes.Teachers.index(null, null, "test")">Value for filter C</a>
或者,如果您想让它们成为可选的,您可以调用它们,如果您的模板具有:

<a href="@routes.Teachers.index(filterA = 10)">Value for filter A</a>
<a href="@routes.Teachers.index(filterB = 20)">Value for filter B</a>
<a href="@routes.Teachers.index(filterC = "test")">Value for filter C</a>
注意,Java的
Integer
,应该是路由文件中Scala的
Int

public static Result index(Integer filterA, String filterB, String filterC) {
    if (filterA == 0 && filterB == null && filterC == null) {
        return badRequest("No filter data...");
    }
    return ok("some filtering");
}

谢谢,因为我希望参数是可选的,所以我尝试了第二种解决方案,但在我的模板中出现了编译错误:方法索引的参数太多:()play.api.mvc.CallI从错误的控制器调用了该操作(应该是
Teachers
而不是
Application
no?@ThomasK检查我的编辑,可以,尼科·埃基托我希望你不要;t介意:)太好了,非常感谢尼科·埃基托和比西尔!我在问题中编辑了教师/应用程序控制器,因此所有内容都是一致的。我用整数代替了Scala Int,我在可选参数方面遇到了一个愚蠢的问题:)因为我正在筛选有课程和学校班级的教师,我的一个参数被命名为“班级”。。。这将导致另一个编译错误“简单表达式的非法启动”=>命名变量“类”。。。不要在家里这样做:DThanks,因为我希望参数是可选的,所以我尝试了第二种解决方案,但在我的模板中出现了一个编译错误:方法索引的参数太多:()play.api.mvc.CallI从错误的控制器调用了该操作(应该是
教师
而不是
应用程序
否?)@托马斯克检查我的编辑,这是工作,尼科埃基托我希望你不;t介意:)太好了,非常感谢尼科·埃基托和比西尔!我在问题中编辑了教师/应用程序控制器,因此所有内容都是一致的。我用整数代替了Scala Int,我在可选参数方面遇到了一个愚蠢的问题:)因为我正在筛选有课程和学校班级的教师,我的一个参数被命名为“班级”。。。这将导致另一个编译错误“简单表达式的非法启动”=>命名变量“类”。。。不要在家里这样做:D