Carl
Carl

Reputation: 1816

Calling a controller method with a date using the Play Framework

This is possibly a stupid question (I'm a bit of a Play Framework noob) but I'm really having no luck with it, essentially I have a display method along the lines of

public static void viewDay( @As("dd-MM-yyyy") Date date) {

Which works fine when accessed via the web at /day?date=22-05-2011 or whatever.

My problem is that I want my save method to redirect back to this page when a new record has been added. In normal circumstances this kinda thing works fine, but since I want to pass a date here I'm running in to some trouble.

My initial idea was just to pass the date object along in a

viewDay(someDate);
kinda way(as i've been able to do with other DataTypes) This results in something along the lines of "ISO8601%3A2011-06-30T00%3A00%3A00%2B0100" ending up in the URL. I could live with this if it worked (even though its not ideal), but play doesn't seem to be able to decipher it ether and is just defaulting to using the current date instead.

I've also attempted to pass the date as a String formatted in the way @As expects, though this just resulted in some Type errors. Removing the @As("dd-MM-yyyy") also doesn't appear to help.

To my mind this seems like something that really shouldn't be very hard to do (though as i said I'm a complete noob to Play and Java Web Frameworks in general) so any pointers would be a great help.

Thanks, Carl

Rough overview of my controller:

public class TimeRecordController extends Controller {
    public static void index() {
        //do stuff
    }
    public static void viewDay( @As("dd-MM-yyyy") Date date) {
        //Do stuff (works fine when accessed via web) 
        //If page is arraived at via the save though url shows UTC date: "ISO8601%3A2011-06-30T00%3A00%3A00%2B0100" 
        //And play is unable to read the correct date
    }
    public static void save(Stuff here, Date rtn){
        //do validation/saving
        viewDay(rtn);
    }
}

Upvotes: 3

Views: 934

Answers (2)

basav
basav

Reputation: 1453

Is it java util Date or joda date.

The date which is returned is in ISO8601 format. Play uses joda-time which defaults to ISO 8601 calendar system.

Upvotes: 0

André Pareis
André Pareis

Reputation: 1094

Just call your template with render(date) or call another controller method that expects a Date with your date object. Don't convert the Date to String in your Java code, play handles that automatically during parameter binding. If you need to render your Date object in a certain format in your view templates, you can either make a global setting in the application.conf (scan for "date.format") or render your Date in the template using ${date.format('yyyy-MM-dd')}.

If this does not answer your question, please post your controller code, so that we get a better picture of what you want to achieve.

Upvotes: 1

Related Questions