SlideShare a Scribd company logo
1 of 7
Download to read offline
BrouGHt to you By: 202 
Getting Started with Play Framework 
© DZone, Inc. | DZone.com 
By Ryan Knight 
» create a new Play App 
» Routing Requests 
» controllers 
» Server-side Templates 
» JSon 
» Static Assets 
» Asset compiler 
CONTENTS 
Java EntErprisE Edition 7 
introduction to play FramEwork 
The Play Framework is a high velocity web framework for 

2* (0$0!*(!/$%#$(5,.+ 10%2!3+.'ý+3 
10 +!/*Ě0/.%ü!/(%(%05ċ(5!01.!/ė1/0ġ 
$%0ġ.!.!/$Ę3+.'ý+30$0!*(!/.,%  !2!(+,)!*0 
5(!ċ$!+),%(0%+** ,,(%0%+*.!(+ %*#$,,!*/ 
!$%* 0$!/!*!/ċ(5%/1%(0+.0$!)+ !.*3!5 
!%*#*+*ġ(+'%*#Č1(5 !1(0Č* $2%*#1%(0ġ 
%*//!0+),%(!./+.)+ !.*(%!*0ġ/% !0!$*+(+#%!/(%'! 
ƫ+û!!.%,0*
ċ 
crEatE a nEw play app 
The easiest way to get started with Play is with Typesafe 
Ƶ0%20+.ġ*+,!*/+1.!0++(+./0.0%*#*!3,.+!0/ 
1/%*#0$!5,!/!(0+.)ċ+#!0/0.0! č 
āċ +3*(+ 5,!/!Ƶ0%20+.čĪ05,!/!ċ+)ĥ 
,(0+.)ĥ#!0/0.0! īĨ$00,čĥĥ05,!/!ċ+)ĥ,(0+.)ĥ 
getstarted) 
Ăċ+((+30$!%*/0.10%+*/+*0$! +3*(+ ,#!0+ 
(1*$Ƶ0%20+.ċ 
ăċƫ.!0!*!3,,(%0%+*1/%*#0$!ė!((+(5 
.)!3+.'Ę0!),(0!,,(%0%+*ċ 
	5+1.!1/%*#0$!	Č0$!*5+1.*!3(5,,(%0%+*/$+1(  
.1*10+)0%((5ċ+3!2!.Č%5+1.!1/%*#0$!+))*  
(%*!Č0$!*5+1*.1*5+1.*!3(5,,(%0%+*3%0$ċĥ 
0%20+.Ʒ.1*%*5+1.*!3,.+!0 %.!0+.5ċ 
+30$05+1$2!*!3(5,,(%0%+*1,* .1**%*#Č 
5+1/$+1( !(!0+.+3/!0+Ī(+($+/0čĊĀĀĀīĨ$00,čĥĥ 
(+($+/0čĊĀĀĀĩ* /!!5+1.*!3,,(%0%+*ċ 
+#!00+0$!(+( +1)!*00%+*+.(5Č*2%#0!0+ 
Ī(+($+/0čĊĀĀĀĥĮ +1)!*00%+*īĨ$00,čĥĥ(+($+/0čĊĀĀĀĥĮ 
documentation) 
You can either use the basic code editor in Activator or 
+,!*5+1.,.+!0%*	*0!((%
+.(%,/!ċ	*0$!Ƶ0%20+.	Č 
navigate to codeČ* 0$!*/!(!0open in to generate the 
,.+!0ü(!/+.5+1.	ċ 
+1.*!3,.+!0$/0$!+((+3%*#(5+10č 
app 
The app source directory 
+.
2Č(Č* (%!*0ġ 
side sources 
conf 
$!+*ü# %.!0+.5 
containing the route 
mapping and application 
+*ü# 
Get More Refcardz! Visit Refcardz.com 
GEttinG startEd witH play FramEwork 
public 
The static assets directory 
Ĩ!ċ#ċ%)#!/Č
ČƫČ 
JavaScript) 
test The test source directory 
You can have both Java and Scala sources for your backend 
+ !ċ+3!2!.(5$/*% %+)0%
2Ƶ	*  
/!,.0!% %+)0%(Ƶ	!1/!0$!% %+)/ %û!. 
!03!!*0$!03+(*#1#!/ċ$%/.!0!/*!4,!.%!*!0$0 
ė!!(/.%#$0Ę3%0$3$0!2!.(*#1#!5+11/!ċ$!,'#!/ 
in Play for Scala begin with play.api while the Java API lives 
under the play,'#!,.!ü4ċ 
routinG rEquEsts 
The conf/routesü(! !ü*!/$+3(5.+10!/.!-1!/0//!  
+*0$!%.2!.* ,0$ċ$!*5+1) !.!-1!/00+ 
http://localhost:9000 your browser made a GEt.!-1!/0 
3%0$ĥ,0$ċ+1*/!!%*0$! !1(0routesü(!0$!.!%/ 
.+10%*# !ü*%0%+*0+$* (!0$0/!č 
GET / controllers.Application.index() 
The third parameter is the method that will be responsible 
+.$* (%*#0$!.!-1!/0* .!01.*%*#.!/,+*/!ċ$! 
/0.101.!+0$!.+10!/ü(!%/č 
VERB PATH CONTROLLER_METHOD 
*(52(% 2!./.!((+3! ċ	5+1$*#!GEt to 
Foo you will get a compile error indicating that Foo is not a 
2(% 2!.ċ 
The path part of the routesü(!*!,.)!0!.%6!  
0+!40.0%*+.)0%+** ,//%00+0$!+*0.+((!.ċ+. 
%*/0*!Č0+,1((*% +10+,0$5+13+1(  +č
3 GETTING STARTED WITH PLAY FRAMEWORK 
GET /user/:id controllers.User.get(id) 
$!č)0$!/+*+*!ĥ/!,.0! /!#)!*0((+3%*#5+10+ 
!40.0)1(0%,(!2(1!/(%'!č 
GET /user/:id/:name controllers.User.get(id, 
name) 
+1*(/+!40.00$!.!/0+0$!,0$1/%*#ĵ(%'!č 

ȇ0,*1%+$ȇȋƭ) ,+1/,))/0ǣ,*1%+$ǣ 
$1ȁƭ)Ȃ 
0$/*(/+1/!.!#!4!4,.!//%+*/0+(%)%03$00$!5)0$ 
+*ċ 
1!.5/0.%*#,.)!0!./*!10+)0%((5!40.0! %*0+ 
+*0.+((!.)!0$+ ,.)!0!./ċ+$* (!GEt.!-1!/00+/ 
foo?bar=neat !ü*!.+10!(%'!č 

ȇ#,, ,+1/,))/0ǣ	,,ǣ$1ȁ/ǥʛ0001/+$Ȃ 
$!-1!.5/0.%*#,.)!0!./.!05,!ġ/!Č/+%5+1/!00$! 
type as intČ0$!*0$!.!3%((!*!..+.%0$!,.)!0!.**+0 
be converted to an intċ 
+1*(/+$2! !1(0* +,0%+*(,.)!0!./ċ 
*!+0$!.!/+*/0$0(5+),%(!/0$!.+10!/ü(!%/0+ 
provide a reverse routing API so that you never have to hard 
+ !
/%*0+5+1.,,(%0%+*ċ	*/0! Č5+1(()!0$+  
%*0$!.!2!./!.+10!.Č3$%$.!01.*/0$!.+10! !ü*! 50$! 
ė.+10!/Ęü(!ċ$%/!*(!/5+10+!/%(5.!0+.5+1.
/ 
3%0$+10.!'%*#5+1.,,ċ 
© DZone, Inc. | DZone.com 
controllErs 
ƫ+*0.+((!./%*(5.!.!/,+*/%(!+.$* (%*#.!-1!/0*  
.!01.*%*#.!/,+*/!ċ!.!%//%
2+*0.+((!.Ĩ3$%$ 
would live in app/controllers/Foo.javaĩč 
package controllers 
import play.api._ 
import play.api.mvc._ 
public class Foo extends Controller { 
public static Result get() { 
return ok(“hello”); 
} 
} 
ǫ5!40!* %*#0$!/!controller(//Č3!,1((%*/+)! 
+*2!*%!*!)!0$+ /Č10 +%*#/+%/*+0.!-1%.! ċ$!get() 
method is static because Play is stateless and static methods 
.!*!/5350+ !ü*!)!0$+ 0$0 +!/*+0$*#!*5 
/00!ċ+1*(/+1/! !,!* !*5%*!0%+*.)!3+.'(%'! 
Spring or Guice with Play if you want to avoid using static 
)!0$+ /ċ$!get() method returns a resultČ3$%$.!,.!/!*0/ 
0$!.!/,+*/!ċ	*0$%//!Č0$!.!/,+*/!%//001/+ ! 
ĂĀĀ.!/,+*/!!1/!0$!ok helper was used to set that status 
+ !ċ$!.!.!)*5+0$!.$!(,!./(%'!notFound and 
badrequest that wrap the general purpose statusƵ	ċ$! 
.!/,+*/!+ 5%*0$%/!4),(!%/1/0string but it could also 
!

More Related Content

What's hot

Ad Strategy - the YMCA
Ad Strategy - the YMCAAd Strategy - the YMCA
Ad Strategy - the YMCAAnna Sandgren
 
Nghien cuu cong_nghe_sx_dich_dam,_bot_ca
Nghien cuu cong_nghe_sx_dich_dam,_bot_caNghien cuu cong_nghe_sx_dich_dam,_bot_ca
Nghien cuu cong_nghe_sx_dich_dam,_bot_casaomai491
 
Tecnicas de sql injection
Tecnicas de sql injectionTecnicas de sql injection
Tecnicas de sql injectionAlan Resendiz
 
Dramatically increase revenue on verio template
Dramatically increase revenue on verio templateDramatically increase revenue on verio template
Dramatically increase revenue on verio templateJanine Soika
 
The Security Risks of Web 2.0 - DEF CON 17
The Security Risks of Web 2.0 - DEF CON 17The Security Risks of Web 2.0 - DEF CON 17
The Security Risks of Web 2.0 - DEF CON 17Security Ninja
 
Replica Sets
Replica SetsReplica Sets
Replica SetsMongoDB
 
Darren stanley
Darren stanleyDarren stanley
Darren stanleybicacafe
 
Buku panduan pengelolaan-e-journal
Buku panduan pengelolaan-e-journalBuku panduan pengelolaan-e-journal
Buku panduan pengelolaan-e-journalCoky Fauzi Alfi
 
Julius Joseph-SAP BASIS Resume
Julius Joseph-SAP BASIS ResumeJulius Joseph-SAP BASIS Resume
Julius Joseph-SAP BASIS ResumeJulius Joseph
 
دراسة تحليلة لمشروع التصميم الحضري لمنطقة سد كمران
دراسة تحليلة لمشروع التصميم الحضري لمنطقة سد كمراندراسة تحليلة لمشروع التصميم الحضري لمنطقة سد كمران
دراسة تحليلة لمشروع التصميم الحضري لمنطقة سد كمرانTaha Farwan
 
NantKwest Chairman & CEO Dr. Patrick Soon-Shiong to Present Vision for Next G...
NantKwest Chairman & CEO Dr. Patrick Soon-Shiong to Present Vision for Next G...NantKwest Chairman & CEO Dr. Patrick Soon-Shiong to Present Vision for Next G...
NantKwest Chairman & CEO Dr. Patrick Soon-Shiong to Present Vision for Next G...JillHollister
 
6.Conocimiento cliente Cuenta Pagos en Linea. (Interlat Group
6.Conocimiento cliente Cuenta Pagos en Linea. (Interlat Group6.Conocimiento cliente Cuenta Pagos en Linea. (Interlat Group
6.Conocimiento cliente Cuenta Pagos en Linea. (Interlat GroupInterlat
 
CompTIA IT - Skills Gaps Study International
CompTIA IT - Skills Gaps Study InternationalCompTIA IT - Skills Gaps Study International
CompTIA IT - Skills Gaps Study InternationalAssespro Nacional
 

What's hot (19)

Ad Strategy - the YMCA
Ad Strategy - the YMCAAd Strategy - the YMCA
Ad Strategy - the YMCA
 
Nghien cuu cong_nghe_sx_dich_dam,_bot_ca
Nghien cuu cong_nghe_sx_dich_dam,_bot_caNghien cuu cong_nghe_sx_dich_dam,_bot_ca
Nghien cuu cong_nghe_sx_dich_dam,_bot_ca
 
RabbitMQ
RabbitMQRabbitMQ
RabbitMQ
 
Thirukkural
ThirukkuralThirukkural
Thirukkural
 
Thirukkural.
Thirukkural.Thirukkural.
Thirukkural.
 
Tecnicas de sql injection
Tecnicas de sql injectionTecnicas de sql injection
Tecnicas de sql injection
 
Dramatically increase revenue on verio template
Dramatically increase revenue on verio templateDramatically increase revenue on verio template
Dramatically increase revenue on verio template
 
The Security Risks of Web 2.0 - DEF CON 17
The Security Risks of Web 2.0 - DEF CON 17The Security Risks of Web 2.0 - DEF CON 17
The Security Risks of Web 2.0 - DEF CON 17
 
Replica Sets
Replica SetsReplica Sets
Replica Sets
 
Darren stanley
Darren stanleyDarren stanley
Darren stanley
 
Résistance à la chloroquine et réversion
Résistance à la chloroquine et réversionRésistance à la chloroquine et réversion
Résistance à la chloroquine et réversion
 
Buku panduan pengelolaan-e-journal
Buku panduan pengelolaan-e-journalBuku panduan pengelolaan-e-journal
Buku panduan pengelolaan-e-journal
 
Julius Joseph-SAP BASIS Resume
Julius Joseph-SAP BASIS ResumeJulius Joseph-SAP BASIS Resume
Julius Joseph-SAP BASIS Resume
 
Ekaw2010 tutorial3 practical
Ekaw2010 tutorial3 practicalEkaw2010 tutorial3 practical
Ekaw2010 tutorial3 practical
 
دراسة تحليلة لمشروع التصميم الحضري لمنطقة سد كمران
دراسة تحليلة لمشروع التصميم الحضري لمنطقة سد كمراندراسة تحليلة لمشروع التصميم الحضري لمنطقة سد كمران
دراسة تحليلة لمشروع التصميم الحضري لمنطقة سد كمران
 
NantKwest Chairman & CEO Dr. Patrick Soon-Shiong to Present Vision for Next G...
NantKwest Chairman & CEO Dr. Patrick Soon-Shiong to Present Vision for Next G...NantKwest Chairman & CEO Dr. Patrick Soon-Shiong to Present Vision for Next G...
NantKwest Chairman & CEO Dr. Patrick Soon-Shiong to Present Vision for Next G...
 
Ppt fiep x_iglesias_2012
Ppt fiep x_iglesias_2012Ppt fiep x_iglesias_2012
Ppt fiep x_iglesias_2012
 
6.Conocimiento cliente Cuenta Pagos en Linea. (Interlat Group
6.Conocimiento cliente Cuenta Pagos en Linea. (Interlat Group6.Conocimiento cliente Cuenta Pagos en Linea. (Interlat Group
6.Conocimiento cliente Cuenta Pagos en Linea. (Interlat Group
 
CompTIA IT - Skills Gaps Study International
CompTIA IT - Skills Gaps Study InternationalCompTIA IT - Skills Gaps Study International
CompTIA IT - Skills Gaps Study International
 

Viewers also liked

Hegemonía Inglesa o Británica
Hegemonía Inglesa o Británica Hegemonía Inglesa o Británica
Hegemonía Inglesa o Británica KelyDiaz
 
Prevention Center Project
Prevention Center ProjectPrevention Center Project
Prevention Center ProjectHeba Yousef
 
How did we use media technologies in the
How did we use media technologies in theHow did we use media technologies in the
How did we use media technologies in thehamzabdilahi
 
How did we use media technologies in the
How did we use media technologies in theHow did we use media technologies in the
How did we use media technologies in thehamzabdilahi
 
Analysis of a previous students work
Analysis of a previous students workAnalysis of a previous students work
Analysis of a previous students workhamzabdilahi
 
UXvue presentation at FIWARE Meetup - July 30th 2015
UXvue presentation at FIWARE Meetup - July 30th 2015UXvue presentation at FIWARE Meetup - July 30th 2015
UXvue presentation at FIWARE Meetup - July 30th 2015UXprobe
 
FINAL PAPER- Salvador, Kimberly V.
FINAL PAPER- Salvador, Kimberly V.FINAL PAPER- Salvador, Kimberly V.
FINAL PAPER- Salvador, Kimberly V.gorgeouskim
 
UXprobe workshop at Dare Festival 2016
UXprobe workshop at Dare Festival 2016UXprobe workshop at Dare Festival 2016
UXprobe workshop at Dare Festival 2016UXprobe
 
FINAL PAPER- SALVADOR KIMBERLY V.
FINAL PAPER- SALVADOR KIMBERLY V.FINAL PAPER- SALVADOR KIMBERLY V.
FINAL PAPER- SALVADOR KIMBERLY V.gorgeouskim
 
UXprobe - Presentation @ Buy from Startups
UXprobe - Presentation @ Buy from StartupsUXprobe - Presentation @ Buy from Startups
UXprobe - Presentation @ Buy from StartupsUXprobe
 
UX Workshop at Startit@KBC
UX Workshop at Startit@KBCUX Workshop at Startit@KBC
UX Workshop at Startit@KBCUXprobe
 
Conventions of a gangster film
Conventions of a gangster film Conventions of a gangster film
Conventions of a gangster film hamzabdilahi
 

Viewers also liked (16)

Hegemonía Inglesa o Británica
Hegemonía Inglesa o Británica Hegemonía Inglesa o Británica
Hegemonía Inglesa o Británica
 
Prevention Center Project
Prevention Center ProjectPrevention Center Project
Prevention Center Project
 
How did we use media technologies in the
How did we use media technologies in theHow did we use media technologies in the
How did we use media technologies in the
 
How did we use media technologies in the
How did we use media technologies in theHow did we use media technologies in the
How did we use media technologies in the
 
Analysis of a previous students work
Analysis of a previous students workAnalysis of a previous students work
Analysis of a previous students work
 
Presentation1
Presentation1Presentation1
Presentation1
 
UXvue presentation at FIWARE Meetup - July 30th 2015
UXvue presentation at FIWARE Meetup - July 30th 2015UXvue presentation at FIWARE Meetup - July 30th 2015
UXvue presentation at FIWARE Meetup - July 30th 2015
 
Hagen Photography Inc.
Hagen Photography Inc.Hagen Photography Inc.
Hagen Photography Inc.
 
FINAL PAPER- Salvador, Kimberly V.
FINAL PAPER- Salvador, Kimberly V.FINAL PAPER- Salvador, Kimberly V.
FINAL PAPER- Salvador, Kimberly V.
 
UXprobe workshop at Dare Festival 2016
UXprobe workshop at Dare Festival 2016UXprobe workshop at Dare Festival 2016
UXprobe workshop at Dare Festival 2016
 
FINAL PAPER- SALVADOR KIMBERLY V.
FINAL PAPER- SALVADOR KIMBERLY V.FINAL PAPER- SALVADOR KIMBERLY V.
FINAL PAPER- SALVADOR KIMBERLY V.
 
UXprobe - Presentation @ Buy from Startups
UXprobe - Presentation @ Buy from StartupsUXprobe - Presentation @ Buy from Startups
UXprobe - Presentation @ Buy from Startups
 
Role of media in our lives
Role of media in our livesRole of media in our lives
Role of media in our lives
 
Pc tv2
Pc tv2Pc tv2
Pc tv2
 
UX Workshop at Startit@KBC
UX Workshop at Startit@KBCUX Workshop at Startit@KBC
UX Workshop at Startit@KBC
 
Conventions of a gangster film
Conventions of a gangster film Conventions of a gangster film
Conventions of a gangster film
 

Similar to Getting Started With Play Framework

レッツゴーデベロッパー2011「プログラミングGroovy〜G*エコシステム編」
レッツゴーデベロッパー2011「プログラミングGroovy〜G*エコシステム編」レッツゴーデベロッパー2011「プログラミングGroovy〜G*エコシステム編」
レッツゴーデベロッパー2011「プログラミングGroovy〜G*エコシステム編」Yasuharu Nakano
 
Танки_в_Лунапарке: нагрузочное_тестирование_в_Яндексе
Танки_в_Лунапарке: нагрузочное_тестирование_в_ЯндексеТанки_в_Лунапарке: нагрузочное_тестирование_в_Яндексе
Танки_в_Лунапарке: нагрузочное_тестирование_в_ЯндексеYandex
 
WALA Tutorial at PLDI 2010
WALA Tutorial at PLDI 2010WALA Tutorial at PLDI 2010
WALA Tutorial at PLDI 2010Julian Dolby
 
[Harvard CS264] 04 - Intermediate-level CUDA Programming
[Harvard CS264] 04 - Intermediate-level CUDA Programming[Harvard CS264] 04 - Intermediate-level CUDA Programming
[Harvard CS264] 04 - Intermediate-level CUDA Programmingnpinto
 
Media sosial mention2011-libre
Media sosial mention2011-libreMedia sosial mention2011-libre
Media sosial mention2011-libreMelur Orkid
 
User Engagement - A scientific Challenge
User Engagement - A scientific ChallengeUser Engagement - A scientific Challenge
User Engagement - A scientific ChallengeJanette Lehmann
 
SEO - It Works Even if You Don’t Know How or Why
SEO - It Works Even if You Don’t Know How or Why SEO - It Works Even if You Don’t Know How or Why
SEO - It Works Even if You Don’t Know How or Why Wolfgang Weicht
 
Massive device deployment - EclipseCon 2011
Massive device deployment - EclipseCon 2011Massive device deployment - EclipseCon 2011
Massive device deployment - EclipseCon 2011Angelo van der Sijpt
 
Distributed Object Computing
Distributed Object ComputingDistributed Object Computing
Distributed Object ComputingEmmanuel Fuchs
 
Explorer X - Alpha Teaser
Explorer X  - Alpha   TeaserExplorer X  - Alpha   Teaser
Explorer X - Alpha TeaserLM Preston
 
8 Steps to Maximize Your Financial Aid Package
8 Steps to Maximize Your Financial Aid Package8 Steps to Maximize Your Financial Aid Package
8 Steps to Maximize Your Financial Aid PackageBrian Davidson
 
What Is Social Media
What Is Social MediaWhat Is Social Media
What Is Social MediaHAL
 

Similar to Getting Started With Play Framework (20)

Device deployment
Device deploymentDevice deployment
Device deployment
 
レッツゴーデベロッパー2011「プログラミングGroovy〜G*エコシステム編」
レッツゴーデベロッパー2011「プログラミングGroovy〜G*エコシステム編」レッツゴーデベロッパー2011「プログラミングGroovy〜G*エコシステム編」
レッツゴーデベロッパー2011「プログラミングGroovy〜G*エコシステム編」
 
All about Apache ACE
All about Apache ACEAll about Apache ACE
All about Apache ACE
 
Танки_в_Лунапарке: нагрузочное_тестирование_в_Яндексе
Танки_в_Лунапарке: нагрузочное_тестирование_в_ЯндексеТанки_в_Лунапарке: нагрузочное_тестирование_в_Яндексе
Танки_в_Лунапарке: нагрузочное_тестирование_в_Яндексе
 
WALA Tutorial at PLDI 2010
WALA Tutorial at PLDI 2010WALA Tutorial at PLDI 2010
WALA Tutorial at PLDI 2010
 
[Harvard CS264] 04 - Intermediate-level CUDA Programming
[Harvard CS264] 04 - Intermediate-level CUDA Programming[Harvard CS264] 04 - Intermediate-level CUDA Programming
[Harvard CS264] 04 - Intermediate-level CUDA Programming
 
Media sosial mention2011-libre
Media sosial mention2011-libreMedia sosial mention2011-libre
Media sosial mention2011-libre
 
InnoDB Magic
InnoDB MagicInnoDB Magic
InnoDB Magic
 
User Engagement - A scientific Challenge
User Engagement - A scientific ChallengeUser Engagement - A scientific Challenge
User Engagement - A scientific Challenge
 
SEO - It Works Even if You Don’t Know How or Why
SEO - It Works Even if You Don’t Know How or Why SEO - It Works Even if You Don’t Know How or Why
SEO - It Works Even if You Don’t Know How or Why
 
Massive device deployment - EclipseCon 2011
Massive device deployment - EclipseCon 2011Massive device deployment - EclipseCon 2011
Massive device deployment - EclipseCon 2011
 
Camino orar7dias
Camino orar7diasCamino orar7dias
Camino orar7dias
 
João Rodrigues - be IN Bragança
João Rodrigues - be IN BragançaJoão Rodrigues - be IN Bragança
João Rodrigues - be IN Bragança
 
Distributed Object Computing
Distributed Object ComputingDistributed Object Computing
Distributed Object Computing
 
SER - SIP Express Router
SER - SIP Express RouterSER - SIP Express Router
SER - SIP Express Router
 
Explorer X - Alpha Teaser
Explorer X  - Alpha   TeaserExplorer X  - Alpha   Teaser
Explorer X - Alpha Teaser
 
Analisis estructural parte 3
Analisis estructural parte 3Analisis estructural parte 3
Analisis estructural parte 3
 
Thai fried pineapple
Thai fried pineappleThai fried pineapple
Thai fried pineapple
 
8 Steps to Maximize Your Financial Aid Package
8 Steps to Maximize Your Financial Aid Package8 Steps to Maximize Your Financial Aid Package
8 Steps to Maximize Your Financial Aid Package
 
What Is Social Media
What Is Social MediaWhat Is Social Media
What Is Social Media
 

Getting Started With Play Framework

  • 1.
  • 2. BrouGHt to you By: 202 Getting Started with Play Framework © DZone, Inc. | DZone.com By Ryan Knight » create a new Play App » Routing Requests » controllers » Server-side Templates » JSon » Static Assets » Asset compiler CONTENTS Java EntErprisE Edition 7 introduction to play FramEwork The Play Framework is a high velocity web framework for 2* (0$0!*(!/$%#$(5,.+ 10%2!3+.'ý+3 10 +!/*Ě0/.%ü!/(%(%05ċ(5!01.!/ė1/0ġ $%0ġ.!.!/$Ę3+.'ý+30$0!*(!/.,% !2!(+,)!*0 5(!ċ$!+),%(0%+** ,,(%0%+*.!(+ %*#$,,!*/ !$%* 0$!/!*!/ċ(5%/1%(0+.0$!)+ !.*3!5 !%*#*+*ġ(+'%*#Č1(5 !1(0Č* $2%*#1%(0ġ %*//!0+),%(!./+.)+ !.*(%!*0ġ/% !0!$*+(+#%!/(%'! ƫ+û!!.%,0*
  • 3. ċ crEatE a nEw play app The easiest way to get started with Play is with Typesafe Ƶ0%20+.ġ*+,!*/+1.!0++(+./0.0%*#*!3,.+!0/ 1/%*#0$!5,!/!(0+.)ċ+#!0/0.0! č āċ +3*(+ 5,!/!Ƶ0%20+.čĪ05,!/!ċ+)ĥ ,(0+.)ĥ#!0/0.0! īĨ$00,čĥĥ05,!/!ċ+)ĥ,(0+.)ĥ getstarted) Ăċ+((+30$!%*/0.10%+*/+*0$! +3*(+ ,#!0+ (1*$Ƶ0%20+.ċ ăċƫ.!0!*!3,,(%0%+*1/%*#0$!ė!((+(5 .)!3+.'Ę0!),(0!,,(%0%+*ċ 5+1.!1/%*#0$! Č0$!*5+1.*!3(5,,(%0%+*/$+1( .1*10+)0%((5ċ+3!2!.Č%5+1.!1/%*#0$!+))* (%*!Č0$!*5+1*.1*5+1.*!3(5,,(%0%+*3%0$ċĥ 0%20+.Ʒ.1*%*5+1.*!3,.+!0 %.!0+.5ċ +30$05+1$2!*!3(5,,(%0%+*1,* .1**%*#Č 5+1/$+1( !(!0+.+3/!0+Ī(+($+/0čĊĀĀĀīĨ$00,čĥĥ (+($+/0čĊĀĀĀĩ* /!!5+1.*!3,,(%0%+*ċ +#!00+0$!(+( +1)!*00%+*+.(5Č*2%#0!0+ Ī(+($+/0čĊĀĀĀĥĮ +1)!*00%+*īĨ$00,čĥĥ(+($+/0čĊĀĀĀĥĮ documentation) You can either use the basic code editor in Activator or +,!*5+1.,.+!0%* *0!((% +.(%,/!ċ *0$!Ƶ0%20+. Č navigate to codeČ* 0$!*/!(!0open in to generate the ,.+!0ü(!/+.5+1. ċ +1.*!3,.+!0$/0$!+((+3%*#(5+10č app The app source directory +. 2Č(Č* (%!*0ġ side sources conf $!+*ü# %.!0+.5 containing the route mapping and application +*ü# Get More Refcardz! Visit Refcardz.com GEttinG startEd witH play FramEwork public The static assets directory Ĩ!ċ#ċ%)#!/Č
  • 4. ČƫČ JavaScript) test The test source directory You can have both Java and Scala sources for your backend + !ċ+3!2!.(5$/*% %+)0% 2Ƶ * /!,.0!% %+)0%(Ƶ !1/!0$!% %+)/ %û!. !03!!*0$!03+(*#1#!/ċ$%/.!0!/*!4,!.%!*!0$0 ė!!(/.%#$0Ę3%0$3$0!2!.(*#1#!5+11/!ċ$!,'#!/ in Play for Scala begin with play.api while the Java API lives under the play,'#!,.!ü4ċ routinG rEquEsts The conf/routesü(! !ü*!/$+3(5.+10!/.!-1!/0//! +*0$!%.2!.* ,0$ċ$!*5+1) !.!-1!/00+ http://localhost:9000 your browser made a GEt.!-1!/0 3%0$ĥ,0$ċ+1*/!!%*0$! !1(0routesü(!0$!.!%/ .+10%*# !ü*%0%+*0+$* (!0$0/!č GET / controllers.Application.index() The third parameter is the method that will be responsible +.$* (%*#0$!.!-1!/0* .!01.*%*#.!/,+*/!ċ$! /0.101.!+0$!.+10!/ü(!%/č VERB PATH CONTROLLER_METHOD *(52(% 2!./.!((+3! ċ 5+1$*#!GEt to Foo you will get a compile error indicating that Foo is not a 2(% 2!.ċ The path part of the routesü(!*!,.)!0!.%6! 0+!40.0%*+.)0%+** ,//%00+0$!+*0.+((!.ċ+. %*/0*!Č0+,1((*% +10+,0$5+13+1( +č
  • 5. 3 GETTING STARTED WITH PLAY FRAMEWORK GET /user/:id controllers.User.get(id) $!č)0$!/+*+*!ĥ/!,.0! /!#)!*0((+3%*#5+10+ !40.0)1(0%,(!2(1!/(%'!č GET /user/:id/:name controllers.User.get(id, name) +1*(/+!40.00$!.!/0+0$!,0$1/%*#ĵ(%'!č ȇ0,*1%+$ȇȋƭ) ,+1/,))/0ǣ,*1%+$ǣ $1ȁƭ)Ȃ 0$/*(/+1/!.!#!4!4,.!//%+*/0+(%)%03$00$!5)0$ +*ċ 1!.5/0.%*#,.)!0!./*!10+)0%((5!40.0! %*0+ +*0.+((!.)!0$+ ,.)!0!./ċ+$* (!GEt.!-1!/00+/ foo?bar=neat !ü*!.+10!(%'!č ȇ#,, ,+1/,))/0ǣ ,,ǣ$1ȁ/ǥʛ0001/+$Ȃ $!-1!.5/0.%*#,.)!0!./.!05,!ġ/!Č/+%5+1/!00$! type as intČ0$!*0$!.!3%((!*!..+.%0$!,.)!0!.**+0 be converted to an intċ +1*(/+$2! !1(0* +,0%+*(,.)!0!./ċ *!+0$!.!/+*/0$0(5+),%(!/0$!.+10!/ü(!%/0+ provide a reverse routing API so that you never have to hard + !
  • 6. /%*0+5+1.,,(%0%+*ċ */0! Č5+1(()!0$+ %*0$!.!2!./!.+10!.Č3$%$.!01.*/0$!.+10! !ü*! 50$! ė.+10!/Ęü(!ċ$%/!*(!/5+10+!/%(5.!0+.5+1.
  • 7. / 3%0$+10.!'%*#5+1.,,ċ © DZone, Inc. | DZone.com controllErs ƫ+*0.+((!./%*(5.!.!/,+*/%(!+.$* (%*#.!-1!/0* .!01.*%*#.!/,+*/!ċ!.!%//% 2+*0.+((!.Ĩ3$%$ would live in app/controllers/Foo.javaĩč package controllers import play.api._ import play.api.mvc._ public class Foo extends Controller { public static Result get() { return ok(“hello”); } } ǫ5!40!* %*#0$!/!controller(//Č3!,1((%*/+)! +*2!*%!*!)!0$+ /Č10 +%*#/+%/*+0.!-1%.! ċ$!get() method is static because Play is stateless and static methods .!*!/5350+ !ü*!)!0$+ 0$0 +!/*+0$*#!*5 /00!ċ+1*(/+1/! !,!* !*5%*!0%+*.)!3+.'(%'! Spring or Guice with Play if you want to avoid using static )!0$+ /ċ$!get() method returns a resultČ3$%$.!,.!/!*0/ 0$!.!/,+*/!ċ *0$%//!Č0$!.!/,+*/!%//001/+ ! ĂĀĀ.!/,+*/!!1/!0$!ok helper was used to set that status + !ċ$!.!.!)*5+0$!.$!(,!./(%'!notFound and badrequest that wrap the general purpose statusƵ ċ$! .!/,+*/!+ 5%*0$%/!4),(!%/1/0string but it could also !
  • 8. +*0!*0Č/0.!)Č+.ü(!Č!0ċ $!+..!/,+* %*#(+*0.+((!.%/-1%0!/%)%(.Ĩ* 3+1( live in app/controllers/Foo.scalaĩč package controllers import play.api.mvc.{Action, Controller} object Foo extends Controller { def get = Action { Ok(“hello”) } } $!,.%).5 %û!.!*!3%0$0$%/(!4),(!%/0$00$! controller returns an action which holds a function that takes a .!-1!/0Ĩ+,0%+*((5/,!%ü! ĩ* .!01.*/.!/,+*/!ċƵ(/+*+0! that the return status code alias ok begins with an uppercase $.0!.ċ The controller(//Ĩ%*0$! 2Ƶ ĩ$//+)!+*2!*%!*! )!0$+ /0+%*0!.03%0$0$!+0$!.,.0/+0$!.!-1!/0* .!/,+*/!č 04Ĩĩ !01.*/0$!+*0!40 which can be used to store storing /00!.!(!2*00+1/00$%/ .!-1!/0 ý/$ĨĩČý/$Ĩ0.%*#'!5ĩČ * ý/$Ĩ0.%*#'!5Č0.%*# value) Can be used to access state that is only available for /%*#(!.!-1!/00!.0$! 1..!*0+*!Ĩ0$%/%/1/!1( for displaying messages after a redirect) .!-1!/0Ĩĩ !01.*/0$!1..!*0 .!-1!/0+!0Č3$%$* !1/! +..! %*# .!-1!/0$! !./* +0$!. )!0 0+100$!.!-1!/0 .!/,+*/!Ĩĩ !01.*/0$!1..!*0 .!/,+*/!+!0Č3$%$ *!1/! 0+/!0++'%!/Č $! !./Č!0ċ /!//%+*ĨĩČ/!//%+*Ĩ0.%*# '!5ĩČ* /!//%+*Ĩ0.%*#'!5Č String value) Can be used to access 0$!/!//%+*/00!Č3$%$%/ backed by a cookie *0$!(Ƶ Č0$!/!05,!/++,!.0%+*/+* +*!!%0$!.+* the action1*0%+*Ě/+,0%+*(.!-1!/0,.)!0!.+.+*0$! resultČ+.!4),(!č def get = Action { request = Ok(“asdf”).withHeaders(request. headers(“foo”) - “bar”) } The other response code helper methods on Controller are Ĩ!#%**%*#3%0$1,,!./!$.0!./+.0$!(Ƶ ĩč mEtHod EquivalEnt to !-1!/0 ąĀĀ
  • 9. 4 GETTING STARTED WITH PLAY FRAMEWORK mEtHod EquivalEnt to created ĂĀā forbidden ąĀă found ăĀĂ %*0!.*(!.2!...+. ĆĀĀ movedPermanently ăĀā noContent ĂĀą notFound ąĀą ok ĂĀĀ redirect ăĀă seeOther ăĀă temporaryRedirect ăĀĈ 1*10$+.%6! ąĀā status Ƶ*5/001/Ģ!ċ#ċ © DZone, Inc. | DZone.com /001/ĨāĀĀČė$!((+Ęĩ ƫ+*0.+((!./%*(5.!%*0!.*((5/5*$.+*+1/* *+*ġ (+'%*#ċ 5+1.+*0.+((!.+ !%/*+0*+*ġ(+'%*#0$!*5+1. +*0.+((!./*1/0.!01.*resultċ+3!2!.Č%5+1.+*0.+((!. + !%/*+*ġ(+'%*#Č0$!*%*0$! 2Ƶ 5+1*.!01.* promiseresult%*/0! +1/0resultċ *0$!(Ƶ Č1/! action.async and return a Futureresult(%'!č def get = Action.async { Future.successful(Ok(“asdf”)) } Interceptors can be added to controllers in order to add /!1.%05Č(+##%*#Č$%*#Č* +0$!.1/0+)!$2%+./ċ$%/%/ called action compositionċ *(5Ě/ 2Ƶ Č**+00%+*/.! 1/! 0+ 0$!%*0!.!,0+./ċ *(Čaction composition is $%!2! 0$.+1#$1*0%+*(+),+/%0%+*ċ ƫ+*0.+((!./#+)1$ !!,!.0$*0$!05,%(.!-1!/0* .!/,+*/!$* (%*#ċ+.%*/0*!Č+*0.+((!.*.!01.* /0.!)+.%0*!1/! 0+/!01,,1/$+**!0%+*Ĩƫ+)!0Č 2!*0+1.!Č!+'!0Č!0ĩċƫ+*0.+((!./*(/+$* (! )+.!0$*1/0
  • 10. Ď0$!5*!1/! +. Č%*.5ü(!/Č+. any content type using custom Body parsersċ sErvEr-sidE tEmplatEs !,,(%0%+*/*1/!/!.2!.ġ/% !0!),(0!//35 0+.!0!
  • 11. +*0!*0ċ *(5Č0$! !1(0/!.2!.ġ/% ! 0!),(0%*#(*#1#!%/(ċ$!.!.!(/+*1)!.+1/+0$!. plugins that support a large variety of other templating (*#1#!/%*(1 %*# Č.++25Č* *1)!.+1/ 2.%,0ġ /! 0!),(0%*#(%..%!/ċ+1/!0$!(0!),(0!/Č.!0! a something.scala.htmlü(!%*0$!app/views %.!0+.5ċ$! *)%*#+0$!ü(!%/1/! 0+*)!0$!1*0%+*0$03%((! ((! 0+.!* !.0$!0!),(0!ċ patH BEcomEs ,,ĥ2%!3/ĥ++ċ/(ċ$0)( 2%!3/ċ$0)(ċ++ ,,ĥ2%!3/ĥ/ ĥǫ.ċ/(ċ html 2%!3/ċ$0)(ċ/ ċǫ. +1/!0$!+),%(! 0!),(0!.+) 2Č/%),(5((0$!.!* !. /00%)!0$+ č views.html.Foo.render() .+)(Č1/!0$!apply1*0%+*č views.html.Foo() !),(0!/*0'!,.)!0!./Č/+0$!Ĩ+,0%+*(ĩü./0(%*!+ (0!),(0!%/0$!,.)!0!./ċ2!.5(/00!)!*0%* (0!),(0!%/,.!ü4! 3%0$*ĮČ/+0+/,!%50$0 template takes a string,.)!0!.Č1/!0$!+((+3%*#č ȕȁ*00$ǥ1/+$Ȃ $!+ 5+0$!0!),(0!%/1/0+)%*0%+*+ėĮĘ,.!ü4! (/00!)!*0/* .3
  • 12. ċ+.%*/0*!č ȕȁ11)ǥ1/+$Ȃ !DOCTYPE html html head title@title/title /head body hello, world /body Since the Scala templates are compiled into functions they are !/%(5+),+/! ċ 0$!,.!2%+1/!4),(!%/*)! main.scala. htmlČ0$!*0+.!1/!%0.+)3%0$%**+0$!.0!),(0!/%),(5 +č @Main(“foo”) 5,%(0!),(0!+,!.0%+*/(%'!(++,/1/01/!*+.)(( !4,.!//%+*/(%'!č @for(user - users) { li@user.getName()/li } Ƶ+* %0%+*(ė%Ę/00!)!*03+1( (++'(%'!č @if(items.isEmpty()) { h1Nothing to display/h1 } else { h1@items.size() items!/h1 } The Scala templates include a number of other features and ,00!.*/(%'!.!1/(!
  • 13. +),+*!*0/%*(1 %*#+.)/2% the @form1*0%+*ċ*!+0$!$1#!!*!ü0/+0$!( templates is that you will see compile errors in your browser 1/0(%'!5+1 +3%0$+*0.+((!./Č.+10!/Č* !2!.50$%*#!(/!0$0 %/+),%(! 5(5ċ Json * %0%+*0+.!#1(.
  • 14. +*0!*0Č(5+*0.+((!./*(/+ .!!%2!* .!01.* /!.%(%6! 0ċ$!(5 2Ƶ 3.,/ 0$!,+,1(. '/+*(%..53%0$/+)!+*2!*%!*!1*0%+*/ċ !.!%/*!4),(! 2+*0.+((!.0$0.!!%2!//+)! Č ,./!/%0Č0$!*.!ġ/!.%(%6!/%0Č* .!01.*/%0%*0$!.!/,+*/!č
  • 15. 5 GETTING STARTED WITH PLAY FRAMEWORK © DZone, Inc. | DZone.com package controllers; import play.libs.Json; import play.mvc.Controller; import play.mvc.Result; public class BarController extends Controller { public static class Bar { -2) 1/+$+*Ǧ } public static Result bar() { Bar bar = Json.fromJson(request(). body().asJson(), Bar.class); return ok(Json.toJson(bar)); } } $!/)!0$%*#%*(3+.'/%*/%)%(.35Č101/!/ ).+ġ/! Ƶ 0+#!*!.0!0$!/!.%(%6!.* !ġ/!.%(%6!.0 +),%(!0%)!Č0$1/2+% %*#0$!1/!+.1*0%)!.!ý!0%+*č package controllers import play.api.mvc.{Action, Controller} import play.api.libs.json.Json 0 )00/ȁ+*ǥ1/+$Ȃ object BarController extends Controller { implicit val barFormat = Json.format[Bar] def bar = Action(parse.json) { request = val bar = request.body.as[Bar] Ok(Json.toJson(bar)) } } $!/!!4),(!//$+3! /!.%(%6%*#* !ġ/!.%(%6%*#*+!0ċ Both the Java and Scala APIs in Play have methods for 0.2!./%*# /0.101.!0+(+0!* !40.0 0Č/3!(( /)!0$+ /0+.!0!* )*%,1(0! /0.101.!/ċ +/!01,.+10%*#0+!%0$!.+0$!/!+*0.+((!.)!0$+ /Č 0$! +((+3%*#0+5+1..+10!/ü(!č ȇ/ controllers.BarController.bar() static assEts The public directory contains static assets that do not need to #+0$.+1#$+),%(0%+*,.+!//0+!1/! %*0$!.+3/!.ċ There is a default mapping in the conf/routesü(!0$0/!0/1, a way to serve these assets from the /assets/
  • 16. ,.!ü41/%*# (5Ě/1%(0ġ%*assets+*0.+((!.č ȇ0010ȇȋƭ) ,+1/,))/0ǣ0010ǣ1ȁ-1%ʛDZȇ-2) DZǤƭ)Ȃ Ƶ0ü./0#(*!%0/!!)/0$00$!/!//!0/.!!%*#.! %.!0(5+10+0$!ü(!/5/0!)ċ+3!2!.Č +%*#/+3+1( )'! (5,,(%0%+*/)+.! %þ1(00+ !,(+5/%*!(51/!/ +*0%*!.ġ(!// !,(+5)!*0)+ !(0$0%/1(0%)0!(51/0 1*$+ .ü(!/ċ */0! Č(5Ě/1%(0ġ%*Ƶ//!0/+*0.+((!. /!.2!///!0/.+)3%0$%*0$! 2(//,0$ċ$!public directory is actually a source directory that puts its contents into a public,'#!%*0$!(//,0$Ĩ+.#!*!.0! .ü(! 3$!*.!0%*# %/0.%10%+*ĩċ +(+ *//!02%/!.2!.ġ/% !0!),(0!Č1/!0$!.!2!./! .+10!.0+#!00$!.%#$0
  • 17. Č(%'!č img src=”@routes.Assets.at(“images/favicon. png”)” %2!*0$!,.!2%+1/.+10%*# !ü*%0%+*Č0$!.!2!./!.+10!.3%(( resolve that to the /assets/images/favicon.png,0$ċ assEt compilEr Play has an asset compiler1%(0ġ%*0$03%((+),%(!(%!*0ġ /% !//!0/(%'!ƫ+û!!.%,0*
  • 18. /,.0+0$!*+.)( +),%(0%+*,.+!//ċ$%/,.+!//3%(((/+)%*%5 2.%,0 .!/+1.!/0+.! 1!0$!%./%6!ċƵ//!0/0+!+),%(! #+%* either the app/assets/javascripts or app/assets/stylesheets %.!0+.5ċ+.!4),(!Č*!3app/assets/javascripts/index. +û!!ü(!3%((!+),%(! * ! 0+0$!(//,0$/ assets/javascripts/index.js* )%*%ü! /assets/stylesheets/ index.min.js.+(+ 0$!)%*%ü! 2.%,02%/!.2!.ġ/% ! 0!),(0!Č1/!č script src=”@routes.Assets.at(“javascripts/ index.min.js”)”/script +.,.+ 10%+* %/0.%10%+*/Č(53%(((/+ + 2.%,0 +*0!*0%+*ċ$!.!.!*1)!.++0$!.+,!*/+1.!//!0 +),%(!.,(1#%*/+.(5ċƫ$!'+100$!(5(1#%* %.!0+.5č $00,čĥĥ333ċ,(5.)!3+.'ċ+)ĥ +1)!*00%+*ĥĂċĂċ4ĥ + 1(!/ tEstinG The test %.!0+.5+*0%*/0$!1*%0Č1*0%+*(Č* %*0!#.0%+* 0!/0/+.5+1.,.+!0ċ+1*3.%0!5+1.0!/0/3%0$*5 .)!3+.'0$0$/ *%0+),0%(!0!/0.1**!.ċ(5$/ /+)!/,!%ü$!(,!./+. *%0* ,!/ĂĨ(0!/0%*# .)!3+.'ĩċƵ((+0$! %û!.!*0,.0/+(5,,(%0%+* *!0!/0! %* !,!* !*0(53%0$+10/0.0%*#/!.2!.ċ * 0!/05+1*(/+/0.0(5/!.2!.Č)'!01(.!-1!/0/ #%*/00$!/!.2!.Č* 0!/00$! 3%0$!(!*%1)0$.+1#$ '!.+3/!.Ĩ
  • 19. *%0ĩ+.0$.+1#$.!(.+3/!.ċ!.!%/ simple Java and JUnit test of the bar controller method on the Barcontroller .+)+2!č
  • 20. 6 GETTING STARTED WITH PLAY FRAMEWORK © DZone, Inc. | DZone.com import controllers.BarController; import controllers.routes; import org.junit.*; import play.libs.Json; import play.mvc.*; import play.test.FakeRequest; import static play.test.Helpers.*; import static org.fest.assertions. Assertions.*; public class BarControllerTest { @Test public void +!5%,2)!,+1+%,// 11/+$ȁȂȅ running(fakeApplication(), new Runnable() { public void run() { BarController.Bar bar = new BarController.Bar(); bar.name = “foo”; FakeRequest fakeRequest = new FakeRequest().withJsonBody(Json. toJson(bar)); Result result = callAction(routes.ref.BarController.bar(), fakeRequest); assertThat(status(result)). isEqualTo(OK); assertThat(contentType(result)). isEqualTo(“application/json”); assertThat(Json. -/0ȁ ,+1+101/+$ȁ/02)1ȂȂǣ$1ȁǰ+*DZȂǣ asText()).isEqualTo(bar.name); } }); } } $!/)!0$%*#3%0$(* ,!/Ă3+1( !č import controllers.{BarController, Bar} import org.specs2.mutable._ import play.api.libs.json.Json import play.api.test._ import play.api.test.Helpers._ )00/,+1/,))/- 51+!0 - ƭ 1,+ȅ “BarController” should { ǰ0%,2)!/12/+ /DZ++4 WithApplication { val bar = Bar(“foo”) val result = BarController. bar(FakeRequest().withBody(Json.toJson(bar) (BarController.barFormat))) status(result) must equalTo(OK) contentType(result) must ,*ȁǰ--) 1,+ȇ'0,+DZȂ (contentAsJson(result) “name”). 0ȃ1/+$Ȅ*201.2),ȁ/ǣ+*Ȃ } } } You can run the tests either from the Activator UI or from the +))* (%*!1/%*#ċ/activator testċ conFiGuration The conf/application.confü(!+*0%*/5+1.,,(%0%+*Ě/ !1(0+*ü#1.0%+*ċ$!.!5+1*+2!..% !+*ü#+. !ü*! 5+1.+3*ċ+.%*/0*!Č%5+13*00+.!0!*!3+*ü# parameter named foo with a value of barČ5+13+1( /%),(5 0$!+((+3%*#0+0$!ü(!č foo=bar +.! 0$0+*ü#%* 2Č5+13+1( 1/!č 0.%*#++œ(5ċ,,(%0%+*Ĩĩċ+*ü#1.0%+*Ĩĩċ #!00.%*#Ĩė++ĘĩĎ *(Č0$%*#/.!/%)%(.!4,!00$0getstring returns an option[string]: 3)*6 ,,ǥ-1,+ȃ1/+$Ȅʛ)6ǣ 2//+1ǣ ,+ƭ$2/1,+ǣ$11/+$ȁǰ#,,DZȂ +1*/,!%5 %0%+*(+*ü#ü(!/0+ !(3%0$+*ü#1.0%+* 0$02.%!/!03!!*!*2%.+*)!*0/ċ(5Ě/+*ü#/5/0!)%/1%(0 +*0$!5,!/!ƫ+*ü#(%..5č$00,/čĥĥ#%0$1ċ+)ĥ 05,!/!$1ĥ+*ü# Build Play uses the sbt1%( 0++(+.)*#%*# !,!* !*%!/Č +),%(%*#0$!,,Č.1**%*#0$!,,Č* .1**%*#0$!0!/0/ċ$! project/build.propertiesü(!/,!%ü!/0$!2!./%+*+sbt0+1/!ċ Any sbt plugins can be added in the project/plugins.sbtü(!ċ $!,.%).51%( !ü*%0%+*%/%*0$!build.sbtü(!Č3$%$3%(( (++'/+)!0$%*#(%'!č name := “””hello-play””” 3/0,+ǥʛǰǐǣǏǸDZ )//6-+!+ 0ʖʖʛ.ȁ javaCore, “org.webjars” %% “webjars-play” % “2.2.0” // Add your own project dependencies in the form: // “group” % “artifact” % “version” ) -)6ǣ/,' 1ǣ-)6 311+$0 +0!č+.(,.+!0/)'!/1.!0+1/!,(5ċ.+!0ċ ,(5(!00%*#/%*/0! +,(5ċ.+!0ċ,(5 2!00%*#/ċ$%/ changes some defaults in Play’s template compiler to make it )+.!% %+)0%(ċ The librarydependencies section of the build.sbt !ü*!/ the application dependencies that should be available in a ,1(% 2!*.!,+/%0+.5ċ+1*(/+ 5+1.+3* 2!* repository using the resolvers/!00%*#ċ$! !,!* !*%!/%* librarydependencies.!+))ġ/!,.0! (%/0%*0$!+.)č “group” % “artifact” % “version” Ƶ/*!4),(!Č0+ 0$! 5
  • 21. .%2!.Č 0$!+((+3%*#(%*!č “mysql” % “mysql-connector-java” % “5.1.26” Play has a number of optional dependencies with shortcut (%/!/č
  • 22. 7 GETTING STARTED WITH PLAY FRAMEWORK ü(0!./ ǫ1%(0ġ%*ü(0!./Ĩ%,Č!0ċĩ scala apis cache Cache API idbc ǫƫ+**!0%+*,++( anorm Ƶ*+.)(ǫ © DZone, Inc. | DZone.com
  • 23. %..5 Java apis 2ƫ+.! Core Java API 2 Java database API 2!* 2!*,(1#%* (5Ě/1%( (/+/1,,+.0//1ġ,.+!0//+0$05+1*,.0%0%+* 5+1.,,(%0%+*%*0+)1(0%,(!/)((!.,%!!/ċ$%/*%),.+2! 1%( 0%)!/* )'! %û!.!*0,%!!/)+.!!/%(5.!1/(!ċ FurtHEr lEarninG đ5,!/!,.+2% !/.!!+*(%*!(50.%*%*#+1./!č $00,/čĥĥ05,!/!ċ+)ĥ$+3ĥ+*(%*!ġ0.%*%*# đƵ0%20+.+*0%*/*1)!.++0$!.0!),(0!/0$03%(( #!05+1/0.0! (!.*%*#+10+0$!./,!0/+(5Č(%'!č đ/%*#,.%*#Ē Ƶ3%0$(5č$00,čĥĥ05,!/!ċ+)ĥ 0%20+.ĥ0!),(0!ĥ,(5ġ/,.%*#ġ 0ġ, đ(53%0$Ƶ*#1(. č$00,čĥĥ05,!/!ċ+)ĥ0%20+.ĥ 0!),(0!ĥ*#1(.ġ/!! ġ,(5 đ(53%0$ +*#+ǫ* *+'+10č$00,čĥĥ05,!/!ċ+)ĥ 0%20+.ĥ0!),(0!ĥ,(5ġ)+*#+ġ'*+'+10 đ+.1(((%/0+0!),(0!/$!'+10č$00,čĥĥ05,!/!ċ+)ĥ 0%20+.ĥ0!),(0!/ đ+#!0/0.0! 3%0$(5.)!3+.'Č$! +2!.0+ Typesafe’s Resource center $!.!.!*1)!.+1/,(1#%*/+.(5(%'!č đ! +(0ĂġƵ*10$+.%60%+*/5/0!)+.(5Ăč$00,/čĥĥ #%0$1ċ+)ĥ/$(+*!.ĥ ! +(0ġĂ đ)%((1#%*č$00,/čĥĥ#%0$1ċ+)ĥ05,!/!$1ĥ,(5ġ ,(1#%*/ĥ0.!!ĥ)/0!.ĥ)%(!. đƫ$!'+100$!1(((5(1#%*(%/0č đ$00,čĥĥ333ċ,(5.)!3+.'ċ+)ĥ +1)!*00%+*ĥĂċĂċ4ĥ + 1(!/ ABOUT THE AUTHOR RECOMMENDED BOOK Ryan Knight is a consultant and trainer for Typesafe where he helps others learn and use Scala, Akka and Play. Ryan frequently does training and presentations at conferences around the world, such as JavaOne, Devoxx, and many other Java get–togethers. He has over 15 years of experience with enterprise software development. He first started consulting with Enterprise Java in 1999 with the Sun Java Center. Since then he has worked with a wide variety of companies, such as the Oracle, LDS Church, Williams Pipeline, Riot Games, Sony, T-Mobile, Deloitte and the State of Louisiana. This has given him experience with wide range of business, such as genealogy, telecommunications, finance and video games. The Play Framework Cookbook is designed to give developers an intuitive feel for practical Play development. Filled with worked examples and detailed recipes for solutions to common problems, this book turns Play novices into experts quickly and efficiently. BUY NOW BROWSE OUR COLLECTION OF 250+ FREE RESOURCES, INCLUDING: RESEARCH GUIDES: Unbiased insight from leading tech experts REFCARDZ: Library of 200+ reference cards covering the latest tech topics COMMUNITIES: Share links, author articles, and engage with other tech experts JOIN NOW DZone, Inc. 150 Preston Executive Dr. Suite 201 Cary, NC 27513 888.678.0399 919.678.0300 Refcardz Feedback Welcome refcardz@dzone.com Sponsorship Opportunities sales@dzone.com Copyright © 2014 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Version 1.0 ĸĈċĊĆ DZone communities deliver over 6 million pages each month to more than 3.3 million software developers, architects and decision makers. DZone offers something for everyone, including news, tutorials, cheat sheets, research guides, feature articles, source code and more. DZone is a developer's dream, says PC Magazine. Text