Tech News

Play with Java and Scala

Java EE continues to evolve in a, sort of, benign continuum. Inspired by frameworks such as Spring, Java EE introduced features such as annotations and dependency injection to address complex tasks like transactions and database connectivity.

Java EE 7, the latest edition as of this writing, further reinforces the advancements in the Java Persistence API (JPA) and JAX-RS for REST-based web services, to name just a few. Most of the complexity involved with Java web development will continue to be addressed, in many ways, by commercial or open web frameworks’ innovations.

However, the Play 2 web framework meanders off the beaten track. Play 2 is not Java EE–centric and is not constrained by Java EE. It is part of the Typesafe stack that provides an alternative to the Java EE stack. Typesafe redefines the layers of modern Java applications formerly defined by Java EE, and in this new demarcation, Play constitutes the web layer. Play is an open-source web application framework, written in Scala and Java, and provides out-of-the-box support for the modern Web.

Play was built for the needs of modern web and mobile applications, leveraging technologies such as REST, JSON, and Web Sockets, among others. Play targets the JVM and focuses on enhancing developer productivity by providing features such as convention over configuration, hot code reloading, and errors in the browser.

Play implements the MVC architecture by means of a route file that maps HTTP requests to controllers and view templates that represent the result. Play 2 builds upon the functional paradigm by providing native support for the Scala programming language and provides an adapted Java-specific API that formulates a highly reactive web framework.

Router

The main entry point of the web application is the conf/routes file that defines the routes needed by the application. Each route consists of an HTTP method and a URI pattern associated with a call to action method. Conf/routes is the configuration file used by the built-in component called Router that translates each incoming HTTP request to an active call.

Controller

n a Java EE–based web application, a controller is a Java class that extends the servlet type. Since Play is not Java EE–centric, a controller in Play 2 is a class in Java or an object in Scala that extends the controller type (both in Java and Scala). This controller type is provided in the play.API.mvc package. A controller in Play 2 comprises a public, a static method called an action. An action is basically a method that processes the request parameters and produces a result to be sent to the client. The controller responds to requests, processes them, and invokes changes to the model.

Model

The model is the domain-specific representation of the information on which the application operates. The most commonly used object for this representation is the JavaBean. However, JavaBeans lead to plenty of boilerplate code. Play 2, like Grails, reduces this boilerplate code by generating the getters and setters for you by means of byte-code enhancement. The model objects may contain persistence artifacts such as JPA annotations, for instance, if they need to be saved into persistent storage.

View

In a Java EE–based web application, the view is usually developed using JSP. That is, the view in a Java EE–based web application consists of JSP elements and template text. As Play is not Java EE–centric, the view in Play comprises the template that contains a mix of HTML and Scala code. In-Play 1, the templates were based on Groovy, but starting with Play 2, the templates are based on Scala. Using Play 2 you can develop both Java- and Scala-based web applications, and the templates are the same in both of them.

Summary

You developed a hello world web application for both Java and Scala, and you learned the basics that are common to all the Play 2 web applications: Java and Scala controllers, actions, and even a bit of view. In addition, you examined the differences between Java and Scala controllers. You saw the best features provided by Play 2, such as compilation on the fly and errors shown on the browser. Then you developed a simple Java-based CRUD web application.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button