The main concepts
The MVC application model
A Play application follows the MVC architectural pattern applied to the web architecture.
This pattern splits the application into separate layers: the Presentation layer and the Model layer. The Presentation layer is further split into a View and a Controller layer.
- The Model is the domain-specific representation of the information on which the application operates. Domain logic adds ‘meaning’ to raw data (e.g., calculating if today is the user’s birthday, or the totals, taxes, and shipping charges for a shopping cart). Most applications use a persistent storage mechanism such as a database to store data. MVC does not specifically mention the data access layer because it is understood to be underneath, or encapsulated by, the Model.
- The View renders the model into a form suitable for interactions, typically a user interface. Multiple views can exist for a single model, for different purposes. In a Web application the view is usually rendered in a ‘web format’ like HTML, XML or JSON. However there are some cases where the view can be expressed in a binary form, e.g. dynamically rendered chart diagrams.
- The Controller responds to events (typically user actions) and processes them, and may also invoke changes on the model. In a Web application, events are typically HTTP requests: a Controller listens for HTTP requests, extracts relevant data from the ‘event’, such as query string parameters, request headers… and applies changes to the underlying model objects.
In a Play application these three layers are defined in the app directory, each one in a separate Java package.
app/controllers
A Controller is a Java class where each public, static, method is an action. An action is a Java entry point invoked when an HTTP Request is received. The Java code from the Controller class isn’t really object oriented: it’s mainly procedural code. The action method extracts relevant data from the HTTP Request, reads or updates the model objects, and sends back a result which is wrapped into an HTTP Response.
app/models
The domain model object layer is a set of Java classes using all the object-oriented features available from the Java language. It contains data structures and operations on which the application operates. Whenever model objects need to be saved to persistent storage, they may contain some glue artifacts like JPA annotations or SQL statements.
app/views
Most of the application views are generated using an efficient templating system provided by Play. The Controller gets some interesting data from the model layer, and then applies a template to decorate these objects. This package contains HTML, XML, JSON or other template files with special directives used to dynamically generate the model representation.
The request life cycle
The Play framework is fully stateless and only request/response-oriented. All HTTP Requests follow the same path:
- An HTTP Request is received by the framework.
- The Router component tries to find the most specific route able to accept this request. The corresponding action method is then invoked.
- The application code is executed.
- If a complex view needs to be generated, a template file is rendered.
- The result of the action method (HTTP Response code, Content) is then written as an HTTP Response.
The following diagram summarizes the HTTP Request path:
The standard application layout
The layout of a Play application is standardized to keep things as simple as possible.
The app directory
This directory contains all executable artifacts: Java source code and view templates.
Where are my .class files?
Don’t look for compiled Java classes. The framework compiles the Java source code at runtime and only keeps compiled classes in a bytecode cache under the tmp directory. The main executable artifacts in a Play application are the .java source files, not the compiled classes.
There are three standard packages in the app directory, one for each layer of the MVC architectural pattern. You can of course add your own packages like for example a utils package.
In addition, the views package is further organized into sub-packages:
- tags, hosts application tags, e.g. reusable pieces of templates.
- One views folder for each Controller, by convention templates related to each Controller are stored in their own sub-package.
The public directory
Resources stored in the public directory are static assets and are served directly by the Web server.
This directory is split into three standard sub-directories: for images, CSS stylesheets and JavaScript files. You should try to organize your static assets like this to keep all Play applications consistent.
Tip
By default the /public directory is mapped to the /public URL path, but you can easily change that, or even use several directories for your static assets.
The conf directory
The conf directory contains all configuration files for the application.
There are two required configuration files:
- application.conf, the main configuration file for the application. It contains standard configuration options.
- routes, the routes definition file.
If you need to add some configuration options specific to your application, it’s a good idea to add more options to the application.conf file. Configuration options in this file are read programmatically withPlay.configuration.get(“propertyName”).
If any library needs a specific configuration file, try to file it under the conf directory: this directory is included in the Java ClassPath.
You can add additional configuration files to the Play configuration by specifying a file name inapplication.conf as the value of a configuration option that has @include. at the start of the key. For example, if you define additional MIME types in a conf/mime-types.conf
# Web fonts
mimetype.eot = application/vnd.ms-fontobject
mimetype.otf = application/octet-stream
mimetype.ttf = application/octet-stream
mimetype.woff = application/x-font-woff
you can include them by adding the following line to application.conf:
@include.mime = mime-types.conf
The lib directory
This directory contains all standard Java libraries needed by your application. They are automatically added to the Java classpath.
Development life cycle
There are no compilation, packaging or deployment phases while working with Play. However Play implements two distinct environments: DEV mode during the development phase and PROD mode when the application is deployed.
About DEV/PROD modes
You can run an application either in a DEV or PROD mode. You toggle this mode using theapplication.mode configuration property. When run in DEV mode, Play will check for file changes and will handle hot reloading if necessary.
The PROD mode is fully optimized for production: Java sources and templates are compiled once and cached for multiple uses.
Java source code is compiled and loaded at runtime. If a Java source file is modified while the application is running, the source code is recompiled and hot-swapped into the JVM.
If a compilation error occurs, the exact problem is displayed in the browser (in DEV mode only).
Template files are hot-compiled and hot-reloaded too.
Connect a Java debugger
When you run the application in DEV mode, you can connect a Java debugger to the port 8000.
For example, using the NetBeans debugger:
Continuing the discussion
Now that you’ve seen what a Play application is, let’s see how HTTP routing works. The Router is in charge of translating incoming HTTP Requests into actions.
http://play-framework.herokuapp.com/zh/main#mvc
The main concepts的更多相关文章
- [Math Review] Statistics Basics: Main Concepts in Hypothesis Testing
Case Study The case study Physicians' Reactions sought to determine whether physicians spend less ti ...
- Deep Learning in a Nutshell: Core Concepts
Deep Learning in a Nutshell: Core Concepts This post is the first in a series I’ll be writing for Pa ...
- (转) Deep Learning in a Nutshell: Core Concepts
Deep Learning in a Nutshell: Core Concepts Share: Posted on November 3, 2015by Tim Dettmers 7 Comm ...
- 分布式流式处理框架:storm简介 + Storm术语解释
简介: Storm是一个免费开源.分布式.高容错的实时计算系统.它与其他大数据解决方案的不同之处在于它的处理方式.Hadoop 在本质上是一个批处理系统,数据被引入 Hadoop 文件系统 (HDFS ...
- 高难度(3)RenderScript
RenderScript RenderScript is a framework for running computationally intensive tasks at high perform ...
- Python Geospatial Development reading note(1)
chapter 1, Summary: In this chapter, we briefly introduced the Python programming language and the m ...
- bing---iis how to process http request
http://msdn.microsoft.com/en-us/library/ms524901(v=vs.90).aspx http://msdn.microsoft.com/en-us/magaz ...
- java.net.MulticastSocket Example--reference
In this example we are going to explain how to use MulticastSocket in Java, in order to enable a ser ...
- Productivity Improvements for the Entity Framework(实体框架设计)【转】
Background We’ve been hearing a lot of good feedback on the recently released update to the Entity F ...
随机推荐
- HBase 在HDFS 上的目录树
总所周知,HBase 是天生就是架设在 HDFS 上,在这个分布式文件系统中,HBase 是怎么去构建自己的目录树的呢? 这里只介绍系统级别的目录树. 一.0.94-cdh4.2.1版本 系 ...
- Linux高级编程--11.信号
基本概念 信号在Linux中是一个比较常见的概念,例如我们按Ctrl+C中断前台进程,通过Kill命令结束进程都是通过信号实现的.下面就以Ctrl+C为例简单的说明信号的处理流程: 用户按下Ctrl- ...
- 事务复制中的snapshot
Snapshot agent读取article的信息,将article的内容和脚本放置到snapshot文件夹中: 接下来distribution agent会读取这些快照文件,传输到订阅,完 ...
- 玩转PowerShell第二节——【利用PsExec进行远程调用】-技术&分享
概述 PowerShell用的最多的地方就是远程调用,在远程机器上执行脚本,监控远程机器的状态,如NLB状态,EventLog,SqlServer DataBase状态等. 本篇将讲到用PsExec. ...
- php -- php读取sqlserver2005的数据实现分页查询
--php5.2 --sqlserver2005 php读取sqlserver多条数据时,有时因为数据太多,需要进行分页查询. 例如,按价格的顺序,读取同一类型的产品的第30到第48条记录 $star ...
- tiny4412的中断资源连接关系示意图
在tiny4412的设备树中可以发现,中断资源是以树的形式呈现的,下面是我画的一张图,大致描述了tiny4412上中断资源的连接关系. 可以到http://pan.baidu.com/s/1ge0sz ...
- Hadoop入门进阶课程13--Chukwa介绍与安装部署
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan ...
- [git]解决:git config --global push.default matching
解决:git config --global push.default matching 这个警告的意思是:需要设置默认push的分支,所以设置好全局push的默认分支就好了.命令如下: 在有git目 ...
- C++ 多态的实现原理与内存模型
多态在C++中是一个重要的概念,通过虚函数机制实现了在程序运行时根据调用对象来判断具体调用哪一个函数. 具体来说就是:父类类别的指针(或者引用)指向其子类的实例,然后通过父类的指针(或者引用)调用实际 ...
- ActiveMQ学习(四)——应用程序接口
在 Java 里有 JMS的多个实现.其中 apache 下的 ActiveMQ就是不错的选择. 用 ActiveMQ最好还是了解下 JMS JMS 公共 点对点域 发布/订阅域 Connection ...