Happy Coding with Vinay Kumar in techartifact…
Latest tip and information on Java and Oracle Fusion Middleware/Weblogic
What is play-Play! is very different compared to your average Java web application. Play! doesn’t follow many of the J2EE conventions or standards. Instead the developers of Play! decided to make the most efficient web framework possible. It’s not a full Java Enterprise stack and Play! expressly states that it is built for web developers. This humble developer started out very skeptical that it would be useful and some of the design choices made little sense to my “Java Enterprise” mindset.
So what is Play!? – let’s take a look at what it offers at a glance.
Completely stateless – matching the HTTP standard
Non-blocking IO – reactive, fast
Hot-reload of Java classes – more on this later
Compiled views – which can be tested
Symmetrical simple routes – created with purpose and design
Both Scala and Java – Built with Scala but Java is a first class alternative
Large third-party module repository – growing daily
Commercial Support – by TypeSafe and Zenexity
Download and install the Play binary package from http://www.playframework.com/download .
To check if play is working or not, open the command prompt and type ‘play help’.
C:\>play help
you should get below screen.
Creting a new Application
step 1 : C:\>play new myFirstApp
step 2 : Confirm the application name.Press enter.
Step 3: type 2 for creating a simple java application.
Creating a working eclipse project from a play application
step 1 : cd to the new application directory
C:\>cd myFirstApp
step 2 : play
C:\myFirstApp>play
step 3 : eclipsify
[myFirstApp] $ eclipsify
Import the project in eclipse.
The app/ directory contains executables. Below are 3 packages
app/controllers
app/models
app/views
The public/ directory contains static assets. Has 3 subdirectories
images
css
javascript
The conf/ directory contains applications configuration files.There are 2 configuration files.
application.conf – standard configuration parameters.
routes – routes defination file
The lib/ directory – libraries/jar files
The project/ directory – sbt build definitions
Plugins.sbt
Build.scala – application build script
The target/ directory – contains things generated by build system
Typical .gitignore file – Generated folders should be ignored by your version control system
logs
project/project
project/target
target
tmp
To run the newly created application
[myFirstApp] $ run
On the browser address type
localhost:9000
The below screen gets displayed.
Modify the source code as below.
Application.java
package controllers; import model.Task; import play.*; import play.data.Form; import play.mvc.*; import views.html.*; import views.*; public class Application extends Controller { public static Result index() { Form<Task> form = form(Task.class); return ok(index.render(form)); } public static Result sayHello() { Form<Task> taskForm = form(Task.class).bindFromRequest(); if(taskForm.hasErrors()) return badRequest(index.render(taskForm)); else { Task data = taskForm.get(); return ok(sayHello.render(data.name, data.age)); } } }
Task.java
package model; import java.util.*; import play.data.validation.Constraints.*; public class Task { @Required public String name; @Required public String age; }
Index.scala @(form1: Form[model.Task]) @import helper._ @main(title = "The 'helloworld' application") { <h1>Configure your 'Hello world':</h1> @form(action = routes.Application.sayHello, args = 'id -> "helloform") { @inputText( field = form1("name"), args = '_label -> "What's your name?" ) @inputText( field = form1("age"), args = '_label -> "What's your age?", 'size -> 3 ) <p class="buttons"> <input type="submit"> <p> } } [/sourcecode ] You can write the code for creating form in HTML also if not in Scala. <form action= @routes.Application.sayHello method=GET > <table> <tr> <td>Name :</td> <td><input type="text" name="name" required="required" > </td></tr> <tr><td> Age :</td><td><input type="text" name="age" > </td></tr> <tr><td><input type="submit" value="Submit" /></td></tr> </table> main.scala @(title: String)(content: Html) <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>@title</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> @content </body> </html>
sayHello.scala
@(name: String, age: String) @main("Details are : "){ <h1>Hello @name!</h1> <h1>Your age is @age</h1> <a href="@{routes.Application.index()}">Back to form</a> } </body> </html>
routes.conf
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / controllers.Application.index()
GET /sayHello controllers.Application.sayHello()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path=”/public”, file)
Refresh the browser.
You will see the below screen.
Test your application.
Happy coding with Techartifact