Play Framework 框架 CURD模块
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
本文由liuguihua0823贡献
doc文档可能在WAP端浏览体验不佳。建议您优先选择TXT,或下载源文件到本机查看。
CURD:管理员的生成器 CRUD: Administration generator
CURD(增加,读取,更新,删除)模块生成一个完全可用的 web 接口为你的 JPA 模型对象。 The CRUD (Create, Read, Update, Delete) module generates a fully usable web interface for your JPA Model objects.
让我们看一个简单的例子。 Let's see a simple example.
为应用激活 CURD 模块 Enable the CRUD module for the application
在/conf/application.conf,文件中,使用下面一行配置激活 CURD 模块 In the **/conf/application.conf** file, enable the CRUD module by adding this line:
# The crud module module.crud=${play.path}/modules/crud
引入默认的 CURD 路由
Import default CRUD routes
在 conf/routes 文件中,增加下面一行导入默认的模块路由 In the **conf/routes** file, import the default module routes by adding this line:
# Import CRUD routes * /admin module:crud
不推荐直接使用默认的路由,你可以定义你自己的路由,或者混合使用两者 p(note). **Note** that using the default routes file is not required. You can also define your own routes, or mix the two.
增加一个 User 类 Create a User class
我们从创建一个 User 类开始 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;
}
增加一个 Users 控制器 Create the Users controller
然后我们创建一个简单的控制器,只是继承与 CURD 控制器 Then, we create a simple controller that just inherits the CRUD controller.
package controllers;
public class Users extends CRUD {
}
现在打开 http://localhost:9000/admin 然后你可以看到管理员的界面 Now open "http://localhost:9000/admin":http://localhost:9000/admin and you should see the User admin area.
!images/crud1!
控制器的类名必须是一个模型类名+'s',如果你想使用其他的名字, 只需要使用 一个注解 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 {
}
User 表单
The User form
点击增加按钮,你应该看到一个 User 表单 Click the **Add** button, and you should see the User form.
!images/crud2!
现在我们给 User 类增加一些验证规则 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; }
}
刷新 User 表单,你可以看到验证已经自动起作用了。 Refresh the User form and you will see that the validation is automatically applied.
!images/crud3!
改变表单的 label Change the form label
在你的应用的 conf/messages 中增加下面这些行 Add these lines to the **conf/messages** file in your application:
name=Name email=Email address address=Postal address
然后刷新 User 表单 And refresh the User form.
!images/crud4!
创建一个 User 然后自定义 list 视图 Create a User and customize the list view 默认的 list 视图值有一列,包含的是对象的 toString()方法的结果 The default list view uses only one column containing the result of the object's toString() method.
!images/crud5!
要自定义这些视图,我们在应用中可以创建一个 /app/views/Users/list.html 模板 To customize this view, we need to create the **/app/views/Users/list.html** template in the application.
打开一个 shell,进入应用的目录里键入: Open a shell, go the application directory and type:
play crud:ov --template Users/list
这样会复制默认的 CURD 的 list.html 模板到你的应用的 Users/list.html, 覆盖它如果存在的话。 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' /}
&{'crud.list.title', }
#{crud.search /}
#{crud.table fields:['email', 'name'] /}
#{crud.pagination /}
&{'crud.add', type.modelName}
然后刷新 list and refresh the list.
!images/crud6!
自定义 fields 渲染,curd.custom 标签 Custom fields rendering: the crud.custom tag
你可以在自定义的路上走的更远一些,在 list 和 form 视图中,为你的 User 实体的每一个 field 的显示做些修改 You can go a bit further by customizing the way each field of your ''User'' entity is displayed in the list and form views.
去自定义 field,使用#{crud.custom}标签 To customize a field, use the ''#{crud.custom}'' tag:
#{crud.table fields:['name', 'company']}
#{crud.custom 'company'} ${p } #{/crud.custom}
#{/crud.table}
你还可以显示额外的 column 或者定义自定义的 handlers You can also display additional columns or form inputs by defining custom handlers:
#{crud.table fields:['name', 'company', 'edit']}
#{crud.custom 'company'} ${p } #{/crud.custom}
#{crud.custom 'edit'} Edit #{/crud.custom}
#{/crud.table}
String 列表,和 enumeration 列表 Li
st of String and List of enumeration
CURD 模块把他们当做一个 text field,在这个 text field 中,list 是一 个用逗号分隔开的 String,例如:
The CRUD module is showing them as a text field. In this text field, the list is represented by a comma separated list of String. For example:
@Entity public class Account extends Model {
@CollectionOfElements public Set
@CollectionOfElements public Set usernames;
public Account(Set usernames { super(); ernames = usernames; } } ~ 被显示为:
is showned as:
"myEnumId1","myEnumId2" for the contentTypes and "string1","string2" for the usernames. Per definition, this is what you should first customize in your CRUD module.
限制: Limitation
curd 模块会显示实体中的关系,那些没有 mappedBy 属性的。 The crud module will show the relations that are unidirectional in one entity: the one that does not have the mappedBy attribute.