Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 使用分页的播放路线的最佳实践?_Scala_Pagination_Routes_Playframework 2.0 - Fatal编程技术网

Scala 使用分页的播放路线的最佳实践?

Scala 使用分页的播放路线的最佳实践?,scala,pagination,routes,playframework-2.0,Scala,Pagination,Routes,Playframework 2.0,我对玩2(Scala)相当陌生。我需要使用分页来输出列表的成员。这很容易,除了分页部分 在我的路线文件中,我有我的搜索: GET /find/thing/:type controllers.Application.showType(type: String) 如果我想将整个列表转储到页面中,这很好 现在,如果我想给它分页呢?我想我可以做- GET /find/thing/:type/:page controllers.Applicatio

我对玩2(Scala)相当陌生。我需要使用分页来输出列表的成员。这很容易,除了分页部分

在我的路线文件中,我有我的搜索:

GET        /find/thing/:type        controllers.Application.showType(type: String)
如果我想将整个列表转储到页面中,这很好

现在,如果我想给它分页呢?我想我可以做-

GET        /find/thing/:type/:page        controllers.Application.showType(type: String, page: Int)
但是,如果用户只键入“myurl.com/find/thing/bestThing”而没有页面,会发生什么呢?显然,当它自动“默认”到第1页时,会出现错误

有没有办法默认这些参数?如果没有,这方面的最佳做法是什么

谢谢大家!

有两种选择:

  • 声明您提到的两个路由(首先使用),然后您可以全局使用,在这种情况下,它会将您的
    /find/thing/something/
    重定向到
    /find/thing/something
    (第1页)
  • 您可以使用,然后您的路线将如下所示:

    GET /find/thing/:type  controllers.Application.showType(type: String, page: Int ?= 1)
    
    /find/thing/something?page=123
    
  • 一般化URL如下所示:

    GET /find/thing/:type  controllers.Application.showType(type: String, page: Int ?= 1)
    
    /find/thing/something?page=123
    

    对于页码,可以使用查询字符串参数而不是路径参数。查询字符串参数将允许您在缺少参数时提供默认值

    GET   /find/thing/:type      controllers.Application.showType(type: String, page: Int ?= 1)
    
    您可以这样使用它们:

    /find/thing/bestThing?page=3    // shows page 3
    
    /find/thing/bestThing           // shows page 1
    

    谢谢,之所以选择您的答案,是因为您的答案有不止一个好的解决方案。