Step 1: Installation
1. Download play framework 1.2.4 http://download.playframework.org/releases/play-1.2.4.zip and extract it to a folder of your preference.
2. Download Java http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html if you don’t have it.
3. Extract it and add the play framework and java/bin to your “PATH” environment variable (Click here for a demonstration)
4. Start -> Run -> cmd <press enter>, and type “java -version” to see if it works properly, and also test “play” as well.
5. Download Eclipse IDE for Java Developers and extract it to a folder of your choosing.
Step 2: Setting up application base
1. Via command line, go type “cd Desktop” (optional)
2. Type “play new jsonplay” to create a new play project named jsonplay in the folder jsonplay (if this did not work, your path is not setup correctly, repeat installation step 3)
3. jsonplay project should show up on your desktop, and you can now also start it up, so “cd jsonplay”
4. Type “play run” and point your browser to http://localhost:9000 and you should see a nice welcome screen.
5. Press CTRL+C to stop the server in your command prompt.
6. While in the same directory as your project, type “play eclipsify” and open eclipse.
7. In eclipse, press File -> Import -> Existing Projects into Workspace, and place the root directory to your Desktop/jsonplay folder and press finish (leave everything else untouched)
Step 3: Lets write our application
1. Expand the project jsonplay then app and right click models -> new -> class, name it Category (leave everything else default)
2. Create one more class right click models -> new -> class, name it Product (leave everything else default)
3. In Product class, add “public String name;” and “public Integer price;”
4. In Category add three members to the class, “public String name;” and “public Integer type;” and “public ArrayList<Product> products;” and press CTRL+SHIFT+O to import any missing classes.
should look like so
Category.java
package models;
import java.util.ArrayList;
public class Category {
public String name;
public Integer type;
public ArrayList<Product> products;
}
Product.java
package models;
public class Product {
public String name;
public Integer price;
}
5. Right click controllers, and add a new class named Manager, that has the superclass of “play.mvc.Controller” leave everything else default, and click Finish.
Replace contents with this
package controllers;
import java.util.ArrayList;
import models.Category;
import models.Product;
import play.mvc.Controller;
public class Manager extends Controller {
public static void productList() {
Product product1 = new Product();
product1.name = “Advil”;
product1.price = 5;
Product product2 = new Product();
product2.name = “Tylenol”;
product2.price = 6;
Category category1 = new Category();
category1.name = “Pharmacy”;
category1.type = 1;
category1.products = new ArrayList<Product>();
category1.products.add(product1);
category1.products.add(product2);
Category category2 = new Category();
category2.name = “Medicine”;
category2.type = 1;
category2.products = new ArrayList<Product>();
category2.products.add(product1);
category2.products.add(product2);
ArrayList<Category> categories = new ArrayList<Category>();
categories.add(category1);
categories.add(category2);
renderJSON(categories);
}
public static void echoVariables(String var1, String var2) {
String[] words = {var1, var2};
renderJSON(words);
}
}
6. Go back to your command prompt, and type “play run”
7. Point your browser to http://localhost:9000/manager/productList it should display a json array. (where manager => Manager.java, and productList => your productList function)
and http://localhost:9000/manager/echoVariables?var1=myVariable&var2=mySecondVariable
Video of Step 2 & Step 3: http://www.youtube.com/watch?v=0rXAXtmGDvs&hd=1
Download project: jsonplay.zip

