On August 31st, 2011: I will have the great honor to present our beloved Play Framework at Salesforce’s Dreamforce conference here in San Francisco, CA. If you are attending, please feel free to check it out or just stop by to say hello. Thank you Zenexity for the opportunity! I am always excited to help spread the word on this awesome framework!
So the big announcement is out—Heroku started offering native support to Play Framework applications! If you haven’t heard it, checkout the post from Jesper Joergensen on Heroku’s blog.
So for the presentation, I am setting up a very basic Twitter clone; it’s meant to be simple yet it displays just enough of the productivity that Play! provides. I am gonna go through the steps to setup the demo application which should cover what was announced on Heroku’s blog post but with a little more depth.
play new twitter
- play -> crud
play dependencies
play eclipsify (for Eclipse), play idealize (for IntelliJ) or play netbeansify (for Netbeans).
package models;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import play.data.validation.MaxSize;
import play.data.validation.Required;
import play.db.jpa.Model;
@Entity
public class Tweet extends Model {
@Required
@MaxSize(140)
public String tweet;
@Required
public Date createDate = new Date();
public static List findLatest() {
return Tweet.find(“order by createDate desc”).fetch();
}
@Override
public String toString() {
return this.tweet;
}
}
db=${DATABASE_URL}
package controllers;
import java.util.List;
import models.Tweet;
import play.mvc.Controller;
public class Application extends Controller {
public static void index() {
List tweets = Tweet.findLatest();
render(tweets);
}
public static void create(String msg) {
Tweet tweet = new Tweet();
tweet.tweet = msg;
tweet.save();
render(tweet);
}
public static void tweets() {
List tweets = Tweet.findLatest();
renderJSON(tweets);
}
}
#{extends ‘main.html’ /}
#{set title:’Home’ /}
<!– Create Tweet Form –>
<form> <input name=”tweet” type=”text” />
<input type=”submit” value=”Tweet” /> </form><!– Latest Tweets List –>
<ul> #{list tweets}
<li>${_.tweet} (${_.createDate.since()})</li><p><p>
#{/list}</ul>
<!– JS –>
<script type=”text/javascript”>
// Capture Form Submit Event
$(‘form’).submit(function() {
// Define Create Action
var createAction = #{jsAction @create(‘:tweet’) /}
// Call Create Action
$.post(createAction({tweet: $(‘input:first’).val()}), function(data) {
// Prepend Results to the List
$(‘ul’).prepend(data);
$(‘input:first’).val(”);
});
// Don’t let the browser redirect
return false;
});
</script>
<li><code>${tweet.tweet} (${tweet.createDate.since()})</li>
import models.Tweet;
import org.junit.Assert;
import org.junit.Test;
import play.test.UnitTest;
public class TweetTest extends UnitTest {
@Test
public void testModelSave() {
long count = Tweet.count();
Tweet t = new Tweet();
t.tweet = “my sample tweet”;
t.save();
long count2 = Tweet.count();
Assert.assertEquals(count + 1, count2);
}
}
package controllers;
public class Tweets extends CRUD {
}
* /admin module:crud
GET /rest/tweets Application.tweets
tweet=Tweet
createDate=Date Created
web: play run –%$FRAMEWORK_ID –http.port=$PORT -DusePrecompiled=$USE_PRECOMPILED -DDATABASE_URL=mem
play run –%dev -DusePrecompiled=false -DDATABASE_URL=mem
heroku create play-twitter –stack cedar
git init; git add .; git commit -a -m “Initial Commit”; git remote add heroku git@heroku.com:play-twitter.git
heroku config:add FRAMEWORK_ID=prod; heroku config:add USE_PRECOMPILED=true
git push heroku master
heroku logs
heroku addons:add shared-database
You can checkout a live demo here, the admin interface here or clone the source code on Github.
Voila! Now Go Play!


{ 3 comments… read them below or add one }
Many thanks Felipe !
You’ve forgot to insert since.seconds, since.minutes, since.hours, since.days, since.months and since.years into conf/messages
No problems at all, I think you’re busy…
Keep up the good work !!!
very good tutorial!
we are preparing something similar for the next java user group meeting
here’s the repo https://github.com/opensas/events with a different branch for each step
and here it is running on gae (with siena) http://jugar-events.appspot.com/ and on heroku http://jugardemo.herokuapp.com/
saludos
sas
Hi,
great post thanks!
I’m trying to deploy a Play app on Heroku, I have a little problem with the shared database.
When I deploy the app with “git push” I have this message :
WARNING: Cannot replace DATABASE_URL in configuration (db=${DATABASE_URL})
Did I miss something?
Thanks
Loic
You must log in to post a comment.
{ 38 trackbacks }