[2.0.1] how to bind Date parameters

2,000 views
Skip to first unread message

eee

unread,
May 20, 2012, 10:24:02 PM5/20/12
to play-fr...@googlegroups.com
Hi,
I have a newbie question, using play with Java.

I have in my controller a method:    

@BodyParser.Of(Json.class)
    public static Result getEvents (Date startDate, Date endDate) {
        List<Event> events = Event.getEvents(startDate, endDate);
        return ok(play.libs.Json.toJson(events));
    }

which is the proper way to specify a route?

GET        /json/events/:startDate/:endDate                controllers.EventController.getEvents(startDate: Date, endDate: Date)

fails because type Date is not recognized





Eishay Smith

unread,
May 21, 2012, 12:03:12 AM5/21/12
to play-fr...@googlegroups.com
check out PathBindable and QueryStringBindable as in http://www.playframework.org/documentation/api/2.0.1/java/play/mvc/PathBindable.html







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

Mathias Clerc

unread,
May 21, 2012, 3:59:24 AM5/21/12
to play-fr...@googlegroups.com
I would pass them as String and call myself parser and formatter to be
sure to have the same format on every platform. Or even to be able to
handle several date formats at the same time.

2012/5/21 Eishay Smith <eis...@gmail.com>:

Leonard Punt

unread,
May 21, 2012, 4:15:09 AM5/21/12
to play-fr...@googlegroups.com
@tlarhices

That is exactly the functionality a PathBinder provides. So using the PathBinder and/or the QueryStringBinder is the solution.

Mathias Clerc

unread,
May 21, 2012, 4:21:22 AM5/21/12
to play-fr...@googlegroups.com
I did a quick search and those Binder looks pretty useful.
I could only find some samples in scala, is there any doc on binders with java ?

2012/5/21 Leonard Punt <leona...@gmail.com>:
> @tlarhices
>
> That is exactly the functionality a PathBinder provides. So using the
> PathBinder and/or the QueryStringBinder is the solution.
>
> --
> 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/-/ARsZTY2or30J.

Leonard Punt

unread,
May 21, 2012, 4:29:54 AM5/21/12
to play-fr...@googlegroups.com
I'm not sure if that's possible. I asked it a while ago: https://groups.google.com/d/topic/play-framework/H9usUW5JTX8/discussion. Unfortunately it haven't been answered.
Anyway, it is no problem to mix Java and Scala code. And I can tell you that, if you take a good look at the examples, it isn't that hard to write a PathBinder in Scala for a Java guy.

Julien Richard-Foy

unread,
May 21, 2012, 4:56:41 AM5/21/12
to play-fr...@googlegroups.com

Enrique Vetere

unread,
May 21, 2012, 11:33:08 AM5/21/12
to play-fr...@googlegroups.com
Great, thanks.
Now, what´s the difference between PathBindable and
QueryStringBindable? I can see QueryStringBindable returns
Some/None, what´s the effect on the framework?


On 21 May 2012 05:56, Julien Richard-Foy <j...@zenexity.com> wrote:
> You can find an example of Java QueryStringBindable here:
> http://stackoverflow.com/questions/10286725/how-to-bind-double-parameter-with-play-2-0-routing/10307927#10307927
>
> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.

Julien Richard-Foy

unread,
May 21, 2012, 12:09:42 PM5/21/12
to play-fr...@googlegroups.com
QueryStringBindable binds values from the request query string.
PathBindable binds values from the request path.

In the routes file you can tell the framework how to dispatch the
incoming requests based on their path. So, when the parameters binders
will be invoked they *know* there will be a value for each path
parameter of the route (otherwise the route wouldn’t have matched). On
the other hand, the query string may not contain some parameters.

So here are the contracts of PathBindable#bind and QueryStringBindable#bind:

T PathBindable#bind(String key, String rawValue)
-> returns a T built from the rawValue string (or throws an exception
if such a value was not possible to build).

Option<T> QueryStringBindable(String key, Map<String, String[]> queryString)
-> returns None if there was no parameter `key` in the `queryString`.
Otherwise returns a value of type Some<T>, containing the value built
from the query string data (or throws an exception if the value was
not possible to build).

Mathias Clerc

unread,
May 21, 2012, 8:30:25 PM5/21/12
to play-fr...@googlegroups.com
Thank you, looks pretty powerful

2012/5/21 Julien Richard-Foy <j...@zenexity.com>:
> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
Reply all
Reply to author
Forward
0 new messages