Developing web apps using Java and the Play framework


Published on

Published in: Technology

Developing web apps using Java and the Play framework

  1. 1. developing web applications using Java and the Play framework<br />
  2. 2. What is Play?<br />A clean alternative to the bloated Java Enterprise Edition<br />Pure Java framework – allows you to keep your preferred development tools and libraries<br />Focuses on developer productivity and targets RESTful(Representational State Transfer) architectures<br />Consist of clients and servers<br />Clients initiate requests to servers<br />Servers process requests and return appropriate responses<br />Requests and responses are built around the transfer of representations of resources<br />
  3. 3. The foundation of Play<br />MVC architecture<br />You’ve got a database on one side and a web browser on the other.<br />Classes are models, the application is the controller, generated HTML is the view<br />HTTP-to-code mapping<br />Example: binding URI patterns to Java procedure calls<br />Efficient template engine<br />The expression language used is Groovy<br />Templating system mainly used to render HTML responses<br />
  4. 4. Why is play awesome?<br />Fix the bug and hit reload<br />The framework compiles your Java sources directly and hot-reloads them into the JVM without the need to restart the server<br />You can then edit, reload and see your modifications immediately, just as in a Rails environment<br />Whenever an error occurs, the framework identifies and shows you the problem<br />Stack traces are stripped down and optimized to make it easier to solve problems<br />Java Persistence API (JPA) and The Java Persistence Query Language (JPQL) <br />A persistence entity is a lightweight Java class saved to a table in a relational database.<br />
  5. 5. JPA, JPQL<br />// Java Persistence API example<br />@Entity <br />public class Book { <br />@Id <br />private Integer id; <br />private String title; <br />private String isbn; <br /> @ManyToOne<br /> private Publisher publisher; <br /> @ManyToMany<br />private List<Author> authors; <br />}<br />
  6. 6. JPA, JPQL<br />// Java Persistence Query Language example<br />public void messages(intpage, User connectedUser) {<br />List<Message> messages = Message.find(“<br /> user = ? <br /> and read = false <br /> order by date desc", <br />connectedUser).from(page * 10).fetch(10); <br />render(connectedUser, messages); <br />}<br />
  7. 7. Why is play awesome?<br />Test-driven development<br />Run them directly in a browser using Selenium<br />Selenium is a Firefox add-on that records clicks, typing, and other actions<br />Database support through JDBC<br />Object-relational mapping using Hibernate (with the JPA API)<br />Integrated cache support<br />Straightforward web services consumption either in JSON or XML<br />OpenIDsupport for distributed authentication<br />Ready to be deployed anywhere (application server, Google App Engine, Cloud, etc…)<br />Image manipulation API.<br />
  8. 8. Things you can do with play<br />
  9. 9. Things you can do with play<br />Bind an HTTP parameter to a Java method parameter<br />// a simple request<br />/articles/archive?date=08/01/08&page=2<br />// the Java method<br />public staticvoid archive(Date date, Integer page) { <br />List<Article> articles = Articles.fromArchive(date, page); render(articles); <br />}<br />
  10. 10. Things you can do with play<br />Smart binding with any class<br />// a simple model<br />public class Person { <br />public String name; <br />public Integer age; <br />}<br />// a simple controller<br />public static void add(Person p) {; }<br />// a simple HTML form<br /><form action="add" method="POST"><br /> Name: <input type="text" name="" /> <br /> Age: <input type="text" name="p.age" /> <br /></form><br />
  11. 11. Things you can do with play<br />Redirect to an action by calling the corresponding Java method<br />// a simple model with a procedure<br />public static void show(Long id) { <br />Article article = Article.findById(id); <br />render(article); <br />}<br />// use the equivalent syntax:<br /><a href="@{}"><br />${article.title}<br /></a><br />
  12. 12. Things you can do with play<br />Redirect to an action by calling the corresponding Java method (another example)<br />// a simple model with a procedure<br />public static void edit(Long id, String title) { <br />Article article = Article.findById(id); <br />article.title= title; <br />;<br />show(id); <br />}<br />// generated HTML:<br /><a href="/articles/15"><br />My new article<br /></a><br />
  13. 13. Things you can do with play<br />Straightforward file upload management<br />// the Java code<br />public static void uploadPhoto(String title, File photo) { <br /> ... <br />}<br />// a simple HTML form<br /><form action="@{Article.uploadPhoto()}" method="POST" enctype="multipart/form-data"> <br /> <input type="text" name="title" /> <br /> <input type="file" id="photo" name="photo" /> <br /> <input type="submit" value="Send it..." /><br /></form><br />
  14. 14. DEMO<br />Let’s create our own web application: <br />a personal agenda<br />
  15. 15. Questions?<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.