• Save
Play 2.0
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,007
On Slideshare
1,007
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Play Framework2.1
  • 2. OverviewDifferences from 1.0
  • 3. Overview
  • 4. ● Stateless web framework● RESTful-friendly● Asynchronous ability built in● LESS and CoffeeScript friendly too!
  • 5. Differences from 1.0
  • 6. Template engine1.0 2.0Groovy Pages Scala Templates
  • 7. Persistence1.0 2.0Hibernate Ebean and Hibernate
  • 8. Language Support1.0 2.0Java Java and Scala
  • 9. Dynamic compilation1.0 2.0Byte code injection Dynamic compilation via SBT
  • 10. And more...Reports on errors in JavaScriptDesigned for concurrent, long connectionsCRSF protection
  • 11. ModulesjdbcanormjavaCorejavaJdbcjavaEbeanjavaJpafilters
  • 12. Setup
  • 13. Using Play from the consoleEnter an existing Play application directory andtype play
  • 14. Using play in your IDEhttp://www.playframework.com/documentation/2.1.0/IDE
  • 15. Creating an applicationplay new your_app_name
  • 16. Example Time
  • 17. ● Java and Scala source code goes here.● Can create own packages
  • 18. Lets create a Play application...
  • 19. Templates
  • 20. Declared at the top of the file@(customer: models.Customer, orders: List[models.Order])
  • 21. Are iterable...<ul>@for(p <- products) { <li>@p.getName() ($@p.getPrice())</li>}</ul>
  • 22. Are conditional...@if(items.isEmpty()) { <h1>Nothing to display</h1>} else { <h1>@items.size() items!</h1>}
  • 23. Can be reusable...@display(product: models.Product) = { @product.getName() ($@product.getPrice())}<ul>@for(product <- products) { @display(product)}</ul>
  • 24. or@title(text: String) = @{ text.split( ).map(_.capitalize).mkString(" ")}<h1>@title("hello world")</h1>
  • 25. Ability for server side comments@********************** This is a comment **********************@
  • 26. Back to the example...
  • 27. HTML Forms
  • 28. HTML form submission data is easy to dealwith using the play.data.* packageUses Spring data binder to wrap a model(class)
  • 29. To use:public class User { public String email; public String password;}Form<User> userForm = form(User.class);
  • 30. Now, you can create a User from ahashmap or request objectMap<String,String> anyData = new HashMap();anyData.put("email", "bob@gmail.com");anyData.put("password", "secret");User user = userForm.bind(anyData).get();orUser user = userForm.bindFromRequest().get();
  • 31. Can add constraints using JSR303implementationpublic class User { @Required public String email; public String password; public String validate() { if(authenticate(email,password) == null) { return "Invalid email or password"; } return null; }}
  • 32. Custom handling of errors wheninvalid form submissionsif(userForm.hasErrors()) { return badRequest(form.render(userForm));} else { User user = userForm.get(); return ok("Got user " + user);}
  • 33. FormHelpers
  • 34. HTML Form creation is simple@helper.form(action = routes.Application.submit()) {}
  • 35. Can add parameters too@helper.form(action = routes.Application.submit(),id -> "myForm") {}
  • 36. There are special FormHelpers@(myForm: Form[User])@helper.form(action = routes.Application.submit()) { @helper.inputText(myForm("username"), id -> "username", size-> 30) @helper.inputPassword(myForm("password"))}
  • 37. Bad news: the markup isnt that pretty.Good news: you can take control
  • 38. Back to the example...
  • 39. Databases
  • 40. By convention, the default database must becalled default.Other databases can have custom names.Only the h2 database driver is provided bydefault. Youll have to manually configure anyother drivers as an application dependency.
  • 41. EBean and HibernateBoth Ebean and Hibernate are supportedEbean is natively supportedHibernate as an application dependency
  • 42. EbeanEnabled in the conf/application.conf file***Play generates getter/setters to be availableat runtime, not compile time!***
  • 43. Hibernate
  • 44. Add Hibernate as a dependencyval appDependencies = Seq( "org.hibernate" % "hibernate-entitymanager" %"3.6.9.Final")
  • 45. Create a persistence.xml in conf/META-INF<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <non-jta-data-source>DefaultDS</non-jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> </properties> </persistence-unit></persistence>
  • 46. Must manually denote a transactionusing @Transactional@Transactionalpublic static Result index() { ...}
  • 47. Can retrieve current entity managerusing the play.db.jpa.JPA helperclasspublic static Company findById(Long id) { return JPA.em().find(Company.class, id);}
  • 48. Working with JSON
  • 49. JSON RequestContent-Type must specify text/json orapplication/json MIME type
  • 50. import org.codehaus.jackson.JsonNode;import play.mvc.BodyParser;...@BodyParser.Of(BodyParser.Json.class)public static Result sayHello() { JsonNode json = request().body().asJson(); String name = json.findPath("name").getTextValue(); if(name == null) { return badRequest("Missing parameter [name]"); } else { return ok("Hello " + name); }}
  • 51. Returning a result of JSON is easy too.import play.libs.Json;import org.codehaus.jackson.node.ObjectNode;...@BodyParser.Of(BodyParser.Json.class)public static Result sayHello() { JsonNode json = request().body().asJson(); ObjectNode result = Json.newObject(); String name = json.findPath("name").getTextValue(); if(name == null) { result.put("status", "KO"); result.put("message", "Missing parameter [name]"); return badRequest(result); } else { result.put("status", "OK"); result.put("message", "Hello " + name); return ok(result); }}
  • 52. Handling XML requests and responses is easytoo.
  • 53. build/deploy
  • 54. sbtScala DSLUses build definitionAccurate incremental recompilationNot all sbt features are included in Play
  • 55. sbt build definitionUse := to set a settingThere are a number of default settings.● Resovers● Source● Target● Hooks for CoffeeScript, LESS, and JavaScript minification and generation
  • 56. Dependency Management with Ivyvia sbt
  • 57. Add dependencies in Build.scalaSyntax:val appDependencies = Seq( "org.apache.derby" % "derby" % "10.4.1.3")
  • 58. Resolvers
  • 59. Uses Maven2 and Scala Tools by defaultYou can add your own:resolvers += ( "Local Repository" at "file://"+Path.userHome.absolutePath+"/.m2/repository")
  • 60. Authentication and Authorization
  • 61. ● Play has its own authorization and authentication● No JAAS compatibility● Only basic support, may not be enough for enterprise apps● There are a number of third party modules that may provide the auth support you need.
  • 62. LinksAPI: http://www.playframework.com/documentation/api/2.1.0/java/index.htmlJava documentation: http://www.playframework.com/documentation/2.1.0/Home
  • 63. Thank you