The best way to introduce the Play framework is to give a complete demonstration step to show you how simple Play is. This demo uses the latest Play 2.0 Beta.
This article is carried out in a Linux environment. If you are using Windows, there will be some minor differences, such as path and environment variable settings. Please do it yourself.
Let's get started:
1. Download and install
- $ wget http://download.playframework.org/releases/play-2.0-beta.zip
- $ unzip -q play-2.0-beta.zip
- $ export PATH=$PATH:`pwd`/play-2.0-beta
2. Create a New Application
- $ play new tasks
-
- What is the application name?
- > tasks
-
- Which template do you want to use for this new application?
-
- 1 - Create a simple Scala application
- 2 - Create a simple Java application
- 3 - Create an empty project
-
- > 2
Let's see what files are generated?
- $ find tasks -type f
- tasks/.gitignore
- tasks/app/controllers/Application.java
- tasks/app/views/index.scala.html
- tasks/app/views/main.scala.html
- tasks/conf/application.conf
- tasks/conf/routes
- tasks/project/build.properties
- tasks/project/Build.scala
- tasks/project/plugins.sbt
- tasks/public/images/favicon.png
- tasks/public/javascripts/jquery-1.6.4.min.js
- tasks/public/stylesheets/main.css
3. Run the program
- $ cd tasks
- $ play run
Then you can open your browser to access http: // localhost: 9000. What do you see?
Next we will add some dynamic data
Edit the app/views/index.scala.html file with the following content:
- @(items: String)
-
- @main("Tasks") {
-
- }
Edit app/controllers/Application. java and modify the index () method. The Code is as follows: we intentionally enter a semicolon less)
- return ok(index.render("Things"))
Refresh the http: // localhost: 9000 page and a template compilation error: not found: value item will be reported.
This compilation error indicates that template parameters must be declared.
On the console, enter Ctrl D to stop the Play program, restart Play control and compile the program:
- $ play
- [tasks] $ compile
You can also find the template compilation error if you have not run the application.
Start the application again:
- [tasks] $ run
Fix this error in app/views/index.scala.html
Refresh the page and a Java compilation error is displayed: ';' expected
Add the missing semicolon in app/controllers/Application. java.
Refresh the page again and the Things title is displayed.
In public/stylesheets/main.css, we add some css code:
- body { font-family:"Helvetica Neue"; padding:2em; background: #B2EB5A url("/assets/images/play20header.png") no-repeat top center ; }
- body:before { content:'Play 2.0 task list demo'; color:rgba(255,255,255,0.7); font-size:150%; text-transform:uppercase; letter-spacing:0.4em; }
- ul { padding:0; list-style:none; }
- li, form { width:30em; background:white; padding:1em; border:1px solid #ccc; border-radius:0.5em; margin:1em 0; position:relative; min-height:1.2em; }
- li a { text-decoration:none; color:transparent; position:absolute; top:1em; right:1em; }
- li a:after { content:'❎'; color:#aaa; font-size:120%; font-weight:bold; }
- form * { font-size:120%; }
- input { width:16em; }
- button { cursor:pointer; color: white; background-color: #3D6F04; background-image: -webkit-linear-gradient(top, #5AA706, #3D6F04); text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); border: 1px solid #CCC; border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25); border-radius:4px; }
- p.error { margin:0; color:#c00; }
In app/controllers/Application. java, we use a string items method parameter to replace the Things string
In conf/routes, replace the first route information with a lowercase string)
- GET / controllers.Application.index(i: string)
Enable http: // localhost: 9000 /? Items = Tasks:
The routes file is compiled, and HTTP parameters must be declared. HTTP parameter names do not have to match the action method names.
Correct this error in conf/routes:
- GET / controllers.Application.index(i: String)
Refresh the page
Undo the changes in app/controllers/Application. java and delete the index method parameters:
- public static Result index(final String items) {
- return ok(index.render(items));
- }
Undo the changes in conf/routes and delete the parameters:
- GET / controllers.Application.index()
The following describes how to use the IDEA environment and form processing and verification. For more information, see the original article.
Connection: http://www.oschina.net/question/12_33439