PLAY框架开发中文手册2010-03-19 11:16PLAY开发中文手册
PLAY开发实战中文手册
PLAY功能中文手册
PLAY中文API手册
有需要的请MSN联系:
[email protected]
Controllers
执行模板:
public class Clients extends Controller {
public static void index() {
render();
}
}
路径:app/views/Clients/index.html
app/views/Controller/(action names) .html
数据添加到模板范围:
using the renderArgs object。使用renderArgs对象。
public class Clients extends Controller {
public static void show(Long id) {
Client client = Client.findById(id);
renderArgs.put("client", client);
render();
}
}
模板使用:<h1>Client ${client.name}</h1>
更简单的办法:
直接用render(...)方法。
public static void show(Long id) {
Client client = Client.findById(id);
render(client);
}
进一步:
public static void show(Long id) {
Client client = Client.findById(id);
render(id, client);
}
指定另一个模板:
不希望使用默认模板,使用自己指定的模板。
public static void show(Long id) {
Client client = Client.findById(id);
render("Clients/showClient.html", id, client);
}
重定向到另一个URL:
使用redirect(...)。
public static void index() {
redirect("http://www.zenexity.fr");
}
不支持:Forward。(因为没有servlet)注:
一个HTTP请求只能调用一个动作,如果你需要调用另一个动作,重定向浏览器URL,调用另一个动作。
拦截:
@Before @After @Finally
These methods have to be static but not public.
添加更多拦截器,使用@With。
测试@Test
An HTTP request:
1.The URI path: in /clients/1541, 1541 is dynamic part of the URI Pattern.
2.The Query String: /clients?id=1541.
3.The request body: if the request was sent from an HTML form, the request body contains the form data encoded as x-www-urlform-encoded.
package controllers;
import models.Client;
import play.mvc.Controller;
public class Clients extends Controller {
public static void show(Long id) {
Client client = Client.findById(id);
render(client);
}
public static void delete(Long id) {
Client client = Client.findById(id);
client.delete();
}
}
Each public, static method in a Controller is called an action. The signature for an action method is always :
public static void action_name(params...);
render(...)是一个执行结果的方法,并显示一个模板。
import play.mvc.Controller;
params
public static void show() {
String id = params.get("id");
String[] names = params.getAll("names");
}
public static void show() {
Long id = params.get("id", Long.class);
}
public static void show(Long[] id) {
for(String anId : id) {
System.out.println(id);
}
}
public static void show(List<Long> id) {
for(String anId : id) {
System.out.println(id);
}
}
Models域对象模型
这一层是商业软件的核心。该模型在一个应用程序的中心地位。负责代表的业务概念,有关业务情况的信息和业务规则。