play framework in practice - devoxx 2009

Upload: morice-bynton

Post on 05-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    1/38

    dinsdag 17 november 2009

  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    2/38

    www.devoxx.com

    What is this play! framework

    exactly?

    Today, we will see how the play! framework canchange the way java web developers work

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    3/38

    www.devoxx.com

    Agenda

    About the framework

    Web developers != Enterprise app developers?

    Looking for a simple web framework for webdevelopers

    Why Play! framework is the solution

    Build a simple application and learn about the play!

    framework

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    4/38

    www.devoxx.com

    Speakers qualifications

    Nicolas Leroux is an application developer/architect

    Works for Lunatech Research since 2001 on enterprise

    applicationsCo-founder of the Benelux JBUG

    Co-founder of the Riviera JUG

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    5/38

    www.devoxx.com

    Are you a web developer?

    Enterprise application developers tend to not claimthey are web developers

    simplicity != enterprise, enterprises are complex

    and require complex solutions (even if not needed)

    Doing HTML != web developer

    Turns out that now we are all web developers

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    6/38

    www.devoxx.com

    Is there any simple frameworks

    in Java for web developers?JSF? too complex (even with Seam)

    Tapestry?Struts?

    ...

    Grails? yes but it is not Java.

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    7/38

    www.devoxx.com

    web application is an application that is accessedvia a web browser over a network such as the

    Internet or an intranet.What do you mean? Building web applications iseasy enough in Java

    But I thought web applications were supposed tobe desktop applications in a browser

    The Play framework makes it

    easier to build Web applications

    dinsdag 17 november 2009

    http://www.devoxx.com/http://en.wikipedia.org/wiki/Intranethttp://en.wikipedia.org/wiki/Intranethttp://en.wikipedia.org/wiki/Internethttp://en.wikipedia.org/wiki/Internethttp://en.wikipedia.org/wiki/Web_browserhttp://en.wikipedia.org/wiki/Web_browserhttp://en.wikipedia.org/wiki/Application_softwarehttp://en.wikipedia.org/wiki/Application_softwarehttp://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    8/38

    www.devoxx.com

    Build web application should be

    a simple taskClearly this is not the case in the Java ecosystem asof today

    Play! philosophy: simple things should stay simplePlay! is an MVC framework

    And still gives you the nice feature that Java offers

    (strongly typed language, etc...)

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    9/38

    www.devoxx.com

    Play! framework in numbers

    Created by Guillaume Bort in 2007 for use byZenexity

    Open-sourced in 2008Version 1.0 in October 2009

    Active mailing list (~20 messages per day)

    Excellent documentation (~7000 lines)

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    10/38

    www.devoxx.com

    Play! is completely stateless like

    the webThe session lives in a signed cookie

    Easy enough to see what the application is doing

    Scale without limitIt adapts to the web standard and not the opposite

    RESTful services become easy to develop

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    11/38

    www.devoxx.com

    Java files are first class citizens

    Like PHP, ruby, python, the source classes are whatyou are developing with.

    There is no compilation or packaging steps in themiddle

    No WAR file, how is that possible?

    Increase development speed dramaticallyError reporting that means something (well mostof the time)

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    12/38

    www.devoxx.com

    Traditional JEE framework

    Traditional web framework

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    13/38

    www.devoxx.com

    Traditional JEE framework

    The play! way

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    14/38

    www.devoxx.com

    What can I do with it out of the

    box?Support for business tiers (through enhanced JPA)Templates engine based on Groovy

    HTTP-to-code mappingAbility to do test driven development

    Plugins

    ModulesWeb services

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    15/38

    www.devoxx.com

    What can I do with it out of the

    box?Security moduleAutomatic binding

    CRUDValidation

    Asynchronous jobs

    InternationalizationFixtures

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    16/38

    www.devoxx.com

    What can I do with it out of the

    box?RESTFul servicesExtended JPA

    Unit testsEmails

    OpenID

    ...

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    17/38

    www.devoxx.com

    Play! scopes

    Application scope

    Session scope (lives in a signed cookie)

    Flash scopeRequest scope

    For anything else, there is the distributed cache

    and it is up to you to manage it

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    18/38

    www.devoxx.com

    But the most important feature

    Its FUN to work again!

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    19/38

    www.devoxx.com

    Fun != Enterprise?

    It really should be fun, and Play! is bridging the gap

    Play! applications can run on an application server

    Play! is written in Java and supports all the existinglibraries out of the box

    Play! works very well in a SOA environment

    Play! has already been deployed within largeorganisations

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    20/38

    www.devoxx.com

    Its too good to be true

    Lets have a go with it

    Lets build ... a swag application. That should be

    useful enough for this weekI propose that we see the basics of play! and then

    jump into the practice

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    21/38

    www.devoxx.com

    Swag app - requirements

    Should be able to create a new swag item

    Display a nice thumbnail of the item

    Display a nice picture of the itemVote for an item

    Display the top voted items

    Search for items

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    22/38

    www.devoxx.com

    Swag app - data model

    We have 3 entities: Item, Picture and Comments

    Item Picture

    Comment

    1 1

    1

    N

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    23/38

    www.devoxx.com

    Swag app - data model

    The item class: @Entity publicclass Item extends Model {

    public Float rating; public String name; public String description;

    public String author;

    public Integer numberOfRating; public Date createdAt;

    @OneToOne(mappedBy="item", cascade=CascadeType.ALL) public Picture picture;

    @OneToMany(mappedBy="item", cascade=CascadeType.ALL)

    public List comments = new ArrayList();

    public Item() {// left empty}

    public Item(String name, String author) { this.name = name; this.numberOfRating = 0;

    this.author = author; this.rating = 0f; this.createdAt = new Date();

    }}

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    24/38

    www.devoxx.com

    Swag app - data model

    Everything is public?

    The Model portion of the play framework automatically

    generates this pattern while keeping your code clean.Effectively, all public variables become instance properties.

    The convention is that any public, non static, non final field

    of a class is seen as a property.

    See http://www.playframework.org/documentation/1.0/model#properties

    dinsdag 17 november 2009

    http://www.playframework.org/documentation/1.0/model#http://www.playframework.org/documentation/1.0/model#http://www.playframework.org/documentation/1.0/model#http://www.playframework.org/documentation/1.0/model#http://www.playframework.org/documentation/1.0/model#http://www.playframework.org/documentation/1.0/model#http://www.playframework.org/documentation/1.0/model#http://www.playframework.org/documentation/1.0/model#http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    25/38

    www.devoxx.com

    Swag app - views

    Name: Author: Description: Picture:

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    26/38

    www.devoxx.com

    Swag app - controller

    # Routes# This file defines all application routes (Higher priority routes first)# ~~~~

    # Home pageGET / Application.index

    #Map static resources from the /app/public folder to the /public pathGET /public/ staticDir:public

    GET /item/new Application.newItemPOST /item/new Application.createItemGET /item/new Application.newItem

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    27/38

    www.devoxx.com

    Swag app - controller

    publicclass Application extends Controller {

    publicstaticvoid index() {List topItems = Item.find("order by rating").fetch();render(topItems);

    }

    publicstaticvoid createItem(Item item, Long pictureId) {Picture pic = Picture.findById(pictureId);item.picture = pic;pic.item = item;item.save();

    index();

    }

    publicstaticvoid newItem() {

    render("/item/new.html");}

    ...}

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    28/38

    www.devoxx.com

    Swag app - controller

    Every controller method has to be static

    Extends the Controller class

    Provides plenty of useful methods to render Json,xml, etc...

    JPA is much simpler: for example Picture.findById()

    Action chaining: automatic redirection for examplecreateItem index()

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    29/38

  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    30/38

    www.devoxx.com

    Swag app - validation

    Modify the view to display the error messages:

    #{error 'item.author' /}

    Or #{errors}

    ${error}

    #{/errors}

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    31/38

    www.devoxx.com

    Swag app - fixtures

    Allows you to boostrap your application with testdata

    for unit / functional tests using @Before andFixtures.load

    simply to have static data available using@OnApplicationStarts on a job class and Fixtures.load

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    32/38

    www.devoxx.com

    Swag app - fixtures

    # Test data

    Picture(catpicture):

    name: ../public/images/mycat.png

    file: ../public/images/mycat.png

    Item(cat):

    name: a cat

    author: nicolas

    description: a cat

    picture: catpicture

    ...

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    33/38

    www.devoxx.com

    Swag app - adding our customtag

    Create a class that extends JavaExtension publicclass CustomTags extends JavaExtensions {

    publicstatic Boolean canRate(Item item) {Http.Cookie cookie = Http.Request.current().cookies.get("item_" +

    item.id); return (cookie == null);

    }

    }

    In the template: ${item.canRate()}

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    34/38

    www.devoxx.com

    Swag app - unit testing

    In the tests directory. The class extends UnitTest.publicclass MyTest extends UnitTest {

    @Before publicvoid setup() {

    Fixtures.deleteAll();

    }

    @Test publicvoid createAndRetrieveItem() { // Create a new Item and save it new Item("a watch", "nicolas").save();

    // Retrieve the user with bob username

    Item watch = Item.find("byName", "a watch").first();

    // TestassertNotNull(watch);assertEquals("nicolas", watch.author);

    }

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    35/38

    www.devoxx.com

    Swag app - functional testing

    In the tests directory. 2 types of functional tests.

    Selenium via the templating #{selenium}

    // Open the home page, and check that no error occured

    open('/')waitForPageToLoad(1000)

    assertNotTitle('Application error')

    #{/selenium}

    Java class that extends FunctionalTest @Test publicvoid testThatIndexPageWorks() {

    Response response = GET("/");assertIsOk(response);

    assertContentType("text/html", response);assertCharset("utf-8", response);

    }

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    36/38

    www.devoxx.com

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    37/38

    www.devoxx.com

    What is next?

    Scala support

    More modules, more plugins through a module

    repositoryBetter support for binding objects

    More users

    Whatever you need really!

    dinsdag 17 november 2009

    http://www.devoxx.com/http://www.devoxx.com/
  • 7/31/2019 Play Framework in Practice - Devoxx 2009

    38/38

    www.devoxx.com

    Thanks for your attention!

    http://www.playframework.org

    http://groups.google.com/group/play-framework

    http://www.lunatech-research.com

    http://www.zenexity.fr

    http://www.playframework.org/documentation/

    http://www.playframework.org/documentation/http://www.playframework.org/documentation/http://www.zenexity.fr/http://www.zenexity.fr/http://www.lunatech-research.com/http://www.lunatech-research.com/http://www.playframework.org/http://www.playframework.org/http://www.devoxx.com/http://www.devoxx.com/