因此,對于演示,我將建立一個非常基本的Twitter副本; 它本來就很簡單,但是卻顯示了Play足夠的生產力! 提供。 我將逐步完成設置演示應用程序的步驟,該應用程序應涵蓋Heroku博客文章中宣布的內容,但要更深入一些。
第一步,創建應用程序
play new twitter
將依賴性添加到CRUD模塊(conf / dependencies.yml)
- play -> crud
獲取依賴項
play dependencies
IDE整合
(對于Eclipse)
play eclipsify
(對于IntelliJ)
play idealize
(對于Netbeans)
play netbeansify
創建模型(app / models / Tweet.java)
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;@Requiredpublic Date createDate = new Date();public static List findLatest() {return Tweet.find(“order by createDate desc”).fetch();}@Overridepublic String toString() {return this.tweet;}}
為JPA模型定義數據庫(conf / application.conf)
db=${DATABASE_URL}
添加控制器操作(app / controllers / Application.java)
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);}
}
定義主視圖(app / views / Application / index.html)
#{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 Actionvar 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 redirectreturn false;});</script>
定義創建操作視圖(app / views / Application / create.html)
<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 {@Testpublic 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);}}
為推特模型創建CRUD管理員
package controllers;public class Tweets extends CRUD {
}
添加路由(conf / routes)
* /admin module:crudGET /rest/tweets Application.tweets
為CRUD管理員定義消息(conf / messages)
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上創建應用程序
heroku create play-twitter –stack cedar
heroku創建推特-堆疊杉
設置Git存儲庫
git init; git add .; git commit -a -m “Initial Commit”; git remote add heroku git@heroku.com:play-twitter.git
設置Heroku環境變量
heroku config:add FRAMEWORK_ID=prod; heroku config:add USE_PRECOMPILED=true
部署到Heroku
git push heroku master
如果有任何問題,您可以隨時檢查日志
heroku logs
在Heroku上設置真實數據庫
heroku addons:add shared-database
您可以檢出現場演示在這里 ,管理界面這里或克隆的源代碼Github上 。
參考:我們的JCG合作伙伴 Felipe Oliveira(位于Geek)完全使用 ,其中包含Play框架,AJAX,CRUD和Heroku的簡單Twitter逐步指南 。
相關文章 :
- RabbitMQ播放模塊! 構架
- Heroku運行Java
- 在90分鐘內使用Grails構建Twitter:要點
- Spring MVC開發–快速教程
- Spring MVC3 Hibernate CRUD示例應用程序
- Java教程和Android教程列表
翻譯自: https://www.javacodegeeks.com/2011/09/simple-twitter-play-framework-ajax-crud.html