简单的Twitter:Heroku上的Play框架,AJAX,CRUD
因此,對于演示,我將建立一個非?;镜腡witter副本; 它本來就很簡單,但是卻顯示了Play足夠的生產(chǎn)力! 提供。 我將逐步完成設(shè)置演示應(yīng)用程序的步驟,該應(yīng)用程序應(yīng)涵蓋Heroku博客文章中宣布的內(nèi)容,但要更深入一些。
第一步,創(chuàng)建應(yīng)用程序
play new twitter將依賴性添加到CRUD模塊(conf / dependencies.yml)
- play -> crud獲取依賴項
play dependenciesIDE整合
(對于Eclipse)
play eclipsify(對于IntelliJ)
play idealize(對于Netbeans)
play netbeansify創(chuàng)建模型(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模型定義數(shù)據(jù)庫(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>定義創(chuàng)建操作視圖(app / views / Application / create.html)
<li><code>${tweet.tweet} (${tweet.createDate.since()})</li>創(chuàng)建推文模型的單元測試
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);}}為推特模型創(chuàng)建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在開發(fā)中運行
play run –%dev -DusePrecompiled=false -DDATABASE_URL=mem在Heroku上創(chuàng)建應(yīng)用程序
heroku create play-twitter –stack cedarheroku創(chuàng)建推特-堆疊杉
設(shè)置Git存儲庫
git init; git add .; git commit -a -m “Initial Commit”; git remote add heroku git@heroku.com:play-twitter.git設(shè)置Heroku環(huán)境變量
heroku config:add FRAMEWORK_ID=prod; heroku config:add USE_PRECOMPILED=true部署到Heroku
git push heroku master如果有任何問題,您可以隨時檢查日志
heroku logs在Heroku上設(shè)置真實數(shù)據(jù)庫
heroku addons:add shared-database您可以檢出現(xiàn)場演示在這里 ,管理界面這里或克隆的源代碼Github上 。
參考:我們的JCG合作伙伴 Felipe Oliveira(位于Geek)完全使用 ,其中包含Play框架,AJAX,CRUD和Heroku的簡單Twitter逐步指南 。
相關(guān)文章 :
- RabbitMQ播放模塊! 構(gòu)架
- Heroku運行Java
- 在90分鐘內(nèi)使用Grails構(gòu)建Twitter:要點
- Spring MVC開發(fā)–快速教程
- Spring MVC3 Hibernate CRUD示例應(yīng)用程序
- Java教程和Android教程列表
翻譯自: https://www.javacodegeeks.com/2011/09/simple-twitter-play-framework-ajax-crud.html
總結(jié)
以上是生活随笔為你收集整理的简单的Twitter:Heroku上的Play框架,AJAX,CRUD的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos攻击犯法吗(被ddos攻击犯法么
- 下一篇: Google App Engine Ja