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:

  1. An HTTP Request is received by the framework.
  2. The Router component tries to find the most specific route able to accept this request. The corresponding action method is then invoked.
  3. The application code is executed.
  4. If a complex view needs to be generated, a template file is rendered.
  5. 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的更多相关文章

  1. [Math Review] Statistics Basics: Main Concepts in Hypothesis Testing

    Case Study The case study Physicians' Reactions sought to determine whether physicians spend less ti ...

  2. 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 ...

  3. (转) Deep Learning in a Nutshell: Core Concepts

    Deep Learning in a Nutshell: Core Concepts Share:   Posted on November 3, 2015by Tim Dettmers 7 Comm ...

  4. 分布式流式处理框架:storm简介 + Storm术语解释

    简介: Storm是一个免费开源.分布式.高容错的实时计算系统.它与其他大数据解决方案的不同之处在于它的处理方式.Hadoop 在本质上是一个批处理系统,数据被引入 Hadoop 文件系统 (HDFS ...

  5. 高难度(3)RenderScript

    RenderScript RenderScript is a framework for running computationally intensive tasks at high perform ...

  6. Python Geospatial Development reading note(1)

    chapter 1, Summary: In this chapter, we briefly introduced the Python programming language and the m ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Oracle数据库入门——高水位线详解

    一.什么是水线(High Water Mark)? 所有的oracle段(segments,在此,为了理解方便,建议把segment作为表的一个同义词) 都有一个在段内容纳数据的上限,我们把这个上限称 ...

  2. jquery $.proxy使用

    在某些情况下,我们调用Javascript函数时候,this指针并不一定是我们所期望的那个.例如: //正常的this使用 $('#myElement').click(function() { // ...

  3. js两种定义函数、继承方式及区别

    一:js两种定义函数的方式及区别 1:函数声明: function sayA() { alert("i am A"); } 2:函数表达式: var sayB = function ...

  4. Netty4 中的内存管理

    在Netty4中引入了新的内存管理机制极大地提升其性能,本文将对该内在管理机制进行剖析. 这里有篇文章讲述了在推特(Twitter)内部 使用Netty的状况以及Netty4所带来的性能收益. 在分析 ...

  5. JavaScript中的String对象

        String对象提供的方法用于处理字符串及字符. 常用的一些方法: charAt(index):返回字符串中index处的字符. indexOf(searchValue,[fromIndex] ...

  6. 基于安卓高仿how-old.net实现人脸识别估算年龄与性别

    前几段微软推出的大数据人脸识别年龄应用how-old.net在微博火了一把,它可以通过照片快速获得照片上人物的年龄,系统会对瞳孔.眼角.鼻子等27个“面部地标点"展开分析,进而得出你的“颜龄 ...

  7. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  8. emacs工程管理,cedet ede插件自动构建Make,Automake

    鉴于自己一直都是在做客户端开发方面的工作,服务端很多知识都随着时间淡忘了,最近有一个计划,用一些时间补一下基础.所以早上很早就起床,花了一点时间大致浏览了一下BSD socket的相关API,然后用G ...

  9. eclispe使用外部tomcat总结

    1. 配置tomcat Servers-->new 配置tomcat的路径,即可 2. 增加/移除application 注意:移除application时请使用"clean" ...

  10. Understanding CMS GC Logs--转载

    原文地址:https://blogs.oracle.com/poonam/entry/understanding_cms_gc_logs Understanding CMS GC Logs By Po ...