Anthony McCormick
Anthony McCormick

Reputation: 2744

PlayFramework 2.0.2 multiple parameters in routes

I cant seem to get multiple parameters to work if i add one parameter everything is fine as soon as i add a second parameter i always get a

No data received
Unable to load the webpage because the server sent no data.
Here are some suggestions:
Reload this webpage later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.

Can anyone else confirm that you can add a second param to a request using play 2.0.2? (using java)

My url is as simple as this

http://localhost:9000/account/foruser?username=somethig&create=0

and the routes

GET     /account/foruser
controllers.user.UserController.foruser(username:String, create:Boolean ) 

Upvotes: 5

Views: 16638

Answers (3)

Moebius
Moebius

Reputation: 6518

A quick example :

The correct url corresponding to this route :

GET     /user/update:action/:id controllers.myFunction(action: String, id: String)

Is the url :

myWebsite.com/user/update$action=update/[email protected]

Note that :

  • each one of the url params begin by a $
  • No & or ? in the url !
  • The / between :id and :action must be there. Play redirect to the correct webpage without it, but it is nescessary if you want to have the correct value (ie action => '$action=update' and id => '[email protected]') transmitted to your function
  • When you have an error, java display an error with the regex to apply to each route.

Upvotes: 1

gavioto
gavioto

Reputation: 1745

@biesior I am getting the same problem using 2.0.4 with two types of parameters

File routes:

GET /v2/object/all/data/:from/:until/:all           controllers.ObjectController.getAllData(from: String, until: String , all: Boolean, username: String ?= null, password: String ?=null)

File controller:

public static Result getAllData(
            @PathParam("from") String from,
            @PathParam("until") String until,
            @PathParam("all") Boolean all,
            @QueryParam("username") String username, @QueryParam("password") String password)

After doing several tests I finally solved the problem. You should use "boolean" instead of Boolean, so "getAllData" transforms to:

public static Result getAllData(
            @PathParam("from") String from,
            @PathParam("until") String until,
            @PathParam("all") boolean all,
            @QueryParam("username") String username, @QueryParam("password") String password)

Upvotes: 1

biesior
biesior

Reputation: 55798

You should put some more attention on the routes docs and samples

In general if you're using named params with &name=value you don't need to specify them in the routes files. Instead use DynamicForm in the Java to access them.

Route file is used for matching unnamed parts of the link with controler's action and params. So your link should look like:

http://localhost:9000/account/foruser/something/0

and route (of course this need to be placed in one line in routes file:

GET     /account/foruser/:username/:create
   controllers.user.UserController.foruser(username: String, create: Integer ) 

Note that was some bug reports on using the Boolean type in the route, so it's just safer to use some numeric type instead.

Upvotes: 15

Related Questions