分页游戏路线的最佳做法?

问题描述 投票:3回答:2

我是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页时会出现错误。

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

谢谢!

scala pagination routes playframework-2.0
2个回答
3
投票

两种选择:

  1. 声明你提到的两个路由(首先使用带有固定值的参数 ),然后你可以全局使用untrail技巧 ,在这种情况下它会将你的/find/thing/something/重定向到/find/thing/something (page = 1)
  2. 您可以使用默认值的参数 ,然后您的路线将是:

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

和生成的URL将像:

/find/thing/something?page=123

2
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.