`

Play framework 2.0 -第一个应用程序

 
阅读更多

所有章节的翻译到此结束,实现一个小应用作为结束!

 

按照play2.0官网的例子编写第一个play Application : todolist

 

步骤如下(详细的步骤及解释请查看原文):

 

先编写控制器如下:

 


aplication.java

package controllers;

import play.*;
import play.data.*;
import play.mvc.*;
import models.*;
import views.html.*;

public class Application extends Controller {

  static Form<Task> taskForm = form(Task.class);

  public static Result index() {
    //return ok(index.render("Your new application is ready."));
	return redirect(routes.Application.tasks());
  }
  
  public static Result tasks() {
    return ok(
    		views.html.index.render(Task.all(), taskForm)
	);
  }
  
  public static Result newTask() {
	  Form<Task> filledForm = taskForm.bindFromRequest();
	  if(filledForm.hasErrors()) {
	    return badRequest(
	      views.html.index.render(Task.all(), filledForm)
	    );
	  } else {
	    Task.create(filledForm.get());
	    return redirect(routes.Application.tasks());  
	  }
  }
  
  public static Result deleteTask(Long id) {
	  Task.delete(id);
	  return redirect(routes.Application.tasks());
  }
}
 

修改routes:

 


# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index()

# Tasks          
GET     /tasks                  controllers.Application.tasks()
POST    /tasks                  controllers.Application.newTask()
POST    /tasks/:id/delete       controllers.Application.deleteTask(id: Long)

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)
 

编写一个model(用到了Ebean,需要在application.conf中配置):

 

Task.java
package models;

import java.util.*;

import play.db.ebean.*;
import play.data.validation.Constraints.*;

import javax.persistence.*;

@Entity
public class Task extends Model{
  
  @Id
  public Long id;
  
  @Required
  public String label;
  
  public static Finder<Long,Task> find = new Finder(
		    Long.class, Task.class
	);
  
  public static List<Task> all() {
	  return find.all();
	}

	public static void create(Task task) {
	  task.save();
	}

	public static void delete(Long id) {
	  find.ref(id).delete();
	}
    
}
 

 

编写views:

 

main.scala.html
@(title: String)(content: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
        <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
    </head>
    <body>
        @content
    </body>
</html>
 

 

index.scala.html

 


@(tasks: List[Task], taskForm: Form[Task])

@import helper._

@main("Todo list") {
    
    <h1>@tasks.size() task(s)</h1>
    
    <ul>
        @for(task <- tasks) {
            <li>
                @task.label
                
                @form(routes.Application.deleteTask(task.id)) {
                    <input type="submit" value="Delete">
                }
            </li>
        }
    </ul>
    
    <h2>Add a new task</h2>
    
    @form(routes.Application.newTask()) {
        
        @inputText(taskForm("label")) 
        
        <input type="submit" value="Create">
        
    }
    
}

 

最后改一下配置:

 


# This is the main configuration file for the application.
# ~~~~~

# Secret key
# ~~~~~
# The secret key is used to secure cryptographics functions.
# If you deploy your application to several instances be sure to use the same key!
application.secret="@8DeN:VCjdHS7g2Y=L0et:x/e8DseMHvPLvEnYqRy@R[o2V[f547@wyV/o^K@56i"

# The application languages
# ~~~~~ 中文
application.langs="zh"

# Global object class
# ~~~~~
# Define the Global object class for this application.
# Default to Global in the root package.
# global=Global

# Database configuration
# ~~~~~ 
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#使用h2
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
# db.default.user=sa
# db.default.password=
#
# You can expose this datasource via JNDI if needed (Useful for JPA)
# db.default.jndiName=DefaultDS

# Evolutions
# ~~~~~
# You can disable evolutions if needed
# evolutionplugin=disabled

# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
# 使用Ebean
ebean.default="models.*"

# Logger
# ~~~~~
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .

# Root logger:
logger.root=ERROR

# Logger used by the framework:
logger.play=INFO

# Logger provided to your application:
logger.application=DEBUG
 

 

现在play run一下就可以看到效果了!

 

附件是程序源代码。

3
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics