Vous êtes sur la page 1sur 38

Play!

framework in practice
Nicolas Leroux Lunatech Research

dinsdag 17 november 2009

What is this play! framework exactly?

Today, we will see how the play! framework can change the way java web developers work
www.devoxx.com dinsdag 17 november 2009

Agenda
About the framework Web developers != Enterprise app developers? Looking for a simple web framework for web developers Why Play! framework is the solution Build a simple application and learn about the play! framework
www.devoxx.com dinsdag 17 november 2009

Speakers qualications
Nicolas Leroux is an application developer/ architect
Works for Lunatech Research since 2001 on enterprise applications Co-founder of the Benelux JBUG Co-founder of the Riviera JUG

dinsdag 17 november 2009

www.devoxx.com

Are you a web developer?


Enterprise application developers tend to not claim they 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
www.devoxx.com dinsdag 17 november 2009

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.
www.devoxx.com dinsdag 17 november 2009

The Play framework makes it easier to build Web applications


web application is an application that is accessed via a web browser over a network such as the Internet or an intranet. What do you mean? Building web applications is easy enough in Java But I thought web applications were supposed to be desktop applications in a browser

dinsdag 17 november 2009

www.devoxx.com

Build web application should be a simple task


Clearly this is not the case in the Java ecosystem as of today Play! philosophy: simple things should stay simple Play! is an MVC framework And still gives you the nice feature that Java offers (strongly typed language, etc...)
www.devoxx.com dinsdag 17 november 2009

Play! framework in numbers


Created by Guillaume Bort in 2007 for use by Zenexity Open-sourced in 2008 Version 1.0 in October 2009 Active mailing list (~20 messages per day) Excellent documentation (~7000 lines)
www.devoxx.com dinsdag 17 november 2009

Play! is completely stateless like the web


The session lives in a signed cookie Easy enough to see what the application is doing Scale without limit It adapts to the web standard and not the opposite RESTful services become easy to develop
www.devoxx.com dinsdag 17 november 2009

Java les are rst class citizens


Like PHP, ruby, python, the source classes are what you are developing with. There is no compilation or packaging steps in the middle No WAR le, how is that possible? Increase development speed dramatically Error reporting that means something (well most of the time)

dinsdag 17 november 2009

www.devoxx.com

Traditional JEE framework

dinsdag 17 november 2009

www.devoxx.com

Traditional web framework

Traditional JEE framework

dinsdag 17 november 2009

www.devoxx.com

The play! way

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 mapping Ability to do test driven development Plugins Modules Web services

dinsdag 17 november 2009

www.devoxx.com

What can I do with it out of the box?


Security module Automatic binding CRUD Validation Asynchronous jobs Internationalization Fixtures

dinsdag 17 november 2009

www.devoxx.com

What can I do with it out of the box?


RESTFul services Extended JPA Unit tests Emails OpenID ...
www.devoxx.com dinsdag 17 november 2009

Play! scopes
Application scope Session scope (lives in a signed cookie) Flash scope Request scope For anything else, there is the distributed cache and it is up to you to manage it
www.devoxx.com dinsdag 17 november 2009

But the most important feature


Its FUN to work again!

dinsdag 17 november 2009

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 existing libraries out of the box Play! works very well in a SOA environment Play! has already been deployed within large organisations

dinsdag 17 november 2009

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 week I propose that we see the basics of play! and then jump into the practice
www.devoxx.com dinsdag 17 november 2009

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 item Vote for an item Display the top voted items Search for items
www.devoxx.com dinsdag 17 november 2009

Swag app - data model


We have 3 entities: Item, Picture and Comments
Item 1 1 Picture

1 N

Comment

dinsdag 17 november 2009

www.devoxx.com

Swag app - data model


