Play 2.0
- 4. ● Stateless web framework
● RESTful-friendly
● Asynchronous ability built in
● LESS and CoffeeScript friendly too!
- 13. Using Play from the console
Enter an existing Play application directory and
type 'play'
- 14. Using play in your IDE
http://www.playframework.com/documentation/2.1.0/IDE
- 18. ● Java and Scala source code goes here.
● Can create own packages
- 25. Declared at the top of the file
@(customer: models.Customer, orders: List
[models.Order])
- 30. Ability for server side comments
@*********************
* This is a comment *
*********************@
- 33. HTML form submission data is easy to deal
with using the play.data.* package
Uses Spring data binder to wrap a model
(class)
- 34. To use:
public class User {
public String email;
public String password;
}
Form<User> userForm = form(User.class);
- 35. Now, you can create a User from a
hashmap or request object
Map<String,String> anyData = new HashMap();
anyData.put("email", "bob@gmail.com");
anyData.put("password", "secret");
User user = userForm.bind(anyData).get();
or
User user = userForm.bindFromRequest().get();
- 36. Can add constraints using JSR303
implementation
public class User {
@Required
public String email;
public String password;
public String validate() {
if(authenticate(email,password) == null) {
return "Invalid email or password";
}
return null;
}
}
- 37. Custom handling of errors when
invalid form submissions
if(userForm.hasErrors()) {
return badRequest(form.render(userForm));
} else {
User user = userForm.get();
return ok("Got user " + user);
}
- 40. Can add parameters too
@helper.form(action = routes.Application.submit(),
'id -> "myForm") {
}
- 41. 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"))
}
- 42. Bad news: the markup isn't that pretty.
Good news: you can take control
- 45. By convention, the default database must be
called default.
Other databases can have custom names.
Only the h2 database driver is provided by
default. You'll have to manually configure any
other drivers as an application dependency.
- 46. EBean and Hibernate
Both Ebean and Hibernate are supported
Ebean is natively supported
Hibernate as an application dependency
- 47. Ebean
Enabled in the conf/application.conf file
***Play generates getter/setters to be available
at runtime, not compile time!***
- 49. Add Hibernate as a dependency
val appDependencies = Seq(
"org.hibernate" % "hibernate-entitymanager" %
"3.6.9.Final"
)
- 50. 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/persistence
http://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>
- 51. Must manually denote a transaction
using @Transactional
@Transactional
public static Result index() {
...
}
- 52. Can retrieve current entity manager
using the play.db.jpa.JPA helper
class
public static Company findById(Long id) {
return JPA.em().find(Company.class, id);
}
- 56. 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);
}
}
- 60. sbt build definition
Use := to set a setting
There are a number of default settings.
● Resovers
● Source
● Target
● Hooks for CoffeeScript, LESS, and
JavaScript minification and generation
- 62. Add dependencies in Build.scala
Syntax:
val appDependencies = Seq(
"org.apache.derby" % "derby" % "10.4.1.3"
)
- 64. Uses Maven2 and Scala Tools by default
You can add your own:
resolvers += (
"Local Repository" at "file://"+Path.userHome.
absolutePath+"/.m2/repository"
)
- 66. ● 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.