英文对照 介绍Play Framework 框架 CURD模块

lyuehh 发布于 2010/08/08 13:48
阅读 1K+
收藏 3

CURD:管理员的生成器

h1. 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模块

h2. <a>Enable the CRUD module for the application</a>

 

在/conf/application.conf,文件中,使用下面一行配置激活CURD模块

In the **/conf/application.conf** file, enable the CRUD module by adding this line:

 

bc. # The crud module

module.crud=${play.path}/modules/crud

 

引入默认的CURD路由

h2. <a>Import default CRUD routes</a>

 

在conf/routes文件中,增加下面一行导入默认的模块路由

In the **conf/routes** file, import the default module routes by adding this line:

 

bc. # 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类

h2. <a>Create a User class</a>

 

我们从创建一个User类开始

We will start by creating a User class:

 

bc. 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控制器

h2. <a>Create the Users controller</a>

 

然后我们创建一个简单的控制器,只是继承与CURD控制器

Then, we create a simple controller that just inherits the CRUD controller.

 

bc. 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.

 

bc. package controllers;

 

@CRUD.For(User.class)

public class AdminUsers extends CRUD {

 

}

 

User 表单

h2. <a>The User form</a>

 

点击增加按钮,你应该看到一个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:

 

bc. 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

h2. <a>Change the form label</a>

 

在你的应用的conf/messages中增加下面这些行

Add these lines to the **conf/messages** file in your application:

 

bc. name=Name

email=Email address

address=Postal address

 

然后刷新User表单

And refresh the User form.

 

!images/crud4!

 

创建一个User然后自定义list视图

h2. <a>Create a User and customize the list view</a>

默认的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:

 

bc. 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:

 

bc. #{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>

 

然后刷新list

and refresh the list.

 

!images/crud6!

 

自定义fields渲染,curd.custom标签

h2. <a>Custom fields rendering: the crud.custom tag</a>

 

你可以在自定义的路上走的更远一些,在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:

 

bc. #{crud.table fields:['name', 'company']}

 

   #{crud.custom 'company'}

       <a href="@{Companies.show(object.company.id)}">${object.company.name}</a>

   #{/crud.custom}

 

#{/crud.table}

 

你还可以显示额外的column或者定义自定义的handlers

You can also display additional columns or form inputs by defining custom handlers:

 

bc. #{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}

 

String列表,和enumeration列表

h2. List 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:

 

bc. @Entity

public class Account extends Model {

 

        @CollectionOfElements

        public Set<ContentType> contentTypes;

 

        @CollectionOfElements

        public Set<String> usernames;

 

 

        public Account(Set<String> usernames {

                super();

                this.usernames = 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.

 

限制:

h2. <a>Limitation</a>

 

curd模块会显示实体中的关系,那些没有mappedBy属性的。

The crud module will show the relations that are unidirectional in one entity: the one that  does not have the mappedBy attribute.

加载中
返回顶部
顶部