Complete demo process record of Play 2.0

Source: Internet
Author: User

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

 
 
  1. $ wget http://download.playframework.org/releases/play-2.0-beta.zip  
  2. $ unzip -q play-2.0-beta.zip  
  3. $ export PATH=$PATH:`pwd`/play-2.0-beta 

2. Create a New Application

 
 
  1. $ play new tasks  
  2.    
  3. What is the application name?  
  4. > tasks  
  5.    
  6. Which template do you want to use for this new application?  
  7.    
  8.   1 - Create a simple Scala application  
  9.   2 - Create a simple Java application  
  10.   3 - Create an empty project  
  11.    
  12. > 2 

Let's see what files are generated?

 
 
  1. $ find tasks -type f  
  2. tasks/.gitignore  
  3. tasks/app/controllers/Application.java  
  4. tasks/app/views/index.scala.html  
  5. tasks/app/views/main.scala.html  
  6. tasks/conf/application.conf  
  7. tasks/conf/routes  
  8. tasks/project/build.properties  
  9. tasks/project/Build.scala  
  10. tasks/project/plugins.sbt  
  11. tasks/public/images/favicon.png  
  12. tasks/public/javascripts/jquery-1.6.4.min.js  
  13. tasks/public/stylesheets/main.css 

3. Run the program

 
 
  1. $ cd tasks  
  2. $ 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:

 
 
  1. @(items: String)  
  2.    
  3. @main("Tasks") {  
  4.     

Edit app/controllers/Application. java and modify the index () method. The Code is as follows: we intentionally enter a semicolon less)

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

 
 
  1. $ play  
  2. [tasks] $ compile 

You can also find the template compilation error if you have not run the application.

Start the application again:

 
 
  1. [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:

 
 
  1. body { font-family:"Helvetica Neue"; padding:2em; background: #B2EB5A url("/assets/images/play20header.png") no-repeat top center ; }  
  2. 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; }  
  3. ul { padding:0; list-style:none; }  
  4. 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; }  
  5. li a { text-decoration:none; color:transparent; position:absolute; top:1em; right:1em; }  
  6. li a:after { content:'❎'; color:#aaa; font-size:120%; font-weight:bold; }  
  7. form * { font-size:120%; }  
  8. input { width:16em; }  
  9. 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; }  
  10. 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)

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

 
 
  1. GET /   controllers.Application.index(i: String) 

Refresh the page

Undo the changes in app/controllers/Application. java and delete the index method parameters:

 
 
  1. public static Result index(final String items) {  
  2.    return ok(index.render(items));  

Undo the changes in conf/routes and delete the parameters:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.