Play Framework implements a complete APP (9), frameworkapp
Add, delete, modify, and query operations
1. Enable CRUD Module
In/Conf/application. confAdd
# Import the crud modulemodule.crud=${play.path}/modules/crud
In/Conf/routesAdd
# Import CRUD routes* /admin module:crud
Restart the Server and import the CRUD Module.
2. Add a controller
/App/controllers
import play.*;import play.mvc.*; public class Posts extends CRUD { }public class Tags extends CRUD { }public class Users extends CRUD { }public class Comments extends CRUD { }
An error may be prompted:CRUD cannot be parsed as a TypeAn error is prompted when running the program.
Solution:
ModifyConf/dependencies. yml
require: - play - play -> crud
Run Shell
> play dependencies
The modules/crud file is generated in the project, and the project can be restarted. However, compilation still fails, possibly because the crud project is not referenced.
3. Create a Controller
package controllers;import models.User;@CRUD.For(User.class)public class AdminUsers extends CRUD {}
4. Modify the Model and add Verification
Take User as an Example
public class User extends Model { @Email @Required public String email; @Required @Password public String password; public String fullname; public String isAdmin; public String toString() { return email; }}
Go to http: // localhost: 9000/admin/and select add user to enter the User Form for testing.
public class Post extends Model { @Required public String title; @Required public Date postedAt; @Lob @Required @MaxSize(10000) public String content; @Required @ManyToOne public User author; @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) public List<Comment> comments; @ManyToMany(cascade = CascadeType.PERSIST) public Set<Tag> tags;}
public class Tag extends Model implements Comparable<Tag> { @Required public String name;}
public class Comment extends Model { @Required public String author; @Required public Date postedAt; @Lob @Required @MaxSize(10000) public String content; @ManyToOne @Required public Post post;}
5. The Label name displayed on the page is in lower case, which is the same as the field name of the corresponding class. to display the Label name in upper case, you can modify it./Conf/messages
title=Titlecontent=ContentpostedAt=Posted atauthor=Authorpost=Related posttags=Tags setname=Common nameemail=Emailpassword=Passwordfullname=Full nameisAdmin=User is admin
..