我是Play 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页时会出现错误。
有没有办法默认这些参数? 如果没有,那么最佳做法是什么?
谢谢!
您可以使用查询字符串参数而不是页码的路径参数。 查询字符串参数将允许您为缺少参数时提供默认值。
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