Best practices for paginated play routes?
I am new to Play 2 (Scala). I need to use pagination to output the members of a list. It's easy enough, except for a portion of the page.
My route file has my search:
GET /find/thing/:type controllers.Application.showType(type: String)
This works great if I want to dump the entire list to a page.
Now what if I want to paginate it? I guess I could do -
GET /find/thing/:type/:page controllers.Application.showType(type: String, page: Int)
But what happens if the user just types "myurl.com/find/thing/bestThing" without a page? Obviously there will be an error when it is automatically "default" on page 1.
Is there a default way to use these arguments? If not, what is the best for what?
Thank!
source to share
Two options:
- declare both routes you specified (using a fixed value parameter first ), you can use untrail trick globally, in which case it will redirect your
/find/thing/something/
to/find/thing/something
(page = 1) -
You can use parameters with default values , then your route will look like this:
GET /find/thing/:type controllers.Application.showType(type: String, page: Int ?= 1)
and the generic url will look like this:
/find/thing/something?page=123
source to share
Instead of a path parameter for the page number, you can use a query string parameter. Query string parameters will allow you to specify default values if the parameter is missing.
GET /find/thing/:type controllers.Application.showType(type: String, page: Int ?= 1)
You would use them like this:
/find/thing/bestThing?page=3 // shows page 3
/find/thing/bestThing // shows page 1
source to share