[2.0] long parameter list

379 views
Skip to first unread message

Lars Grote

unread,
Apr 9, 2012, 8:02:28 AM4/9/12
to play-fr...@googlegroups.com
Hi there, 
I came across Play cause I was looking in stateless/restful web architectures, currently I'm using Java Stripes which has some similarities with Play. I've read 
quite a lot of the available documentation and think that I understand most of whats going on there (at least theoretically). 

When I think about how I would implement my current use case with Play I have an idea how to do that, but I find it somewhat "ugly" - well ugly might be a bit harsh, 
maybe say "not as beautiful as the rest of play". I think this is due to my lack of experience with Play, but I want get an clearer idear before I start.

Here is the usecase: 
I have search result page. The route would be something like: 
GET /myapp/search/:q  controllers.Searcher(q: String)

Now I have filters for this search result. They are in a form but I want to send as GET Parameters (restful ...). The filters could be something like:
price.min, price.max, color, language, size, ... I have ten or fifteen of those. What I understood form the docs at ScalaRouting and ScalaForms
there are two ways to acces those parameters. Either have a route like this: 
GET /myapp/search/:q  controllers.Searcher(q: String, price.min: Int ?= 0,price.max: Option[Int], color: Option[String], ...)
 
Which means I need to have a method in my Controller which has 10 to 15 parameters, which I find a bit awkward. But it'll probably work.
The other solutions I found is to use POST parameters and use the form binding. But then I loose the restfulness which is not an option for me. 

I know that it is still possible to access the parameters like this:
String id = params.get("id");
 
but that feels like going back to the old servlet days. I think another solution is to retrieve the request parameters as a map, and that apply the form binding to that map. 
At the moment this seems to be the best solution for me. But somehow I'm not completely satisfied with this. Maybe cause I'm used to the binding of Stripes.

It would be great if some experienced Play developer could give me a hint what the best practice for a use cases like this is.

Thanks very much, 
Lars

Kevin Bosman

unread,
Apr 9, 2012, 8:15:14 AM4/9/12
to play-fr...@googlegroups.com
bindFromRequest also binds from GET params, so you could use your simple route:

GET /myapp/search/:q controllers.Searcher(q: String)

and then access your parameters using a form, the same was as if they
were POST params with bindFromRequest()

Lars Grote

unread,
Apr 9, 2012, 8:25:23 AM4/9/12
to play-fr...@googlegroups.com
Ahh cool. Didn't know that. Thanks very much. 
That would work for me than. I still like the way Stripes handles binding of complex objects, but that's another topic. 

Cheers, Lars

Guillaume Bort

unread,
Apr 9, 2012, 4:18:55 PM4/9/12
to play-fr...@googlegroups.com
You can do the binding of complex objects as well with play. Check for querystringBindable. 
--
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/-/W9S_H72WSeQJ.
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.

Julien Tournay

unread,
Apr 9, 2012, 4:52:32 PM4/9/12
to play-fr...@googlegroups.com
Or using a Form + a java object

public class Query {
   public String page;
   public String query;
   // etc.
}

And in your controller

form(Query.class).bindFromRequest.

jto


Le lundi 9 avril 2012 13:18:55 UTC-7, Guillaume Bort a écrit :
You can do the binding of complex objects as well with play. Check for querystringBindable. 

On 9 avr. 2012, at 14:25, Lars Grote <lag...@gmail.com> wrote:

Ahh cool. Didn't know that. Thanks very much. 
That would work for me than. I still like the way Stripes handles binding of complex objects, but that's another topic. 

Cheers, Lars

On Monday, April 9, 2012 1:15:14 PM UTC+1, Kevin Bosman wrote:
bindFromRequest also binds from GET params, so you could use your simple route:

GET /myapp/search/:q  controllers.Searcher(q: String)

and then access your parameters using a form, the same was as if they
were POST params with bindFromRequest()

--
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/-/W9S_H72WSeQJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Lars Grote

unread,
Apr 9, 2012, 6:21:51 PM4/9/12
to play-fr...@googlegroups.com
This sound very interesting, and very promising. I can find the API doc, but do you know of any example?
I looked around but didn't find anything.

Thanks, Lars


On Monday, April 9, 2012 9:18:55 PM UTC+1, Guillaume Bort wrote:
You can do the binding of complex objects as well with play. Check for querystringBindable. 

On 9 avr. 2012, at 14:25, Lars Grote <lag...@gmail.com> wrote:

Ahh cool. Didn't know that. Thanks very much. 
That would work for me than. I still like the way Stripes handles binding of complex objects, but that's another topic. 

Cheers, Lars

On Monday, April 9, 2012 1:15:14 PM UTC+1, Kevin Bosman wrote:
bindFromRequest also binds from GET params, so you could use your simple route:

GET /myapp/search/:q  controllers.Searcher(q: String)

and then access your parameters using a form, the same was as if they
were POST params with bindFromRequest()

--
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/-/W9S_H72WSeQJ.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

Julien Richard-Foy

unread,
Apr 10, 2012, 3:48:40 AM4/10/12
to play-fr...@googlegroups.com
You can find an example on my blog:
http://julien.richard-foy.fr/blog/2012/04/09/how-to-implement-a-custom-pathbindable-with-play-2/
Here I’m talking about PathBindable, but QueryStringBindable works
similarly. I may update my post to mention QueryStringBindable…

