Play framework is joining the Typesafe Stack — More information in the official announcement
Manual, tutorials & references
Get help with google
The CRUD (Create, Read, Update, Delete) module generates a fully usable web interface for your JPA Model objects.
Let’s see a simple example.
In the /conf/application.conf file, enable the CRUD module by adding this line:
# The crud module
module.crud=${play.path}/modules/crud
In the conf/routes file, import the default module routes by adding this line:
# Import CRUD routes
* /admin module:crud
Note that using the default routes file is not required. You can also define your own routes, or mix the two.
We will start by creating a User class:
package models;
import play.*;
import play.db.jpa.*;
import javax.persistence.*;
import java.util.*;
@Entity
public class User extends Model {
public String name;
public String email;
public String address;
}
Then, we create a simple controller that just inherits the CRUD controller.
package controllers;
public class Users extends CRUD {
}
Now open http://localhost:9000/admin and you should see the User admin area.
The controller’s class name has to be the model class name with a final ‘s’. If you want to name it differently, you can do it using an annotation.
package controllers;
@CRUD.For(User.class)
public class AdminUsers extends CRUD {
}
Click the Add button, and you should see the User form.
Now we can add some validation rules to the User class:
package models;
import play.*;
import play.db.jpa.*;
import javax.persistence.*;
import java.util.*;
import play.data.validation.*;
@Entity
public class User extends Model {
@Required
@MinSize(8)
public String name;
@Required
@Email
public String email;
@Required
@MaxSize(1000)
public String address;
public String toString() {
return email;
}
}
Refresh the User form and you will see that the validation is automatically applied.
Add these lines to the conf/messages file in your application:
name=Name
email=Email address
address=Postal address
And refresh the User form.
The default list view uses only one column containing the result of the object’s toString() method.
To customize this view, we need to create the /app/views/Users/list.html template in the application.
Open a shell, go the application directory and type:
play crud:ov --template Users/list
This will copy the default CRUD list.html template to the Users/list.html template in your application, overwriting it if present.
Edit the template like this:
#{extends 'CRUD/layout.html' /}
<div id="crudList" class="${type.name}">
<h2 id="crudListTitle">&{'crud.list.title', type.name}</h2>
<div id="crudListSearch">
#{crud.search /}
</div>
<div id="crudListTable">
#{crud.table fields:['email', 'name'] /}
</div>
<div id="crudListPagination">
#{crud.pagination /}
</div>
<p id="crudListAdd">
<a href="@{blank()}">&{'crud.add', type.modelName}</a>
</p>
</div>
and refresh the list.
You can go a bit further by customizing the way each field of your '‘User’' entity is displayed in the list and form views.
To customize a field, use the '‘#{crud.custom}’' tag:
#{crud.table fields:['name', 'company']}
#{crud.custom 'company'}
<a href="@{Companies.show(object.company.id)}">${object.company.name}</a>
#{/crud.custom}
#{/crud.table}
You can also display additional columns or form inputs by defining custom handlers:
#{crud.table fields:['name', 'company', 'edit']}
#{crud.custom 'company'}
<a href="@{Companies.show(object.company.id)}">${object.company.name}</a>
#{/crud.custom}
#{crud.custom 'edit'}
<a href="@{Persons.edit(object.id)}">Edit</a>
#{/crud.custom}
#{/crud.table}
Comments
Use this form to add corrections, additions and suggestions about the documentation on this page. Please ask questions on the play-framework group instead. Support requests, bug reports, and off-topic comments will be deleted without warning.