`
sillycat
  • 浏览: 2552693 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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


分享到:
评论

相关推荐

    Play Framework

    1. Play Framework 介绍 2. 创建和发布 Play 应用 2.1 创建 Play 的工程 2.2 Play 常用指令 2.3 Play 应用的 JVM 调优 3. 如何读取静态资源 4. Play框架的配置文件 5. 使用 Play 框架开发 Java 应用 5.1 HTTP...

    Play Framework2本教程

    Play Framework2是一个强大的Java和Scala应用开发框架,它以其简洁的API、快速的开发周期以及对Web标准的紧密集成而闻名。本教程旨在为初学者和有经验的开发者提供全面的指导,帮助他们掌握Play Framework2的核心...

    playframework中文教程.zip

    Play Framework 是一个开源的Web应用框架,主要针对Java和Scala开发者设计,它的核心理念是简化开发流程,提高开发效率,并且特别强调了RESTful架构风格。这个“playframework中文教程.zip”压缩包很可能是为了帮助...

    Play framework框架

    Play Framework框架是一种基于Java的软件框架,旨在提高开发效率和提供REST式的架构风格。该框架可以让开发者继续使用他们喜欢的开发环境或繻库,不需要切换到另一种语言、IDE或者其他繻库。 一、Play Framework...

    Play Framework Cookbook.pdf

    - **框架简介**:Play Framework 是一个开源的 Web 开发框架,基于 Java 和 Scala 编程语言。它采用轻量级、非阻塞的服务端架构,特别适合开发高性能、可扩展的应用程序。Play Framework 通过其独特的设计理念简化了...

    playframework javaweb

    playframework javaweb playframework javaweb

    play framework api,play! framework api,play api

    Play Framework 是一个开源的Web应用框架,用于构建现代、高性能的Java和Scala应用程序。它采用模型-视图-控制器(MVC)架构模式,并且强调简洁的代码和开发的即时反馈。Play Framework API 是开发者使用该框架进行...

    playFramework1.2.3

    2. **无XML配置**:Play Framework倾向于使用Java注解和纯代码配置,避免了XML配置文件的繁琐,降低了学习曲线,同时也减少了出错的可能性。 3. **简单的路由系统**:通过简单的 routes 文件,你可以定义URL与控制...

    PlayFramework框架验证.pdf

    PlayFramework是一个高性能的Java和Scala框架,它支持Web应用的快速开发,并且主要面向RESTful应用程序。在PlayFramework中,为了确保数据的准确性和合法性,通常会在数据保存到数据库之前,对HTTP请求中的参数进行...

    Java Web框架Play Framework v2.1.0.zip

    Play Framework是一个full-stack(全栈的)Java Web应用框架,包括一个简单的无状态MVC模型,具有Hibernate的对象持续,一个基于Groovy的模板引擎,以及建立一个现代Web应用所需的所有东西。   Play Framework的...

    Play Framework Cookbook

    - **定义与背景**:Play Framework 是一款基于 Java 和 Scala 的高性能、轻量级 Web 开发框架。它采用 RESTful 架构设计,支持热重载功能,能够显著提高开发效率。本书(《Play Framework Cookbook》)提供了超过 60...

    play2-war-plugin, 用于 Play Framework 2.x的WAR插件.zip

    play2-war-plugin, 用于 Play Framework 2.x的WAR插件 用于 Play Framework 2.x的 WAR插件Current versions: Play 2.2.x : 1.2.1 Play 2.3.0 -&gt; 2.3.1 : 1.3-beta1 (Scala 2.1

    play,play framework资料大全

    Play Framework 是一个基于Java的开源Web应用框架,它遵循MVC(模型-视图-控制器)架构模式,致力于简化Web应用程序的开发过程。这个资料大全包含了许多关于Play Framework的重要资源,帮助开发者深入理解和高效使用...

    Play Framework应用程序框架 v2.7.9.zip

    Play Framework是一款基于Java和Scala的开源Web应用框架,它遵循模型-视图-控制器(MVC)架构模式,旨在提供高效、简洁且可测试的开发环境。标题中的"v2.7.9"指的是该框架的特定版本,通常每个新版本会包含性能优化...

    Play framework 2.0 -第一个应用程序

    Play Framework 2.0 是一个开源的Web应用框架,它基于Scala和Java语言,遵循“模式-动作”(Action)架构,提供了一种轻量级、敏捷开发的方式。本篇文章将引导你通过创建一个简单的待办事项(Todo List)应用来了解...

    playframework 框架学习之路 1

    Play Framework 是一个基于Java和Scala的开源Web应用框架,它遵循模型-视图-控制器(MVC)架构模式。在“Playframework框架学习之路 1”中,我们可能要探讨这个框架的基础概念、安装过程以及如何创建一个简单的应用...

    play framework2.01 part1

    play framework2.01上半部分。

    Mastering Play Framework for Scala

    Mastering Play Framework for Scala

    Play framework 2.0入门教程(三)的源代码,Play留言板

    Play Framework 2.0 是一个基于Java和Scala的开源Web应用程序框架,以其“写后即运行”的特性而闻名。这个入门教程的第三部分是关于如何使用Play Framework构建一个简单的留言板应用。在这里,我们将深入探讨Play ...

    playframework框架项目部署文档

    Play Framework是一个强大的、基于Java和Scala的开源Web应用程序框架,它采用模型-视图-控制器(MVC)架构模式,以简洁的API和直观的开发体验受到开发者喜爱。本篇文章将详述如何在Windows环境下安装配置Play环境...

Global site tag (gtag.js) - Google Analytics