play2.0文档-面向java开发者(3)
2012-04-09 21:17
429 查看
Manipulating the response 处理应答
Changing the default Content-Type 改变默认内容类型The result content type is automatically inferred from the Java value you specify as body.result的内容类型会根据你指定的java值自动推断出来For example:例如:Result textResult = ok("Hello World!");Will automatically set the
Content-Typeheader to
text/plain, while:这将会自动的设置内容类型为
text/plain,而:
Result jsonResult = ok(jerksonObject);will set the
Content-Typeheader to
application/json.将会设置内容类型为
application/json.This is pretty useful, but sometimes you want to change it. Just use the
as(newContentType)method on a result to create a new similiar result with a different
Content-Typeheader:这是十分有用的,如果你想改变它,只需要调用
as(newContentType)方法来创建一个新的result:
Result htmlResult = ok("<h1>Hello World!</h1>").as("text/html");You can also set the content type on the HTTP response:你也可以设置HTTP应答的内容类型。
public static Result index() {
response().setContentType("text/html");
return ok("<h1>Hello World!</h1>");
}
Setting HTTP response headers 设置HTTP应答头
You can add (or update) any HTTP response header:你可以添加(或更新)任何HTTP应答头:public static Result index() {
response().setContentType("text/html");
response().setHeader(CACHE_CONTROL, "max-age=3600");
response().setHeader(ETAG, "xxx"); return ok("<h1>Hello World!</h1>");
}Note that setting an HTTP header will automatically discard any previous value.注意 设置HTTP头会覆盖当前的值。
Setting and discarding cookies 设置和删除cookies
Cookies are just a special form of HTTP headers, but Play provides a set of helpers to make it easier.Cookies只不过是HTTP头的特定格式,但是play 提供了一系列便利方法:You can easily add a Cookie to the HTTP response:你可以很容易给HTTP应答添加一个Cookie:response().setCookie("theme", "blue");Also, to discard a Cookie previously stored on the Web browser:你也可以删除已有的Cookie:
response().discardCookies("theme");
Specifying the character encoding for text results
指定文本results 的字符编码
For a text-based HTTP response it is very important to handle the character encoding correctly. Play handles that for you and usesutf-8by default.正确的处理字符编码对于文本类型的HTTP应答是非常重要的,Play默认用
utf-8.The encoding is used to both convert the text response to the corresponding bytes to send over the network socket, and to add the proper
;charset=xxxextension to the
Content-Typeheader.编码即用于把文本应答转换成相应的网络字节码,也为内容类型头添加恰当的
;charset=xxx扩展The encoding can be specified when you are generating the
Resultvalue:你可以在生成
Result值的时候指定编码:
public static Result index() {
response().setContentType("text/html; charset=iso-8859-1");
return ok("<h1>Hello World!</h1>", "iso-8859-1");
}
相关文章推荐
- play2.0文档-面向java开发者(4)
- play2.0文档-面向java开发者(6)
- play2.0文档-面向java开发者(7)异步HTTP编程
- play2.0文档-面向java开发者(5)
- play2.0文档-面向java开发者(2)
- Play 2.0 文档 - 入门 -- 针对Scala开发者
- 给Java开发者的Play Framework(2.4)介绍 Part1:Play的优缺点以及适用场景
- 【GO for java programmers】面向Java开发者的GO编程2
- Play 2.0 用户指南 - 使用缓存 -- 针对Scala开发者
- 30天了解30种技术系列---(11)Java开发者梦想的框架Play
- 【GO for java programmers】面向Java开发者的GO编程3
- Play 2.0 用户指南 - HTTP编程 --针对Scala开发者
- Play 2.0 用户指南 - 使用JSON库 -- 针对Scala开发者
- 面向Java开发者的Ajax
- 黑马程序员——Java基础---面向对象(面向对象,类,对象,匿名对象,封装,构造函数,构造代码块,this,static,main,帮助文档,静态代码块,单例)
- 【原创翻译】使用Hibernate持久化java 5 的枚举类型【AppFuse 2.0 文档-11】
- 面向 Java 开发者的 NoSQL 选项
- Play 2.0 用户指南 - 使用JSON库 -- 针对Scala开发者