On Tue, Apr 10, 2012 at 12:21 AM, Lars Grote <lag...@gmail.com> wrote:
> This sound very interesting, and very promising. I can find the API doc, but
> do you know of any example?
> I looked around but didn't find anything.
>
> Thanks, Lars
>
>
> On Monday, April 9, 2012 9:18:55 PM UTC+1, Guillaume Bort wrote:
>>
>> You can do the binding of complex objects as well with play. Check for
>> querystringBindable.
>>
>> On 9 avr. 2012, at 14:25, Lars Grote <lag...@gmail.com> wrote:
>>
>> Ahh cool. Didn't know that. Thanks very much.
>> That would work for me than. I still like the way Stripes handles binding
>> of complex objects, but that's another topic.
>>
>> Cheers, Lars
>>
>> On Monday, April 9, 2012 1:15:14 PM UTC+1, Kevin Bosman wrote:
>>>
>>> bindFromRequest also binds from GET params, so you could use your simple
>>> route:
>>>
>>> GET /myapp/search/:q  controllers.Searcher(q: String)
>>>
>>> and then access your parameters using a form, the same was as if they
>>> were POST params with bindFromRequest()
>>
>> --
>> 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/-/W9S_H72WSeQJ.

>> 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.
>

> --
> 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/-/fiDYcAl-LtoJ.
>
> To post to this group, send email to play-fr...@googlegroups.com.


> To unsubscribe from this group, send email to

> play-framewor...@googlegroups.com.

Kevin Bosman

unread,
Apr 10, 2012, 4:16:35 AM4/10/12
to play-fr...@googlegroups.com

That's quite straightforward. I don't suppose you have a Java example
to complement that though?
The Java interface's bind method simply returns T, so what would be
the correct way to return an equivalent of Left("Article not found")
if the article is not found?
Should it just throw a RuntimeException?

GrailsDeveloper

unread,
Apr 10, 2012, 4:18:48 AM4/10/12
to play-fr...@googlegroups.com
Is there exists something similar in java? Furthermore I think this should become part of the documentation.
Niels

>> 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.
>
> --
> 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/-/fiDYcAl-LtoJ.
>

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


> To unsubscribe from this group, send email to

Julien Richard-Foy

unread,
Apr 10, 2012, 4:36:35 AM4/10/12
to play-fr...@googlegroups.com
> That's quite straightforward. I don't suppose you have a Java example
> to complement that though?

Indeed, I don’t ;)

> The Java interface's bind method simply returns T, so what would be
> the correct way to return an equivalent of Left("Article not found")
> if the article is not found?
> Should it just throw a RuntimeException?

Yes, just throw an exception e and Play will produce a Left(e.getMessage()).

Kevin Bosman

unread,
Apr 10, 2012, 4:55:39 AM4/10/12
to play-fr...@googlegroups.com
> Yes, just throw an exception e and Play will produce a Left(e.getMessage()).

Great, thanks. I'll give it a bash.

Lars Grote

unread,
Apr 10, 2012, 7:35:28 AM4/10/12
to play-fr...@googlegroups.com
Hi Julien, 
this very cool, and helps me in a lot of cases. I think for most use cases this should be the preferred way cause you have a clean separation between,
retrieving the Object and do something with the Object. Thanks for pointing me to your blog (found loads of other interesting stuff there ;) )

But my curent use case is in some way the other way around. I have all the information that I need in the URL, so I don't need to enrich this info with new values.
What I need or want to do is group them into one Object. Lets say I have the URL Parameters price.min, and price.max in that case it would be great to bind
those two parameters into one Object named Price with two values min and max. In that object I could then have methods like isValid()or something like that.
Is there any way to do that with PathBindable ?

Thanks, 
Lars

>> 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.
>
> --
> 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/-/fiDYcAl-LtoJ.
>

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


> To unsubscribe from this group, send email to

Julien Richard-Foy

unread,
Apr 10, 2012, 9:16:13 AM4/10/12
to play-fr...@googlegroups.com
On Tue, Apr 10, 2012 at 1:35 PM, Lars Grote <lag...@gmail.com> wrote:
> Hi Julien,
> this very cool, and helps me in a lot of cases. I think for most use cases
> this should be the preferred way cause you have a clean separation between,
> retrieving the Object and do something with the Object. Thanks for pointing
> me to your blog (found loads of other interesting stuff there ;) )
>
> But my curent use case is in some way the other way around. I have all the
> information that I need in the URL, so I don't need to enrich this info with
> new values.
> What I need or want to do is group them into one Object. Lets say I have the
> URL Parameters price.min, and price.max in that case it would be great to
> bind
> those two parameters into one Object named Price with two values min and
> max. In that object I could then have methods like isValid()or something
> like that.
> Is there any way to do that with PathBindable ?

Not with PathBindable (because you can only bind your resulting object
only from one value), but with QueryStringBindable, yes (since you can
make use of all the query string parameters).

Something like this (untested):
https://gist.github.com/2344517#gistcomment-248124

Lars Grote

unread,
Apr 10, 2012, 9:22:27 AM4/10/12
to play-fr...@googlegroups.com
How cool is that?!
Thank you so much, that's exactly what I was looking for.

Is really a nice way to do that binding stuff. 

Thanks again,
Lars
Reply all
Reply to author
Forward
0 new messages