Play Framework implements a complete APP (9), frameworkapp

Source: Internet
Author: User

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

  

 

..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.