Developing web apps using Java and the Play framework

15
developing web applications using Java and the Play framework

description

 

Transcript of Developing web apps using Java and the Play framework

Page 1: Developing web apps using Java and the Play framework

developing web applications using Java and the Play framework

Page 2: Developing web apps using Java and the Play framework

WHAT IS PLAY?• A clean alternative to the bloated Java Enterprise Edition

• Pure Java framework – allows you to keep your preferred development tools and libraries

• Focuses on developer productivity and targets RESTful (Representational State Transfer) architectures

• Consist of clients and servers

• Clients initiate requests to servers

• Servers process requests and return appropriate responses

• Requests and responses are built around the transfer of representations of resources

Page 3: Developing web apps using Java and the Play framework

THE FOUNDATION OF PLAY• MVC architecture

• You’ve got a database on one side and a web browser on the other.

• Classes are models, the application is the controller, generated HTML is the view

• HTTP-to-code mapping

• Example: binding URI patterns to Java procedure calls

• Efficient template engine

• The expression language used is Groovy

• Templating system mainly used to render HTML responses

Page 4: Developing web apps using Java and the Play framework

WHY IS PLAY AWESOME?• Fix the bug and hit reload

• The framework compiles your Java sources directly and hot-reloads them into the JVM without the need to restart the server

• You can then edit, reload and see your modifications immediately, just as in a Rails environment

• Whenever an error occurs, the framework identifies and shows you the problem

• Stack traces are stripped down and optimized to make it easier to solve problems

• Java Persistence API (JPA) and The Java Persistence Query Language (JPQL)

• A persistence entity is a lightweight Java class saved to a table in a relational database.

Page 5: Developing web apps using Java and the Play framework

JPA, JPQL// Java Persistence API example@Entity public class Book {

@Id private Integer id; private String title; private String isbn;

@ManyToOne private Publisher publisher;

@ManyToMany private List<Author> authors;

}

Page 6: Developing web apps using Java and the Play framework

JPA, JPQL// Java Persistence Query Language examplepublic void messages(int page, User connectedUser ) {

List<Message> messages = Message.find(“user = ? and read = false order by date desc", connectedUser).from(page * 10).fetch(10);

render(connectedUser, messages); }

Page 7: Developing web apps using Java and the Play framework

WHY IS PLAY AWESOME?• Test-driven development

• Run them directly in a browser using Selenium

• Selenium is a Firefox add-on that records clicks, typing, and other actions

• Database support through JDBC

• Object-relational mapping using Hibernate (with the JPA API)

• Integrated cache support

• Straightforward web services consumption either in JSON or XML

• OpenID support for distributed authentication

• Ready to be deployed anywhere (application server, Google App Engine, Cloud, etc…)

• Image manipulation API.

Page 8: Developing web apps using Java and the Play framework

THINGS YOU CAN DO WITH PLAY

Page 9: Developing web apps using Java and the Play framework

THINGS YOU CAN DO WITH PLAY• Bind an HTTP parameter to a Java method parameter

// a simple request/articles/archive?date=08/01/08&page=2

// the Java methodpublic static void archive(Date date, Integer page) {

List<Article> articles = Articles.fromArchive(date, page); render(articles);

}

Page 10: Developing web apps using Java and the Play framework

THINGS YOU CAN DO WITH PLAY• Smart binding with any class

// a simple modelpublic class Person {

public String name; public Integer age;

}// a simple controllerpublic static void add(Person p) { p.save(); }

// a simple HTML form<form action="add" method="POST">

Name: <input type="text" name="p.name" /> Age: <input type="text" name="p.age" />

</form>

Page 11: Developing web apps using Java and the Play framework

THINGS YOU CAN DO WITH PLAY• Redirect to an action by calling the corresponding Java method

// a simple model with a procedurepublic static void show(Long id) {

Article article = Article.findById(id); render(article);

}

// use the equivalent syntax:<a href="@{Article.show(article.id)}">

${article.title}</a>

Page 12: Developing web apps using Java and the Play framework

THINGS YOU CAN DO WITH PLAY• Redirect to an action by calling the corresponding Java method (another example)

// a simple model with a procedurepublic static void edit(Long id, String title) {

Article article = Article.findById(id); article.title = title; article.save();

show(id); }

// generated HTML:<a href="/articles/15">

My new article</a>

Page 13: Developing web apps using Java and the Play framework

THINGS YOU CAN DO WITH PLAY• Straightforward file upload management

// the Java codepublic static void uploadPhoto(String title, File photo) {

... }

// a simple HTML form<form action="@{Article.uploadPhoto()}" method="POST"

enctype="multipart/form-data">

<input type="text" name="title" /> <input type="file" id="photo" name="photo" /> <input type="submit" value="Send it..." />

</form>

Page 14: Developing web apps using Java and the Play framework

DEMO

Let’s create our own web application:

a personal agenda

Page 15: Developing web apps using Java and the Play framework

Questions?