注解: CRUD——Create,Retrieve, Update, Delete
文件结构
一、添加控制器(controller和action)——/app/controllers/Products.java
packagecontrollers;importplay.mvc.Controller;importplay.mvc.Result;public class Products extendsController {public static Result list() { //列出所有的产品
returnTODO;
}public static Result newProduct() { //显示一个空的产品Form
returnTODO;
}public static Result details(String ean) { //显示一个编辑产品的Form
returnTODO;
}public static Result save() { //保存产品资料
returnTODO;
}
}
说明: controllers的action方法必须满足以下三个条件:
必须是public
必须是static
返回类型必须是Result和Result的子类
完整代码为
packagecontrollers;importmodels.Product;importplay.data.Form;importplay.mvc.Controller;importplay.mvc.Result;importjava.util.ArrayList;importjava.util.List;import views.html.*;public class Products extendsController {//constant for Form
private static final Form productForm = Form.form(Product.class);public staticResult list(){
List products =Product.findAll();returnok(list.render(products));
}public staticResult newProduct(){returnok(details.render(productForm));
}public staticResult details(String ean){final Product product =Product.findByEan(ean);if(product == null){return notFound(String.format("Product %s does not exist.", ean));
}
Form filledForm =productForm.fill(product);returnok(details.render(filledForm));
}public staticResult save(){
Form boundForm =productForm.bindFromRequest();if(boundForm.hasErrors()){
flash("error","Please correct the form below.");returnbadRequest(details.render(boundForm));
}
Product product=boundForm.get();
product.save();
flash("success",String.format("Successfully added product %s", product));returnredirect(routes.Products.list());
}public staticResult delete(String ean){final Product product =Product.findByEan(ean);if(product == null){return notFound(String.format("Product %s does not exist.", ean));
}
Product.remove(product);returnredirect(routes.Products.list());
}
}
Java Code
二. 修改routes文件
GET /products/controllers.Products.list()
GET/products/newcontrollers.Products.newProduct()
GET/products/:ean controllers.Products.details(ean: String)
POST/products/ controllers.Products.save()
DELETE /products/:ean controllers.Products.delete(ean: String)
action和route对应关系如下图
三 . 添加Model Class——/app/models/Product.java
3.1) 数据模型
public classProduct {publicString ean;publicString name;publicString description;publicProduct(){}publicProduct(String ean, String name, String description){this.ean =ean;this.name =name;this.description =description;
}publicString toString(){return String.format("%s - %s", ean, name, description);
}
}
说明:不同于Java,play使用public,不用setter和getter方法
3.2)为了模拟练习,在Product.java中加一些模拟数据
//mocking some data
private static Listproducts;static{
products= new ArrayList();
products.add(new Product("111111111","Paperclips 1","Paperclips description 1"));
products.add(new Product("222222222","Paperclips 2","Paperclips description 2"));
products.add(new Product("333333333","Paperclips 3","Paperclips description 3"));
products.add(new Product("444444444","Paperclips 1","Paperclips description 4"));
products.add(new Product("555555555","Paperclips 1","Paperclips description 5"));
}
3.3) 数据处理方法——findAll(), findByEan(), findByName(), remove(), save()
public static ListfindAll(){return new ArrayList(products);
}public staticProduct findByEan(String ean){for(Product candidate : products){if(candidate.ean.equals(ean)){returncandidate;
}
}return null;
}public static ListfindByName(String term){final List results = new ArrayList();for(Product candidate : products){if(candidate.name.toLowerCase().contains(term.toLowerCase())){
results.add(candidate);
}
}returnresults;
}public static booleanremove(Product product){returnproducts.remove(product);
}public voidsave(){
products.remove(findByEan(this.ean));
products.add(this);
}
完整代码为
Java Code
四、添加和修改模板文件——list.scala.html, details.scala.html
4.1) list.scala.html
@(products:List[Product]) @main("Product catalogue"){
All Product
EANNameDescription
@for(product@product.ean@product.name@product.description}}
相应的,修改controller用于显示该模板的HTML内容,即修改/app/controllers/Products.java中的list()方法
...import views.html.*; //引入模板
public class Products extendsController {public staticResult list() {
List products =Product.findAll();returnok(list.render(products));
}
...
}
在浏览器地址栏输入: http://localhost:9000/products
4.2) details.scala.html
@(productForm:Form[Product])
@importhelper._
@importhelper.twitterBootstrap._
@main("Product Form"){
Product Form
@helper.form(action=routes.Products.save()){Product(@productForm("name").valueOr("New"))@helper.inputText(productForm("ean"),'_label -> "EAN")
@helper.inputText(productForm("name"),'_label -> "Name")
@helper.textarea(productForm("description"),'_label -> "description")
}
说明: 本模板使用了Play表单Form
@(productForm:Form[Product]) —>— productForm是play.data.Form.form()的方法,在controller中定义
@importhelper._ —>— 引入生产HTML,@importhelper.twitterBootstrap._生产的HTML符合Twitter Bootstrap标准
@helper.form(action =routes.Products.save()){ —>— 生产的 HTML的Form代码, 并将数据提交给controller的save()方法处理,@helper.inputText生产的 HTML的input代码,@helper.textarea生产的 HTML的textarea代码
Product(@productForm("name").valueOr("New")) —>— productForm.field("name")的缩写,请求表单中名为name的域(field)值,如何该值不存在,则创建一个"New"为默认值。
最后生产的HTML代码如下
Product(New)
EAN
Required