Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play...
Transcript of Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play...
![Page 1: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/1.jpg)
Play framework 2.0Peter Hilton • geecon • 18 May 2012 • #playgeecon
![Page 2: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/2.jpg)
Peter Hilton
■ (sorry, I am not Guillaume Bort)■ Senior web developer and Operations Director at
Lunatech Research in Rotterdam, the Netherlands■ Web application architecture, design and construction■ Technical project management and functional design
■ Play framework committer since 2010■ Co-author of the book Play 2 for Scala (Manning)
2
![Page 3: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/3.jpg)
“Play brings type safe high-‐produc8vity web development to the JVMPresenta8on goal: persuade you that Play 2.0 is cool
3
![Page 4: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/4.jpg)
Outline
■ What Play is and why it matters
■ High-productivity web development, but on the JVM
■ Developer Experience (DX) that doesn’t suck
■ What’s new in Play 2.0 - it isn’t only about Scala
■ HTML5 web development - integrated third-party stuff
4
![Page 5: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/5.jpg)
What Play is
■ Full-stack web framework
■ Simple, flexible and powerful HTTP interface
■ High-productivity web development
■ Asynchronous HTTP programming model
■ High-performance scalable architecture
■ Designed by web developers for web developers
■ Play is fun
5
![Page 6: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/6.jpg)
“ It’s the web browser, stupidThat’s why Play maHers
6
![Page 7: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/7.jpg)
The Back buHon works
Play’s stateless architecture is based on HTTP.
When a web framework starts an architecture fight
with the web, the framework loses.
Why Play maAers 7
![Page 8: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/8.jpg)
Why Play maAers
The Reload buHon works
During development, just reload the page to see changes in your Java (or Scala) code.
That’s high-‐produc8vity web development.
Back buHon
8
![Page 9: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/9.jpg)
Back buHon
Why Play maAers
Reload buHon
You design the URL
You can use ‘clean’ URLs:
/products/product/42/product/42/comments
9
![Page 10: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/10.jpg)
Back buHon
Why Play maAers
Reload buHon
URLUsability (DX)
Convenient HTTP API and template syntax
Clear error messages and short stack traces
10
![Page 11: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/11.jpg)
“There is no story sorry.Feel free to forge one :)Guillaume Bort (on the heart-‐shaped favicon)
11
![Page 12: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/12.jpg)
“Play doesn’t fight HTTP or the browserStateless, HTTP-‐centric
12
![Page 13: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/13.jpg)
![Page 14: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/14.jpg)
Stateless architecture
■ Simplifies application development and testing
■ Matches the web’s stateless HTTP architecture
■ Avoids synchronising state between additional layers
■ (state belongs in the client or persistent storage)
■ Enables cloud deployment and horizontal scalability
■ (search the web for “Play framework Heroku”)
14
![Page 15: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/15.jpg)
“For years, PHP and Rails developers have been laughing at you… every 8me they reload a page’s code changes
15
![Page 16: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/16.jpg)
■ During development, reload the page to see changes in:
■ Java and Scala classes
■ configuration files
■ templates.
■ Play pre-compiles classes and templates for better performance in production mode
■ This just works out-of-the-box
Code reloading 16
![Page 17: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/17.jpg)
“URLs want to be loved tooREST architecture isn’t just for web service APIs
17
![Page 18: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/18.jpg)
h t t p : / / a p p . e x a m p l e . c o m / W a r R o o t D i r e c t o r y 1 / S e r v l e t s O n A P l a n e ? s e s s i o n I d = x 8 1 n j 3 8 a v n g j L O L d x p a n e w q & a c t i o n = N e x t P a g e & H o n e y B a d g e r C a r e s = f a l s e & e n t i t y I d = 1 2 9 9 1 2 7 4 3 & p r o c e s s N a m e = U n l a d e n S w a l l o w C o m p u t a t i o n & r o l e = p e o n & d a t e = 1 4 % 2 F 0 2 % 2 F 2 0 1 2 & f l a g S e t t i n g s = 0 1 0 1 0 1 1 & r e t u r n = % 2 F v i d e o s % 2 F r i c k r o l l . a v i
18
![Page 19: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/19.jpg)
URL design (HTTP rouKng)
■ Clean URLs are stable URLs:
■ http://example.com/products
■ http://example.com/product/42
■ Read it, bookmark it, mail it, tweet it
■ Configure your application’s URLs in one file:
19
# HTTP routes configuration file
# method, URL path, controller action method (and params)
GET /products controllers.Products.list()
GET /product/:id controllers.Products.details(id:Long)
![Page 20: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/20.jpg)
# HTTP routes configuraKon file
GET / controllers.Application.index()
GET /products controllers.Products.list()
POST /products controllers.Products.add(p: Product)
GET /product/:id controllers.Products.details(id: Long)
DELETE /product/:id controllers.Products.delete(id: Long)
GET /products.json controllers.Products.listJSON()
GET /product/:id.json controllers.Products.detailsJSON(id:Long)
20
![Page 21: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/21.jpg)
“You have to be a genius to successfully use some web frameworksFor the rest of us, there are good error messages
21
![Page 22: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/22.jpg)
22
![Page 23: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/23.jpg)
13:07:55,796 ERROR [[PersonServlet]] Servlet.service() for servlet PersonServlet threw exceptionjavax.ejb.EJBException: null; CausedByException is: null at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46) at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70) at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:32) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:189) at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107) at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:37) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:88) at $Proxy76.getAllPeople(Unknown Source) at uk.co.mediaport.web.PersonServlet.showTelephones(PersonServlet.java:54) at uk.co.mediaport.web.PersonServlet.doPost(PersonServlet.java:45) at uk.co.mediaport.web.PersonServlet.doGet(PersonServlet.java:34) at javax.servlet.http.HttpServlet.service(HttpServlet.java:697) at javax.servlet.http.HttpServlet.service(HttpServlet.java:810) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
Where’s the actual error message? 23
![Page 24: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/24.jpg)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112) at java.lang.Thread.run(Thread.java:595) java.lang.NullPointerException at uk.co.mediaport.core.PeopleBean.getAllPeople(PeopleBean.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:109) at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:32) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66) at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:61) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:32) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:91) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:189) at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107) at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:37) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98) at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:88) at $Proxy76.getAllPeople(Unknown Source) at uk.co.mediaport.web.PersonServlet.showTelephones(PersonServlet.java:54) at uk.co.mediaport.web.PersonServlet.doPost(PersonServlet.java:45) at uk.co.mediaport.web.PersonServlet.doGet(PersonServlet.java:34) at javax.servlet.http.HttpServlet.service(HttpServlet.java:697) at javax.servlet.http.HttpServlet.service(HttpServlet.java:810) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
24
![Page 25: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/25.jpg)
![Page 26: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/26.jpg)
“Play 2.0 con8nues the innova8onFirst-‐class support for both Java and Scala
Type-‐safe templates
Compile-‐8me checking
Asynchronous HTTP programming
26
![Page 27: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/27.jpg)
“Diversity maHersNow there’s Java and Scala
27
![Page 28: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/28.jpg)
■ Play 2.0 provides parallel APIs for Java and Scala,for example, a controller action:
Play in Java and Scala 28
// Scala controller action method
def hello(name: String) = Action {
Ok("Hello " + name)
}
// Java controller action method
public static Result index(String name) {
return ok("Hello" + name);
}
![Page 29: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/29.jpg)
Play 2.0 Scala implementaKon
■ Play 2.0 is itself implemented in Scala
■ Scala removes the need for so much bytecode enhancement in the Java API (which is how Play 1.x works)
■ There is less ‘magic’ and strangeness in the API
■ The code you see in the IDE is the code that runs
■ Scala source code not necessarily harder to read
29
![Page 30: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/30.jpg)
“Templates want to be beau8ful tooType safe template parameter delcara8ons
Minimal interference with HTML mark-‐up
30
![Page 31: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/31.jpg)
Template funcKons
■ Play 2.0’s template system is based on Scala
■ A template is a Scala function that you call from your code
31
// Render the ‘Products.list’ template in Java code.
Html html = views.html.Products.list.render(products);
// e.g. as the result of a controller action method.
public static Result list() {
final List<Product> products = Products.list();
return ok(views.html.Products.list.render(products));
}
![Page 32: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/32.jpg)
Type-‐safe template parameters
■ Templates include type-safe parameter declarations
■ Similar to the lightweight template syntax in Play 1.x
■ Templates are compiled into class files for run-time speed
32
@(products: List[Product])
<ul>
@for(product <-‐ products) {
<li>@product.name</li>
}
</ul>
![Page 33: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/33.jpg)
@(products: List[Product])
@if(products.isEmpty) {
<h1>No products</h1>
}
33
Template syntax starts with @
No delimiter for the end of a template syntax section
Template parameter declaration
![Page 34: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/34.jpg)
@(products: List[Product])
@if(products.isEmpty) {
<h1>No products</h1>
} else {
<h1>@items.size products</h1>
}
34
Output the value of an expression
![Page 35: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/35.jpg)
@display(product: models.Product) = { <a href="@routes.Products.details(product.id)"> @product.name </a>}
<ul>@for(product <-‐ products) { @display(product)} </ul>
35
Define a ‘tag’ - output a details page link
Use the ‘display’ tag
![Page 36: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/36.jpg)
<section class="content">
…
</section>
@footer()
36
Include the ‘footer’ template(i.e. call the ‘footer’ function)
![Page 37: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/37.jpg)
@(title: String)(content: Html)<!DOCTYPE html><html> <head> <title>@title</title> </head> <body> @content </body></html>
37
index.scala.html
@main("Home page") { <h1>Welcome</h1>}
main.scala.html
Define a page layouttemplate called ‘main’
Two parameter lists(one parameter each)
Render the ‘main’ template, passing two parameters
![Page 38: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/38.jpg)
Compile-‐Kme checking
■ Not just Java and Scala classes
■ HTTP routes file (maps URLs to controller actions)
■ Templates
■ JavaScript files (using Google Closure Compiler)
■ CoffeeScript files (alternative to JavaScript)
■ LESS style sheets (alternative to CSS)
■ Fewer errors at run-time
38
![Page 39: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/39.jpg)
public class Products extends Controller {
public static Result details(final Long id) { return ok(); }}
39controllers/Products.java
conf/routes
GET /product/:id controllers.Products.details(id: String)
![Page 40: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/40.jpg)
public class Products extends Controller {
public static Result details(final Long id) { return ok(); }}
39controllers/Products.java
conf/routes
GET /product/:id controllers.Products.details(id: String)
Incompatible types
![Page 41: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/41.jpg)
public class Products extends Controller {
public static Result details(final Long id) { return ok(); }}
39controllers/Products.java
conf/routes
GET /product/:id controllers.Products.details(id: String)
Incompatible types
![Page 42: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/42.jpg)
![Page 43: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/43.jpg)
hHp://www.flickr.com/photos/jameswragg/4688532009/
![Page 44: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/44.jpg)
![Page 45: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/45.jpg)
![Page 46: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/46.jpg)
![Page 47: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/47.jpg)
@rainbow: -‐webkit-‐gradient(linear, left top, left bottom, color-‐stop(0.00, red), color-‐stop(20%, orange), color-‐stop(25%, yellow), color-‐stop(30%, yellow), color-‐stop(45%, green), color-‐stop(65%, blue), color-‐stop(80%, indigo), color-‐stop(1.00, violet));
![Page 48: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/48.jpg)
“Use HTML5 technologiesChoose a web framework that lets you
46
![Page 49: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/49.jpg)
Modern web development
■ Play is designed to work with HTML5 technologies
■ No constraints on HTML output (front-end dev-friendly)
■ UI components belong in the client, e.g. JQuery UI
■ Built-in support for improvements to CSS and JavaScript:
■ LESS http://lesscss.org/
■ CoffeeScript http://coffeescript.org/
■ Closure Compiler http://code.google.com/closure/compiler
47
![Page 50: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/50.jpg)
Asynchronous HTTP programming model
■ The future of web programming is asynchronous:
■ Perform simultaneous web service requests
■ Process streams, instead of filling up memory or disk
■ Publish real-time data
■ Predictable and minimal resource consumption
■ Play’s internal architecture uses a reactive model based on Iteratee IO (read about ‘Iteratees’ in the Play 2.0 docs)
48
![Page 51: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/51.jpg)
Other Play 2.0 features
■ Build environment based on sbt
■ Scala REPL (irb eat your heart out)
■ Asynchronous controller API (web sockets)
■ Designed for easy cloud deployment, e.g. Heroku
■ Persistence - use your favourite framework
■ Ebean (Java) or Anorm (Scala) included with Play
■ Test framework integration
49
![Page 52: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/52.jpg)
$ play [info] Loading project definition from /demo/project[info] Set current project to demo (in build file:/Users/pedro/Documents/code/examples-‐play/demo/) _ _ _ __ | | __ _ _ _| || '_ \| |/ _' | || |_|| __/|_|\____|\__ (_)|_| |__/ play! 2.0.1, http://www.playframework.org
> Type "help" or "license" for more information.> Type "exit" or use Ctrl+D to leave this console.
[demo-‐app] $
![Page 53: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/53.jpg)
[jfokus-‐app] $ console[info] Starting scala interpreter...[info] Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-‐Bit Server VM, Java 1.6.0_31).Type in expressions to have them evaluated.Type :help for more information.
scala>
![Page 54: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/54.jpg)
scala> views.html.rainbow.renderres0: play.api.templates.Html =
<!DOCTYPE html><html> <head> <title>rainbow</title> <link rel="stylesheet" type="text/css" href="/assets/colours.css"> </head> <body> <div class="nyan"> <div class="trail"></div> <div class="cat">insert meme</div> </div>
</body></html>
![Page 55: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/55.jpg)
Play 2 books
■ Play 2 for Scala,Peter Hilton, Erik Bakker, Francisco Canedohttp://bit.ly/playforscala
■ Play 2 for Java, Nicolas Leroux, Sietse de Kaper http://bit.ly/playjava
■ Early Access (MEAP) editions now available
53
![Page 56: Play%framework2s3-eu-west-1.amazonaws.com/presentations2012/1_presentation.pdf · Outline What Play is and why it matters High-productivity web development, but on the JVM Developer](https://reader034.fdocuments.us/reader034/viewer/2022050203/5f56ac99b308aa03f06290c2/html5/thumbnails/56.jpg)
@PeterHilton
www.lunatech.com
@PlayFramework
www.playframework.org