爱悠闲 > Play Framework Cookbook (play框架食谱...)2

Play Framework Cookbook (play框架食谱...)2

分类: 翻译Translate  |  标签: 框架,文件传输工具,annotations,path,command,下载工具  |  作者: amtea 相关  |  发布日期 : 2014-11-03  |  热度 : 1166°

Installing and enabling the module should not leave any open questions for you at this point. 

在安装开启模块这一点上,你应该别留下任何模糊的问题。

As you can see in the controller, it is now filled with annotations that resemble the entries in the routes.conf file,

 正如你所看到的在控制器中,它现在充满了注释,就像于进入了routes.conf(路由配置)文件,

which you could possibly have deleted by now for this example.

或许现在这个例子已经被你删除了.

However, then your application will not start, so you have to have an empty file at least.

然而,你的程序将为此无法启动,所以你至少要有一个空白文件。

The @ServeStatic annotation replaces the static command in the routes file.

在路由文件中,@ServeStatic注释代替了静态命令

 The @StaticRoutes annotation is just used for grouping several @ServeStatic annotations and could be left out in this example.

这个@StaticRoutes注释只是用来划分出几个@ServeStatic注解,其实,在这例子中可以忽略。

Each controller call now has to have an annotation in order to be reachable. 

现在,为了达到每个控制器调用,不得不都有了一个注释。

The name of the annotation is the HTTP method, or @Any, if it should match all HTTP methods.

注释的名字是HTTP方法,,或者@Any,如果它需要匹配所有的HTTP方法。

 Its only mandatory parameter is the value, 

它的唯一强制性参数是这个值

which resembles the URI—the second field in the routes.conf. 

就像在路由配置中的第二个属性URI

All other parameters are optional. Especially interesting is the priority parameter, 

所有其他参数都是可选的。特别令人注意的是参数优先级,

which can be used to give certain methods precedence. 

可给某些方法优先权。

This allows a lower prioritized catch-all controller like in the preceding example, 

这样就允许一个较低的优先权捕获所有控制器,就像在之前的例子中那样。

but a special handling is required if the URI is called with the PUT method. 

但是一种特殊处理是必需的,那就是如果URI已被PUT方法调用。

You can easily check the correct behavior by using curl, 

通过使用curl,你可以很容易检查正确的行为。

a very practical command line HTTP client:

一个非常实用的HTTP客户端命令行:

(来自百度:cURL是一个利用URL语法在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下载工具。cURL支援的通讯协定有FTP、FTPS、HTTP、HTTPS...)

curl -v localhost:9000/
This command should give you a result similar to this:
> GET / HTTP/1.1
> User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 
OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: localhost:9000
> Accept: */*
>
< HTTP/1.1 403 Forbidden
< Server: Play! Framework;1.1;dev
< Content-Type: text/html; charset=utf-8
< Set-Cookie: PLAY_FLASH=;Path=/
< Set-Cookie: PLAY_ERRORS=;Path=/
< Set-Cookie: PLAY_SESSION=0c7df945a5375480993f51914804284a3bb
ca726-%00___ID%3A70963572-b0fc-4c8c-b8d5-871cb842c5a2%00;Path=/
< Cache-Control: no-cache
< Content-Length: 32
<
<h1>Reserved for administrator</h1>


You can see the HTTP error message and the content returned. You can trigger a PUT request 
in a similar fashion:
curl -X PUT -v localhost:9000/
> PUT / HTTP/1.1
> User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 
OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: localhost:9000
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: Play! Framework;1.1;dev
< Content-Type: text/plain; charset=utf-8
< Set-Cookie: PLAY_FLASH=;Path=/
< Set-Cookie: PLAY_ERRORS=;Path=/
< Set-Cookie: PLAY_SESSION=f0cb6762afa7c860dde3fe1907e8847347
6e2564-%00___ID%3A6cc88736-20bb-43c1-9d43-42af47728132%00;Path=/
< Cache-Control: no-cache
< Content-Length: 16
Secret news here
As you can see now, the priority has voted the controller method for the PUT method which is 
chosen and returned.