- 浏览: 2539970 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 467NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 464NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 285Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 276NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 254Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 564NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 255Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 356Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 363Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
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是一个强大的Java和Scala应用开发框架,它以其简洁的API、快速的开发周期以及对Web标准的紧密集成而闻名。本教程旨在为初学者和有经验的开发者提供全面的指导,帮助他们掌握Play Framework2的核心...
Play Framework 是一个开源的Web应用框架,主要针对Java和Scala开发者设计,它的核心理念是简化开发流程,提高开发效率,并且特别强调了RESTful架构风格。这个“playframework中文教程.zip”压缩包很可能是为了帮助...
Play Framework框架是一种基于Java的软件框架,旨在提高开发效率和提供REST式的架构风格。该框架可以让开发者继续使用他们喜欢的开发环境或繻库,不需要切换到另一种语言、IDE或者其他繻库。 一、Play Framework...
- **框架简介**:Play Framework 是一个开源的 Web 开发框架,基于 Java 和 Scala 编程语言。它采用轻量级、非阻塞的服务端架构,特别适合开发高性能、可扩展的应用程序。Play Framework 通过其独特的设计理念简化了...
playframework javaweb playframework javaweb
Play Framework 是一个开源的Web应用框架,用于构建现代、高性能的Java和Scala应用程序。它采用模型-视图-控制器(MVC)架构模式,并且强调简洁的代码和开发的即时反馈。Play Framework API 是开发者使用该框架进行...
2. **无XML配置**:Play Framework倾向于使用Java注解和纯代码配置,避免了XML配置文件的繁琐,降低了学习曲线,同时也减少了出错的可能性。 3. **简单的路由系统**:通过简单的 routes 文件,你可以定义URL与控制...
PlayFramework是一个高性能的Java和Scala框架,它支持Web应用的快速开发,并且主要面向RESTful应用程序。在PlayFramework中,为了确保数据的准确性和合法性,通常会在数据保存到数据库之前,对HTTP请求中的参数进行...
Play Framework是一个full-stack(全栈的)Java Web应用框架,包括一个简单的无状态MVC模型,具有Hibernate的对象持续,一个基于Groovy的模板引擎,以及建立一个现代Web应用所需的所有东西。 Play Framework的...
- **定义与背景**:Play Framework 是一款基于 Java 和 Scala 的高性能、轻量级 Web 开发框架。它采用 RESTful 架构设计,支持热重载功能,能够显著提高开发效率。本书(《Play Framework Cookbook》)提供了超过 60...
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 -> 2.3.1 : 1.3-beta1 (Scala 2.1
Play Framework 是一个基于Java的开源Web应用框架,它遵循MVC(模型-视图-控制器)架构模式,致力于简化Web应用程序的开发过程。这个资料大全包含了许多关于Play Framework的重要资源,帮助开发者深入理解和高效使用...
Play Framework是一款基于Java和Scala的开源Web应用框架,它遵循模型-视图-控制器(MVC)架构模式,旨在提供高效、简洁且可测试的开发环境。标题中的"v2.7.9"指的是该框架的特定版本,通常每个新版本会包含性能优化...
Play Framework 2.0 是一个开源的Web应用框架,它基于Scala和Java语言,遵循“模式-动作”(Action)架构,提供了一种轻量级、敏捷开发的方式。本篇文章将引导你通过创建一个简单的待办事项(Todo List)应用来了解...
Play Framework 是一个基于Java和Scala的开源Web应用框架,它遵循模型-视图-控制器(MVC)架构模式。在“Playframework框架学习之路 1”中,我们可能要探讨这个框架的基础概念、安装过程以及如何创建一个简单的应用...
play framework2.01上半部分。
Mastering Play Framework for Scala
Play Framework 2.0 是一个基于Java和Scala的开源Web应用程序框架,以其“写后即运行”的特性而闻名。这个入门教程的第三部分是关于如何使用Play Framework构建一个简单的留言板应用。在这里,我们将深入探讨Play ...
Play Framework是一个强大的、基于Java和Scala的开源Web应用程序框架,它采用模型-视图-控制器(MVC)架构模式,以简洁的API和直观的开发体验受到开发者喜爱。本篇文章将详述如何在Windows环境下安装配置Play环境...