Created
September 24, 2010 12:20
-
-
Save jto/595263 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package controllers; | |
| import com.google.gson.Gson; | |
| import play.mvc.*; | |
| import play.data.validation.*; | |
| import java.util.*; | |
| import models.*; | |
| import com.thoughtworks.xstream.*; | |
| public class Application extends Controller { | |
| public static void index() { | |
| Date now = new Date(); | |
| render(now); | |
| } | |
| //V1 - Action dédiée + Serialisation avec XStream | |
| /* | |
| public static void listXml(){ | |
| List<Contact> contacts = Contact.list(); | |
| XStream xs = new XStream(); | |
| String xml = xs.toXML(contacts); | |
| renderXml(xml); | |
| } | |
| */ | |
| //v2 - Action dédiée + template xml | |
| /* | |
| public static void listXml(){ | |
| List<Contact> contacts = Contact.list(); | |
| render("Application/listXml.xml", contacts); | |
| } | |
| */ | |
| //v3 - Réutilisation de l'action + utilisation de "format" + JSON | |
| public static void list() { | |
| List<Contact> contacts = Contact.list(); | |
| if(request.format.equals("json")) | |
| renderJSON(contacts); | |
| else | |
| render(contacts); | |
| } | |
| public static void form(Long id) { | |
| if(id == null) { | |
| render(); | |
| } | |
| Contact contact = Contact.findById(id); | |
| render(contact); | |
| } | |
| public static void save(@Valid Contact contact) { | |
| if(validation.hasErrors()) { | |
| if(request.isAjax()) error("Invalid value"); | |
| render("@form", contact); | |
| } | |
| contact.save(); | |
| list(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0"?> | |
| <contacts> | |
| #{list items:contacts, as:'c'} | |
| <contact> | |
| <id>${c.id}</id> | |
| <firstname>${c.firstname}</firstname> | |
| <name>${c.name}</name> | |
| <birthdate>${c.birthdate}</birthdate> | |
| <email>${c.email}</email> | |
| </contact> | |
| #{/list} | |
| </contacts> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Routes | |
| # This file defines all application routes (Higher priority routes first) | |
| # ~~~~ | |
| # Home page | |
| GET / Application.index | |
| GET /contacts Application.list | |
| POST /contacts Application.save | |
| GET /contacts/{id} Application.form | |
| GET /contacts/new Application.form | |
| ################## | |
| ## REST API ## | |
| ################## | |
| # V1 et V2 | |
| #GET /contactsXML Application.listXml | |
| # V3 | |
| GET /contacts.{<json|xml|html>format} Application.list(format:${format}) | |
| # Map static resources from the /app/public folder to the /public path | |
| GET / staticDir:public |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment