[2.0] Is it possible get get request parameters in Java actions?

3,770 views
Skip to first unread message

Steve Chaloner

unread,
Mar 16, 2012, 9:55:55 AM3/16/12
to play-fr...@googlegroups.com
Hi,

This is for a Java app.

I've annotated a controller method as @With(MyInterceptor.class), but the interceptor isn't able to access any request parameters.  Looking in the context, they don't seem to be in there.  Is this no longer possible?

Thanks,
Steve

Julien Richard-Foy

unread,
Mar 16, 2012, 10:10:30 AM3/16/12
to play-fr...@googlegroups.com
Http.Context.args is a map available for you to store values across
the request processing.
Request parameters can be retrieved from the Http.Context.request()
value (see http://www.playframework.org/documentation/api/2.0/java/play/mvc/Http.Request.html)

Steve Chaloner

unread,
Mar 16, 2012, 10:18:52 AM3/16/12
to play-fr...@googlegroups.com
Http.Context.args doesn't contain the request parameters as I understand it - it's for storing server-side info during the request lifetime.

Damn, just realised what the problem is.  The query parameters are not classic ?x=y, but part of the URL parsed by the router.  That's why ctx.request().queryString() returns an empty map, I guess.

Thanks,
Steve
Message has been deleted

Steve Chaloner

unread,
Mar 16, 2012, 10:46:14 AM3/16/12
to play-fr...@googlegroups.com
Debugging in the controller method, the request parameters are also not available through the request.  Think I'm missing something here!


On Friday, 16 March 2012 15:10:30 UTC+1, Julien Richard-Foy wrote:

Olivier Refalo

unread,
Mar 16, 2012, 10:51:25 AM3/16/12
to play-fr...@googlegroups.com
trying to clarify your last message:


"The query parameters are not classic ?x=y, but part of the URL parsed by the router"

controler/x/y

if this is the case, you shall read the uri() and parse it

another option is to parametrize the route:

GET   /clients/:id          controllers.Clients.show(id: Long) 

Steve Chaloner

unread,
Mar 16, 2012, 6:03:29 PM3/16/12
to play-fr...@googlegroups.com
The route is already parameterised - in fact, I'm updating Zentasks to use Deadbolt 2 so everything is exactly as it is in that sample project.

To clarify, in Play 1 you could just have 'request.params.get("x")' to get a request parameter but that no longer seems possible with the examples and samples I've checked, which feels like a regression (unless, of course, I'm missing something).

- Steve

Guillaume Bort

unread,
Mar 16, 2012, 6:10:52 PM3/16/12
to play-fr...@googlegroups.com
It shouldn't be part of request.getQueryString for sure, since it
doesn't comes from the QueryString. Now perhaps it should be part of
another scope (something like routes parameters_.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/h2r7JPnY-v4J.
>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

--
Guillaume Bort

Steve Chaloner

unread,
Mar 17, 2012, 5:49:41 PM3/17/12
to play-fr...@googlegroups.com
It would be immensely useful - certainly, in Play 1 apps I used this, and now there's a usecase in Deadbolt for it too.  Debugging further down the stack, I can see the parameter values have already been parsed, so if they can be made available through the routes/contexts/whatever, that would be great.

FYI I mentioned get getQueryString because it had the only signature that looked vaguely like it was correct.

Thanks,
Steve

> To post to this group, send email to play-framework@googlegroups.com.


> To unsubscribe from this group, send email to


> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

--
Guillaume Bort

Olivier Refalo

unread,
Mar 20, 2012, 11:15:19 AM3/20/12
to play-fr...@googlegroups.com
+100

Hum, interesting enough. I now have the exact same issue...

from a validator I need to read request parameters.
I use play.mvc.Http.Context.current() to get the the current context... but then there is no method to access the request parameters

Something like a play.mvc.Http.Context.current().request().params().get("myparam") is a must have. 

Olivier Refalo

unread,
Mar 20, 2012, 11:50:09 AM3/20/12
to play-fr...@googlegroups.com
The only way I figured was to copy/alter the code from Form.

protected Map<String,String> requestData()

It parses the body again... not perfect but it works.

Reply all
Reply to author
Forward
0 new messages