中英文对照 介绍Play Framework 框架 概述(Overview)

lyuehh 发布于 2010/07/11 11:34
阅读 2K+
收藏 6

Play框架概述

h1. Play framework overview

Play框架是臃肿的企业级Java之外的另一个选择,它关注的是开发的效率和提供REST式的架构风格,
Play是“敏捷软件开发”的绝佳伴侣。

The Play framework is a clean alternative to bloated Enterprise Java stacks. It focuses on developer productivity and targets RESTful architectures. Play is a perfect companion to *agile software development*.

Play框架的目标是使用Java进行快速的web项目开发,让我们看一下它是怎么做到的。
The Play framework’s goal is to ease web applications development while sticking with Java. Let’s see how this is possible.

想看一些代码?
p(note). **Wanna see some code?**

查看“你可以使用Play做的5件最酷的事”:5thinds,或者直接跳至“hello world 实例”:firstapp
Check "Five cool things you can do with play":5things, or start directly with the "hello world tutorial":firstapp.

一个不会让你感觉Java麻烦的java框架
A Java framework without the pain

Play是一个使用纯Java开发的框架,它可以让你继续使用你喜欢的开发环境或类库。如果你已经是一个Java平台的开发者,
那么你不需要切换到另一种语言,其他IDE或者其他类库,只是切换到了另一个更有效率的java环境。
Play is a pure Java framework and allows you to keep your preferred development tools and libraries. If you already use Java as a development platform you don’t need to switch to another language, another IDE and other libraries. *Just switch to a more productive Java environment!*

修改bug,然后Play会自动载入
Fix the bug and hit Reload

Java平台在开发效率方面已经是声名狼藉了,可能的原因就是重复 “编译-打包-部署”的循环。
The Java platform is infamous for its low productivity, mainly due to repeated and tedious compile-package-deploy cycles.

这就是为什么我们重新考虑开发周期,让使用Play开发变得更有效率。
That’s why we rethought the development cycle to make developing with Play an efficient process.

框架自动编译Java源代码,然后直接热加载到JVM中而不需要重启服务器,你可以编辑,重新加载
然后直接看到修改后的结果,就像在LAMP或者Rails环境中一样。
The framework compiles your Java sources directly and hot-reloads them into the JVM without the need to restart the server. You can then edit, reload and see your modifications immediately, just as in a LAMP or Rails environment.

而且更有趣的是你按自己的爱好,仅仅使用一个简单的文本编辑器而避免使用全功能的Java IDE。
It’s so fun that you can even work with a simple text editor and skip a full-featured Java IDE if you want.

当有错误发生时,框架努力辨别,然后直接显示出你的错误。
Whenever an error occurs, the framework makes its best effort to identify and show you the problem.

//下面一句不太好翻译啊
甚至堆栈被分析优化使错误更容易的被解决,看模板执行时如何完美地指出错误所在地的.
Even the stack traces are stripped down and optimized to make it easier to solve problems. Look how template execution is nicely integrated to the Java stack trace.

简单的无状态的MVC架构
Simple stateless MVC architecture

你一边有一个数据库另一边有一个浏览器,为什么你要在此之间需要一个状态呢?
You’ve got a database on one side and a web browser on the other. Why should you have a state in between?

以有状态和组件式为基础的Java web框架使之容易的自动保存页面状态,但是这带来了很多其他问题,如果用户
打开了第二个窗口时会发生什么,如果用户单击了浏览器的后退按钮呢?
Stateful and component based Java Web frameworks make it easy to automatically save page state, but that brings a lot of other problems: what happens if the user opens a second window? What if the user hits the browser back button?

PHP,Ruby on Rails和Django等许多web应用框架促进了“无共享”架构。
随着浏览器越来越强大,现在很容易使用Ajax,或者离线存储去解决客户端的状态问题。
The "Share Nothing":http://zef.me/883/the-share-nothing-architecture architecture is promoted by many Web application frameworks from PHP to Ruby on Rails or Django. As the browser is becoming more and more powerful, it is now easy to use Ajax or offline storage to solve the state problems client-side.

我们不需要再去hack HTTP模型,在web上重建一个虚假的状态。另一方面,
“无共享”使渲染局部页面,相似的页面,或者部分页面更新变的更容易。
We don’t need to hack the HTTP model anymore to rebuild a fake state over the Web. Another aspect of ‘share nothing’ is to make it easier to render portions of the page in parallel, and to do partial page updates (or progressive enhancements).

HTTP到代码映射
HTTP-to-code mapping

如果你已经使用了另外一种Java web框架,例如Servlet API或者Struts框架,你已经使用了把HTTP协议和Java API抽象的联系起来的奇怪的观念。(好难翻译啊)
我们不是这么想的。一个web应用框架应该给你完全的,直达的对Http进行操作,这是Play和其他Java web框架的一个根本不同。
If you’re already using another Java Web framework like the Servlet API or the Struts framework, you have already used an abstract view of the HTTP protocol with strange Java APIs and concepts. We think differently. A Web application framework should give you full, straightforward access to HTTP and its concepts. This is a fundamental difference between Play and other Java web application frameworks.

Http,Request,Response,REST架构,类型识别,URI都是Play框架主要的思想。
HTTP, the Request/Response pattern, the REST architectural style, content-type negotiation, URI are all major concepts for the Play framework.

例如,绑定一个URI 参数 到Java中调用就像下面一样
For instance, binding a URI pattern to a Java call is just one line:

