This is the job of the router to check query string parameters. Just define your parameter in the routes
file:
GET /test controllers.Application.test(top: Int ?= 42)
And add top
as a parameter to your controller method:
def test(top: Int) = Action { // Use top here val data = CData(top) }
Then Play will do the whole check. Notice how the default value is specified using ?=
Syntax.
You should use forms only for POST requests.
Update:
If you want to manually check the parameters, then you can define a helper method:
def getQueryParam(key: String, default: String)(implicit request: RequestHeader) = request.queryString.get(key).flatMap(_.headOption).getOrElse(default)
And use it in your controller methods:
def test = Action { implicit request => val top = getQueryParam("top", "42") ...
But in doing so, you lose type checking. Of course, you can define helpers for each type, i.e. getIntParam
, getStringParam
, etc., but Play already contains a secure router implementation designed to solve such problems. I advise you to use a routing mechanism instead of a manual check.
source share