`
starbhhc
  • 浏览: 631606 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

Play Framework介绍2--Hello World

 
阅读更多

 

Play是一个Rails风格的Java Web框架。先上官网的Hello World,感觉下。

运行环境,我换成了Windows ^_^

准备

我自己用的版本是play-2.2.6。

把路径放在环境变量下的path中,和jdk一样。

项目创建

打开CMD,执行:

play new helloworld(你在那个路径下new的就放在那个路径,)

 

Play new 命令在当前路径下创建了一个helloworld目录,其中包含一系列文件和目录,重要的如下:

app/ 包含应用核心,分为models,controllers和views目录。.java生活的地方^_^

conf/包含应用的所有配置。application.conf应用主配置.routes定义url路由规则,messages国际化用。

lib/ 包含应用依赖的标准.jar文件。

public/包含所有外部可访问的资源:js,css和image。

test/包含所有应用的测试程序。测试程序基于JUnit或Selenium。

注:Play要求所有文件必须是UTF-8编码。

 

等等应用的.class文件在哪儿。恩,Play不使用class文件而是直接读取Java源文件,并使用Eclipse compiler编译他们。

这导致两件重要的事情。首先运行时Play会检查你对源文件所作的变更并自动加载它们。其次,当发生异常时,Play将创建更好的错误报告并附加相关代码。

 

运行应用

在刚才创建的项目helloworld目录下:  play run helloworld

play启动Web Server并监听9000端口

打开浏览器键入http://localhost:9000,应用显示了一个缺省的欢迎页

 

注:很重要1.0和2.0不一样的地方,弄了好久才知道:

如果想放在eclipse中,就直接执行 play eclipse helloworld  (star)

 

现在,看下此页是如何显示的。

 

应用的主入口点配置在conf/routes文件中。它定义了应用所有可访问的URL。打开routes文件,会看到第一个route:

GET     /    Application.index

它告诉Play,当/路径收到GET请求后调用Application.indexJava方法。它是controllers.Application.index的缩写,因为controllers包是隐式的附加的。

创建标准Java应用时,通常使用一个入口点即main方法。Play应用则有多个,一个URL一个。这些方法称为action方法。定义action方法的类称为controller

打开helloworld/app/controllers/Application.java:

复制代码
package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

publicclass Application extends Controller {

publicstaticvoid index() {
render();
}

}
复制代码

 

看到Application扩展了play.mvcController类。它提供了所有Controller需要使用的方法,如index动作中使用的render方法。

index方法定义成public static void,因为Controller永远无需实例化和返回值。(译注:为了防止被使用者引入状态,并让Controller自然、干净而如此设计。但副作用是render只能通过throw扔出结果,用异常当GOTO,可谓兵行诡道)。

缺省的index动作调用render方法,通知Play渲染一个模板。模板是app/views目录下一个简单的text文件。此处使用Application/index.html

打开helloworld/app/views/Application/index.html文件:

#{extends 'main.html' /}
#{set title:'Home' /}

#{welcome /}

 

其中的内容是Play tag,类似JSP taglib.#{welcome/}tag生成了之前看到的欢迎消息。#{extends/}tags告诉Play此模板集成另一个main.html的模板.模板继承可用来创建复杂的web也并重用公共部分。

打开helloworld/app/views/main.html模板

复制代码
<!DOCTYPE html>

<html>
<head>
<title>#{get 'title' /}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" media="screen" href="@{'/public/stylesheets/main.css'}">
#{get 'moreStyles' /}
<link rel="shortcut icon" type="image/png" href="@{'/public/images/favicon.png'}">
<script src="@{'/public/javascripts/jquery-1.4.2.min.js'}" type="text/javascript" charset="utf-8"></script>
#{get 'moreScripts' /}
</head>
<body>
#{doLayout /}
</body>
</html>
复制代码

 

看到#{doLayout/}tag吗?是Application/index.html插入的位置。

 

创建FORM

编辑helloworld/app/views/Application/index.html模板

复制代码
#{extends 'main.html' /}
#{set title:'Home' /}

<form action="@{Application.sayHello()}" method="GET">
<input type="text" name="myName"/>
<input type="submit" value="Say hello!"/>
</form>
复制代码

 

我们使用@{…}符号请求Play自动产生调用Application.sayHello动作的方法。刷新浏览器。

 

Oops,出错了。因为引用了一个不存在的动作。需要在helloworld/app/controllers/Application.java中创建:

复制代码
package controllers;


import play.mvc.*;

publicclass Application extends Controller {

publicstaticvoid index() {
render();
}

publicstaticvoid sayHello(String myName){
render(myName);
}
}
复制代码

 

我们声明了myName参数,它会自动映射到form提交的HTTP请求的myName参数。刷新浏览器。

 

输入name提交,出现另一个错误.

 

因为Play渲染此动作的缺省模板时,没有找到它。我们创建文件helloworld/app/views/Application/sayHello.html

复制代码
#{extends'main.html'/}
#{set title:'Home'/}

<h1>Hello ${myName ?: 'guest'}!</h1>

<a href="@{Application.index()}">Back to form</a>
复制代码

 

然后刷新:

 

提供更好的URL

看下提交的url:

http://localhost:9000/application/sayhello?myName=chaos

它不够RESTful。因为Play通过缺省规则捕获了此URL

* /{controller}/{action} {controller}.{action}

 

可以编辑helloworld/conf/routes文件在缺省规则前添加一条规则,提供更自然的hello url

GET /hello Application.sayHello

 

 

自定义布局

可以修改模板更改布局。编辑helloworld/app/views/main.html文件:

 

添加验证

给form添加一个验证,要求name字段必填。我们通过Play validation实现。编辑helloworld/app/controllers/Application.java,在sayHello action处:

复制代码
publicstaticvoid sayHello(@Required String myName) {
if (validation.hasErrors()) {
flash.error("Oops, please enter your name!");
index();
}
render(myName);
}
复制代码

 

并import play.data.validation.*。@Required告诉Play自动检查myName字段是否填写。如果验证失败,我们加入一条消息到flash scope中并重定向到index动作。flash scope允许在重定向时保持消息。

编辑helloworld/app/views/Application/index.html显示错误消息

复制代码
#{extends'main.html'/}
#{set title:'Home'/}

#{if flash.error}
<p style="color:#c00">
${flash.error}
</p>
#{/if}

<form action="@{Application.sayHello()}" method="GET">
<input type="text" name="myName"/>
<input type="submit" value="Say hello!"/>
</form>
复制代码

 

输入空参数并提交,OK起作用了。

 

自动化测试

Selenium Test

在测试模式下运行应用。在cmd中输入play test helloworld。

 

打开浏览器,输入http://localhost:9000/@tests启动测试器。

 

执行测试

 

Selenium测试用例通常写成一个html文件。Play使用Play模板引擎生成这些文件。helloworld/test/Application.test.html文件:

复制代码
*{ You can use plain selenium command using the selenium tag }*

#{selenium}
// Open the home page, and check that no error occured
open('/')
assertNotTitle('Application error')
#{/selenium}
复制代码

此测试打开home页,确认响应中没有“Application error”。

让我们来编写自己的测试。编辑测试内容:

复制代码
*{ You can use plain selenium command using the selenium tag }*

#{selenium}
// Open the home page, and check that no error occurred
open('/')
assertNotTitle('Application error')

// Check that it is the form
assertTextPresent('The Hello world app.')

// Submit the form
clickAndWait('css=input[type=submit]')

// Check the error
assertTextPresent('Oops, please enter your name!')

// Type the name and submit
type('css=input[type=text]', 'bob')
clickAndWait('css=input[type=submit]')

// Check the result
assertTextPresent('Hello bob!')
assertTextPresent('The Hello world app.')

// Check the back link
clickAndWait('link=Back to form')

// Home page?
assertTextNotPresent('Hello bob!')
#{/selenium}
复制代码

重新执行

 

debug模式:

 

 启动debug的时候,在项目路径下 play debug run

 

1.配置好Play2.0开发环境

2.命令行转到创建项目的位置,play new 项目名称

3.回答play提出的问题,并输入play eclipsify,将play项目转为eclipse新项目

4.打开eclipse导入新建的项目

5.命令行输入play debug进入play的debug模式,会显示监听9999端口

6.在eclipse中右击项目,选择菜单Debug As ... 

7.新建Remote Java Application配置,将端口号改为9999,其它不变,点击Debug按钮将eclipse挂在到play启动调试的jvm上

(如有错误按顺序重复5-7步)

8.在上面启动play调试的命令行界面输入play run,启动play内置server

9.在eclipse中的代码加入断点,访问http://localhost:9000,命中断点,后面就可以正常调试了

分享到:
评论

相关推荐

    play-2.3-hello-world:Play Framework 2.3.x 版的简单“Hello World”示例

    这是 Play Framework 2.3.x 版的示例“Hello World”应用程序。 它是从转换而来的。 相关代码 使用activator new hello-world play-java创建应用程序后,唯一需要更改的内容在这些文件中: app/controllers/...

    play-scala-hello-world-tutorial:在Scala中播放的Hello World教程

    play-scala-hello-world-tutorial:在Scala中播放的Hello World教程

    play-angular11-starter:使用快速的即用型默认值引导您的游戏角项目

    播放Java的Hello World Web教程 要遵循本教程中的步骤,您将需要正确版本的Java和构建工具。 您可以使用任何Java构建工具来构建Play项目。 由于sbt利用了诸如自动重新加载之类的Play功能,因此本教程介绍了如何使用...

    puppet-play:木偶组件模块,用于部署打包的(.zip)播放框架应用程序

    play::application { ' helloworld ' : source =&gt; ' http://10.0.0.1/binaryrepo/helloworld.zip ' , port =&gt; ' 9000 ' , } 骇客 请根据需要向该模块发送更新。 一些突出的事情: spec 更好的文档

    一步一步学习Revel Web开源框架

    Hello World Go Web开发之Revel - 开发手册 介绍 概念 组织结构 Revel运行原理 概述 路由 参数绑定 验证 Session/Flash 返回值 模板 拦截器 插件 模块 Websockets 测试 日志 部署 app.conf 命令行工具 Revel框架中用...

    java8看不到源码-prune:Play框架的性能测试工具

    Framework 性能的工具。 它会自动检查不同版本的 Play,针对这些版本编译应用程序,然后运行负载测试。 它将所有结果保存到 Git 存储库中的文件中。 它还会将结果摘要推送到网站。 Prune这个名字来自“Play runner”...

    Corona.SDK.Mobile.Game.Development.Beginners.Guide.2nd.Edition

    This book will take you through the journey of developing games right from installing Corona SDK and creating Hello World as your first app. Going further, you will learn how to script in Lua and ...

    Android.5.Programming.by.Example.178528844X

    Beginning with detailed instructions on how to install and configure the Android SDK, Studio, and Virtual Device Manager, the book moves on to creating a simple, but working, "Hello World" app that ...

    微型JavaWeb框架pippo.zip

    其概念是不是新的 (灵感来自于 Sinatra, Express JS, Play Framework),但目的是提供一个清晰,简单易用的模块化解决方案。 pippo可用于中小型应用,并应用在基于微服务架构的应用程序中。 示例代码:  ...

    MFC+Windows程序设计

    Part I introduces the core tenets of MFC and Windows programming, beginning with a simple "Hello, MFC" application and introducing, one by one, menus, controls, dialog boxes, and other application ...

    VC技术内幕第五版.chm

    Part I introduces the core tenets of MFC and Windows programming, beginning with a simple "Hello, MFC" application and introducing, one by one, menus, controls, dialog boxes, and other application ...

    java 面试题 总结

    2.继承: 继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类...

Global site tag (gtag.js) - Google Analytics