bc. GET    /clients/{id}        Clients.show
如果Ajax,REST和在页面之间维护前进、后退操作是你每天开发web项目时都会遇到的问题,那么请尝试一下play吧。
If Ajax, REST and managing back/forward movement between web pages are some of the issues you face in your day-to-day web development, just give Play a try.


高效的模板引擎
Efficient templating engine

我们很喜欢JSP和JSTL表达式语。但是为什么我们需要这么多的配置文件去创建一个标签库,为什么我们不能对象模型进行完全的接触呢?(有点别扭)
jSP有很多的限制也是令人沮丧的原因。这就是为什么我们需要创建一个通用的模板系统,由JSP激发的灵感,但是没有它的限制。
We like the idea behind JSP & Expression Language. But why do we need so many configuration files to create a tag library? Why can’t we have full access to the underlying object model? JSP has a lot of limitations and this is indeed frustrating. That’s why we’ve created a custom template system, inspired by JSP but without its constraints!

你和其他人可能会疲倦的写类似这样的代码
You and anyone else should be tired of writing things like this:

bc. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
 
<c:choose>
    <c:when test="${emails.unread != null && fn:size(emails.unread)}">
        You have ${fn:size(emails.unread)} unread email(s)!
    </c:when>
    <c:otherwise>
        You have no unread emails!
    </c:otherwise>
</c:choose>

我们认为你应该更倾向于这样写
We think you would certainly prefer to write:

bc. You have ${emails.unread ?: 'no'} ${emails.unread?.pluralize('email')} !
Play模板使用的 表达式语言是Groovy,它和Java语法一致,
Play使用这些模板系统区渲染HTML请求,你可以使用它去产生其他格式的文档例如 email信息,JSON输出等。
The expression language used by the play template engine is "Groovy":http://groovy.codehaus.org/, which provides a syntax consistent with Java. While Play mainly uses the templating system to render HTML responses, you are free to use it to generate any other documents such as e-mail messages, JSON responses, etc.

JPA绑定
JPA on steroids

JPA是一个Java的ORM框架,如果你已经知道它的话,你会惊讶于它和Play的集成。
不需要任何配置,Play会自动启动JPA实体管理器,并神奇的同步,在代码重新载入时。
The Java Persistence API (JPA) is the cleanest object-relational mapping ORM) API available for Java. If you already know it you will be amazed how much simpler it becomes with Play. With nothing to configure, Play will automatically start the JPA Entity Manager and magically synchronize it while the code is reloaded.

而且如果你使用提供的 **play.db.jpa.Model** 超类时,它会帮助你 把代码变的更漂亮。来看一下。
Moreover if you use the provided **play.db.jpa.Model** superclass it will help make your code prettier. Have a look:

bc. public void messages(int page) {
    User connectedUser = User.find("byEmail", connected());
    List<Message> messages = Message.find(
        "user = ? and read = false order by date desc",
        connectedUser
    ).from(page * 10).fetch(10);
    render(connectedUser, messages);
}

测试驱动开发(如果你喜欢它的话)
Test driven development (if you like it)

集成的测试可以让你更容易的去进行测试驱动开发,你可以写下所有类型的测试,从简单的集成测试到完整
的acceptance 测试,然后直接在浏览器中使用Selenium运行。代码覆盖率也会考虑到。
The integrated test runner makes it easy for you do test-driven development (TDD). You can write all kinds of tests, from simple unit tests to full acceptance tests, and run them directly in a browser using "Selenium":http://seleniumhq.org/. Code coverage is also measured.

全栈的应用开发框架
Full-stack application framework

Play框架的灵感来自我们自己的Java应用,它包含了创建现代 web应用所需的一切。它包含:
通过JDBC的关系数据库支持
使用hibernate管理对象关系映射(使用了JPA API)
集成的缓存支持,容易使用的分布式缓存系统(如果需要的话)
使用JSON和XML的简单web service(我们说的是’真正‘的web service,不是SOAP)
支持使用OpenID进行分布式的身份认证
你的web 应用可以部署到任何支持的地方(应用服务器,GAE,云等)
图形操作API

The Play framework was initially inspired by our own Java applications. It has all the tools needed to create a modern web application, including:

* relational database support through JDBC.
* object-relational mapping using Hibernate (with the JPA API).
* integrated cache support, with easy use of the distributed memcached system if needed.
* straightforward web services consumption either in JSON or XML (we are talking *real* web services here; not the SOAP stuff).
* OpenID support for distributed authentication.
* your web application ready to be deployed anywhere (application server, Google App Engine, Cloud, etc…)
* image manipulation API.

Play模块化的结构使你可以把web 应用和其他结合在一起,感谢模块,你可以以一种非常简单的方式重用你的Java 代码,
模板,静态资源(如JavaScript和CSS文件等).
The modular architecture lets you combine a web application with many others. Thanks to "application modules":modules, you can reuse your Java code, templates and static resources (such as JavaScript and CSS files) in a simple way.

请尝试一下吧
p(note). **Give it a try**
 
 安装、启动你的第一个应用
"Install the Play framework":install and start developing your first application.

加载中
0
红薯
红薯

已帮你把缺失的图片补上了:)

0
l
lyuehh

呵呵,谢谢了。

我翻译的是下载的发布包里的manual文件夹里的文件,我看了一下,和官方网站上的内容是一致的,但是它是textile格式的。图片的话,我下次加上。 

0
Kevin.
Kevin.

谢谢翻译,非常不错。

0
刘逸飞
刘逸飞
很好,顶顶!!!
返回顶部
顶部