Play Framework in JAVA

What is the Play Framework?

Developed by Lightbend, the Play Framework is a powerful, efficient, and intuitive framework that was originally written and coded in Scala but is used extensively in JAVA as well. Play follows the MVC (Model – view – controller) architecture and is used to develop web-based enterprise applications.

Why Choose the Play Framework? (Advantages)

  • Browse Reload – Also commonly known as ‘Hot Code Reloading’, whenever you use the Play Framework and make changes, it will reflect simply when you refresh the browser. You do not need to start the application all over again.
  • As the Framework is open source, the source code is available to all developers. Changes can be made to improve the functionalities of the framework by anyone.
  • The framework is quite accepting and flexible when it comes to adding external plugins pr frameworks to support the application.
  • Debugging – The Play framework has one of the best error debugging systems. Whenever an error occurs, the file, line, and even the column of the code where the error persists is highlighted.
  • The Play Framework is constantly on the hunt to improve its interface and make it easier for users to use. Regular updates are provided on the official website of the Play framework. It is also compatible with new advancements like JSON, ORM, SQL, and NoSQL.

Testing Framework

Play Framework is often used for unit testing and functional testing as it easily integrates with several testing frameworks. Play allows testing its modules separately without having to initiate a server request. You can also start a server if required during testing.

Play Framework Architecture

Play follows a MVC architecture as mentioned before. The workflow starts from a Client browser from where a request is initiated. The Request is then passed on to the Controller via the Play Routings. The Play Controller has a direct link with the Play Services and the Play Repository. The Model is interlinked with the above two components. After the request is registered, the controller consults with the Model where an action is decided upon. Following this, the Model passes the response to the View component that prepares a final presentation to display the response to the client. The display is then sent to the Client Browser as a response to the request.

A Play Application – Example

/* In this example, we will be building a simple application that will print out the words ‘Hello’ and a name that you will provide to the app. If you feed in ‘Jack’, then the output will be ‘Hello Jack’.
//In this class, we will extend the controller methods into our application. Action methods are //fundamentally entry points that the Play app will use.
package controllers;
import play.mvc.*;
public class App extends Controller {
public static void index() {
//The render method is part of the Controller class
render();
}
}
/*Now comes the HTML template file of the program. To focus only on the play features, the HTML file is skipped in this example. You can think of a basic html template that will dictate the font and size of the letters and its positioning. */
//This is the form template (.html) that will ask you to input a name that will be eventually printed //out.
#{extends ‘main.html’ /}
#{set title:’Home’ /}
<form action = “@{Application.sayHello()}” method = “GET”>
<input type = “text” name = “myName” />
<input type = “submit” value = “Say Hello!” />
</form>
//Finally, we will create the JAVA class that will bridge the above .html templates
package controllers;
import play.mcv.*;
public class Application extends Controller {
public static void main index() {
render();
}
public static void sayHello(String myName) {
render (myName);
}
}

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.