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

使用Play的模块

h1. Using Play modules

 

一个Play的应用可以是一些应用模块的集合,这允许你可以在不同的应用中重用应用的组件,或在一个大的项目中将应用分割为几个小的应用。

A Play application can be assembled from several application modules. This allows you to reuse application components across several application or split a large application into several smaller applications.

 

什么是模块

h2. <a>What is a module?</a>

 

一个模块只是另一个Play应用,只是在应用的模块中的资源的载入方面中有一些不同。

A module is just another Play application; however some differences exist in the way resources are loaded for an application module:

 

一个模块不能有conf/application.conf文件

一个模块可以有conf/routes文件,但是这些路由不会被自动载入。

首先在主应用的路径中搜索所有文件,然后在所有载入的模块总搜索文件。

一个模块可以在module/lib中包含java字节码包(jar包)

模块中的所有东西都是可选的。

* A module does not have a **conf/application.conf** file.

* A module can have a **conf/routes** file, but these routes will not be  loaded automatically.

* All files are first searched for in the main application path, then in all loaded modules.

* A module can contain plain Java code packaged as a JAR file in the module/lib directory.

* Everything in a module is optional.

 

你可以使用play new-module命令创建一个模块。

You can create a module with the **play new-module** command.

 

怎样从一个应用中载入一个模块?

h2. <a>How to load a module from an application</a>

 

默认的模块的安装路径在$PLAY_HOME/modules文件夹中。这个路径下的模块会自动的对使用这个框架的应用可用。

The default modules installation path is in the **$PLAY_HOME/modules** directory. This way all modules are automatically available to all applications run with the framework.

 

如果你需要从一个应用中载入一个额外的模块,只需要在住应用的application.conf文件中声明额外的模块,如下:

If you want to load external modules from an application, just declare external modules in the application.conf file in the main application.

 

 

bc. # Additional modules

# ~~~~~

# A module is another Play application. Add a line for each module you want

# to add to your application. Module paths are either absolute or relative to

# the application root.

#

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

 

你还可以创建一个独立的项目包含你的应用中所需的所有模块,你只需要把所有的模块复制到你的应用中相同的位置即可。

You can also create a standalone project that contains all modules needed by your application. You just need to copy all modules into the same directory as your application. 

 

例如你有一个大的项目,包含一个CMS组件,一个论坛组件,一个号码簿组件,而且使用了GWT模块,你可以建立一个项目使用下面的这个布局。

Let’s say you have a large application with a CMS component, a forum component, a directory component and using the GWT module. You can create a project using this layout:

 

bc. /my-project

    /cms

    /forum

    /directory

    /gwt

    /main

 

在这里main是你的主项目所在的文件夹(使用play new 命令创建的),gwt是一个从模块库中安装的模块,cms,forum,director是使用(play new-module命令创建的)。

Here **main** is the directory of your main application (created using the **play new** command), **gwt** is a module installed from the modules repository, and **cms**, **forum** and **directory** are modules created using the **play new-module** command.

 

现在我们在主项目的配置文件中添加这些导入的模块。

Now from the main application configuration file (my-project/main/conf/application.conf), you can load these modules using:

 

bc. # Additional modules

# ~~~~~

mdoule.gwt=../gwt

module.cms=../cms

module.forum=../forum

module.directory=../directory

 

当你运行主项目时(使用 play run my-project/main),它会把所有这些模块加载到内存之内存中作为一个大的项目。

When you run the main application (using **play run my-project/main**) it will load all these modules in memory as a larger application.

 

如果模块的路径是相对的,那么它们相对的是主应用的根目录。

p(note). If module paths are relative, they are resolved from the main application root.

 

从模块中载入默认的路由。

h2. <a>Load default routes from modules</a>

 

一个模块可以提供一个默认的路由文件,你可以把它导入到主应用的路由文件中,使用一个特殊的路由声明。

A module can provide a default routes file. You can load it in the main application routes file, using a special route declaration:

 

bc. # Import the default CRUD routes

GET     /admin     module:crud

 

你甚至可以从所有可得到的模块中导入所有的路由。

You can even load routes from all available modules:

 

bc. GET     /     module:*

 

使用模块库

h2. <a>Using the module repository</a>

 

模块仓库认定了所有社区贡献的模块,一个模块可以有很多版本,你需要检查模块的文档去知道你需要的是哪个版本。

The "module repository":http://www.playframework.org/modules identifies all modules contributed by the community. A module can have several versions. You have to check the module’s documentation for which version you need to use for your framework version.

 

你还可以查看模块库中所有的模块,使用play list-modules命令。

You can also browse the module repository using the **play list-modules** command. 

 

bc. gbo-mac:~ guillaume$ play11 list-modules

~        _            _ 

~  _ __ | | __ _ _  _| |

~ | '_ \| |/ _' | || |_|

~ |  __/|_|\____|\__ (_)

~ |_|            |__/   

~

~ play! 1.1-unstable-r761, http://www.playframework.org

~

~ You can also browse this list online at http://www.playframework.org/modules

~

~ [bespin]

~   Bespin online editor

~   http://www.playframework.org/modules/bespin

~   Versions: 1.0, 1.0.1

~

~ [cobertura]

~   Cobertura

~   http://www.playframework.org/modules/cobertura

~   Versions: 1.0

...

 

你可以是使用命令play install {module}-{version}安装模块,例如,为框架安装Scala支持,使用命令:play install scala-head。

You can install a module using the **play install {module}-{version}** command. For example, to install the Scala support to the framework, use:

 

bc. play install scala-head

 

按惯例,使用head版本号的版本是模块的不稳定版本。你也可以忽略版本号,直接安装默认的模块的版本。例如play install scala。

By convention the **head** version is the unstable version of the module. You can also install the default version of a module by omitting the version information. For example:

 

bc. play install scala

 

按这种方式安装的模块被下载到了你的框架的安装目录下的/modules文件夹中。你可以使用--path选项改变安装位置。

Modules installed this way are downloaded to the **/modules** directory of your framework installation. You can change the installation path using the **--path** option:

 

bc. play install gwt --path=my-project

 

为模块仓库贡献新的模块

h2. <a>Contributing a new module to the module repository</a>

 

首先你需要一个OpenbID。它帮助我们确认你是你的模块的作者。然后在Google Group中发给我们一个模块登记请求。

First you need to have an OpenID. It will help us to authenticate you as author of your modules. Then send us a module registration request on the Google Group.

 

请告诉我们:

 更多的关于你的模块的信息。

 你的模块的名称,他必须匹配[a-zA-Z]正则表达式

 模块的简短描述

 你的项目主页

 你的OpenID

 你的模块必须被托管到某个地方,以便使代码可得到并且可以提交bugs,如果你没有主意,那么github,Google Code和Launchpad都是不错的选择。

Please tell us:

 

* More about your module. What is it?

* Your module name. It must match the [a-zA-Z]+ regular expression.

* A short description of the module.

* Your project home page.

* Your OpenID.

* Your module must be hosted somewhere with the source code available and a way to report bugs. If you don’t have any idea, github, Google Code and Launchpad are good choices. 

 

需要发行你的模块,简单的使用play build-module就可以了,然后连接模块库中上传生成的包。

To release your module, simply use the play build-module command. Then connect to the module repository and upload the generated package.

 

你当然还可以使用官方的Google  Group去寻求帮助,或者分享关于你的工作的信息。

You can of course use the offical Google Group to provide help and share information about your work.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值