The item class:
@Entity public class Item extends Model { public public public public public public Float rating; String name; String description; String author; Integer numberOfRating; Date createdAt;

@OneToOne(mappedBy="item", cascade=CascadeType.ALL) public Picture picture; @OneToMany(mappedBy="item", cascade=CascadeType.ALL) public List<Comment> comments = new ArrayList<Comment>(); 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

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 nal eld of a class is seen as a property.
www.devoxx.com

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

dinsdag 17 november 2009

Swag app - views


<form action="@{Application.createItem}" method="POST" enctype="multipart/form-data" > <div> <dl> <dt class="name">Name: </dt> <dd><input name="item.name" type="text"/></dd> <dt class="name">Author:</dt> <dd><input name="item.author" type="text"/></dd> <dt class="name">Description:</dt> <dd><textarea name="item.description" rows="10" cols="40"></textarea></dd> <dt class="name">Picture:</dt> <dd><img id="myimage" style="display:none"/></dd> </dl> </div> <input type="hidden" name="pictureId" value="${pictureId}"/> <input type="submit"/> </form>

dinsdag 17 november 2009

www.devoxx.com

Swag app - controller


# Routes # This file defines all application routes (Higher priority routes first) # ~~~~ # Home page GET /

Application.index

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

GET POST GET

/item/new /item/new /item/new

Application.newItem Application.createItem Application.newItem

dinsdag 17 november 2009

www.devoxx.com

Swag app - controller


public class Application extends Controller { public static void index() { List<Item> topItems = Item.find("order by rating").fetch(); render(topItems); } public static void createItem(Item item, Long pictureId) { Picture pic = Picture.findById(pictureId); item.picture = pic; pic.item = item; item.save(); index(); } public static void newItem() { render("/item/new.html"); }
www.devoxx.com

... }

dinsdag 17 november 2009

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.ndById() Action chaining: automatic redirection for example createItem index()

dinsdag 17 november 2009

www.devoxx.com

Swag app - validation


Add the @Required annotation to the required eld in the model or on the controller Modify the controller to handle the validation error:
if (validation.hasErrors()) { params.flash(); // add http parameters to the flash scope validation.keep(); // keep the errors for the next request render("/item/new.html", pictureId); }
www.devoxx.com dinsdag 17 november 2009

Swag app - validation


Modify the view to display the error messages:
<dd> <input name="item.author" type="text" value="$ {flash['item.author']}" /> <span class="error">#{error 'item.author' /}</span> </dd>

Or
www.devoxx.com

#{errors} <li>${error}</li> #{/errors}

dinsdag 17 november 2009

Swag app - xtures


Allows you to boostrap your application with test data
for unit / functional tests using @Before and Fixtures.load simply to have static data available using @OnApplicationStarts on a job class and Fixtures.load
www.devoxx.com dinsdag 17 november 2009

Swag app - xtures


# 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 ...
www.devoxx.com dinsdag 17 november 2009

Swag app - adding our custom tag


Create a class that extends JavaExtension
public class CustomTags extends JavaExtensions { public static Boolean canRate(Item item) { Http.Cookie cookie = Http.Request.current().cookies.get("item_" + item.id); return (cookie == null); } }

dinsdag 17 november 2009

www.devoxx.com

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

Swag app - unit testing


In the tests directory. The class extends UnitTest.
public class MyTest extends UnitTest { @Before public void setup() { Fixtures.deleteAll(); } @Test public void 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();
www.devoxx.com

// Test assertNotNull(watch); assertEquals("nicolas", watch.author); }

dinsdag 17 november 2009

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 public void testThatIndexPageWorks() { Response response = GET("/"); assertIsOk(response); assertContentType("text/html", response); assertCharset("utf-8", response); }

dinsdag 17 november 2009

www.devoxx.com

DEMO

www.devoxx.com dinsdag 17 november 2009

What is next?
Scala support More modules, more plugins through a module repository Better support for binding objects More users Whatever you need really!
www.devoxx.com dinsdag 17 november 2009

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/
www.devoxx.com dinsdag 17 november 2009

Vous aimerez peut-être aussi