Playframework(2)Java Project and Concept HTTP programming

Playframework(2)Java Project and Concept HTTP programming
It is not good to using eclipse IDE. I will verify the Playframework again based on other IDE.
Play with IDEA
>idea with-sources

But it seems that I am still only familiar with eclipse.
Face these error message during run eclipse enable command
>play eclipsify with-sources=true

Error Message:
[info] Loading global plugins from /Users/carl/.sbt/plugins
[info] Loading project definition from /Users/carl/work/play/firstapp/project
[info] Set current project to firstapp (in build file:/Users/carl/work/play/firstapp/)
[error] Not a valid command: eclipsify (similar: eclipse)
[error] Not a valid project ID: eclipsify
[error] Not a valid configuration: eclipsify
[error] Not a valid key: eclipsify
[error] eclipsify

Solutions:
Just go and get rid of the ~/.sbt/plugins
play>eclipsify with-source=true

1. HTTP programming
Actions, Controllers and Results
Action is a java method that processes the request parameters, and produces a result to be sent to the client.

Controller groups several action methods.

An HTTP result with a status code, a set of HTTP headers and a body to be sent to the web client.

Redirects are simple results. They do not have result body.
return redirect("/user/home");

HTTP routing
The router is the component that translates each incoming HTTP request to an action call.
The event contains two major pieces of information:
1. The request path (such as /clients/152, /photos/list), including the query string
2. The HTTP method.(GET, POST, …)

Routes are defined in the conf/routes file.

The routes file syntax.
GET /clients/:id controllers.Clients.show(id:Long)

The HTTP method (GET, POST, PUT, DELETE, HEAD)

The URI pattern
1. static path
GET /clients controllers.Clients.list()
2. Dynamic parts
GET /clients/:id controllers.Clients.show(id:Long)
3. Dynamic parts spanning serveral /
GET /files/*name controllers.Application.download(name)
4. Dynamic parts with custom regular expressions
GET /clients/$id<[0-9]+> controllers.Clients.show(id: Long)

Reverse Routing
GET /hello/:name controllers.Application.hello(name)
//Redirect to /hello/Bob
public static Result index(){
return redirect(controllers.routes.Application.hello("Bob"));
}

Manipulating the response
Changing the default Content-Type
The result content type is automatically inferred from the Java value you specify as body.
Result textResult = ok("Hello World"); // text/plain
Result jsonResult = ok(jerksonObject); //application/json

Another way, we can identify the content type in HTTP response.
public static Result index(){
response().setContentType("text/html");
return ok("<h1>Hello World!</h1>");
}

Setting HTTP response headers just like what we did in java struts actions
response().setContentType("text/html");
response().setHeader(CACHE_CONTROL, "max-age=3600");

Setting and discarding cookies
response().setCookie("theme", "blue");
response().discardCookies("theme");

response().setContentType("text/html; charset=utf-8");

Session and Flash Scopes
Save the data in Session or the Flash scope to share the data across multiple HTTP requests.
Data in session are available during the whole user session.
Data stored in the flash scope are only available to the next request.

Maybe these rules are only in play.
Session and Flash data are not stored in the server but are added to each subsequent HTTP request, using cookies. The data size is very limited (up to 4 KB) and we can only store string values.
And there is no technical timeout for the session.

Reading a Session value
public static Result index(){
String user = session("connected");
…snip...
}

Storing data into the Session
public static Result index(){
session("connected", "user@gmail.com");
…snip...
}

or we can remove the session like this > session.remove("connected");

Discarding the whole session > session.clear();

Flash Scope
The flash scope should only be used to transport success/error messages on simple non-Ajax applications.
String message = flash("success");
flash("success", "The item has been created");

Body Parsers
An HTTP request (POST and PUT operations) contains a body. This body can be formatted with any format specified in the Content-Type header. A body parser transforms this request body into a Java value.

The BodyParser Java API
RequestBody body = request().body();
return ok("Got body: " + body);

or

return ok("Got json: " + body.asJson());

The types and methods:
text/plain String asText()
application/json JsonNode asJson()
text/xml org.w3c.Document asXml()
application/form-url-encoded Map<String, String[]> asFormUrlEncoded()
multipart/form-data Http.MultipartFormData asMultpartFormData()
other content type Http.RawBuffer asRaw()

Action Composition

references:
http://www.playframework.org/documentation/2.0.4/JavaHome
http://www.playframework.org/documentation/2.0.4/JavaBodyParsers
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值