參考文獻(xiàn):http://www.playframework.org/documentation/1.2.3/controllers
當(dāng)參數(shù)名和HTTP請求中的參數(shù)名(即界面中的name)相同時(shí),后臺Controller可以直接獲取該變量的值。變量分兩大類:
1. Simple types
所有的基本數(shù)據(jù)類型和一些常用的Java類型可以被自動(dòng)綁定
int, long, boolean, char, byte, float, double, Integer, Long, Boolean, Char, Byte, Float, Double, String
以上數(shù)據(jù)類型可以被自動(dòng)綁定,當(dāng)參數(shù)為丟失時(shí),默認(rèn)會將變量的值賦為:null或0。
2. Date
當(dāng)時(shí)間對象以一下格式展現(xiàn)時(shí),可以被自動(dòng)綁定到后臺:
- yyyy-MM-dd'T'hh:mm:ss'Z' //ISO8601 + timezone
- yyyy-MM-dd'T'hh:mm:ss" //ISO8601
- yyyy-MM-dd
- yyyyMMdd'T'hhmmss
- yyyyMMddhhmmss
- dd'/'MM'/'yyyy
- dd-MM-yyyy
- ddMMyyyy
- MMddyy
- MM-dd-yy
- MM'/'dd'/'yy
引用@As 注釋,可以直接定義時(shí)間格式。for example:
archives?from=21/12/1980
public static void articlesSince(@As("dd/MM/yyyy") Date from) {
List<Article> articles = Article.findBy("date >= ?", from);
render(articles);
}
另外也可以根據(jù)語言來格式化時(shí)間。See as follows:
public static void articlesSince(@As(lang={"fr,de","*"}, value={"dd-MM-yyyy","MM-dd-yyyy"}) Date from) {
List<Article> articles = Article.findBy("date >= ?", from);
render(articles);
}
在這個(gè)例子中,我們將French和German語言對應(yīng)的時(shí)間格式設(shè)置為dd-MM-yyyy和MM-dd-yyyy。需要注意的是,語言必須用都好分隔開,value和lang的個(gè)數(shù)要匹配。
如果@As注釋沒寫的話,時(shí)間會按照默認(rèn)格式來轉(zhuǎn)化。
3.Calendar
Calendar跟Date類似,它使用的注釋是@Bind。
4.Arrays or collections of supported types
所有被支持的類型都可以作以Array的形式被綁定獲取到。for example:
public static void show(Long[] id){...}
public static void show(List<Long> id){...}
public static void show(Set<Long> id){...}
Play也支持像是Map<String, String>這樣子的特殊例子:
public static void show(Map<String, String> client){...}
在上面的這個(gè)例子中,傳入的語句如下:
?client.name=John&client.phone=111-1111&client.phone=222-222
此時(shí)后臺獲取到一個(gè)map元素,第一個(gè)元素的key為name,值為John,第二個(gè)元素的key為phone,值為111-1111,222-2222.
5.POJO object binding
play同樣支持自動(dòng)綁定任何model,只要該對象遵守相同的命名規(guī)則。for example:
public static void create(Client client){
client.save();
show(client);
}
在頁面端,一個(gè)保存client的action需要規(guī)定如下:
?client.name=Zenexity&client.email=contact&zenexity.fr
play可以創(chuàng)建一個(gè)Client對象,并且從HTTP請求中讀取相關(guān)的屬性值賦到Client對象上。一些不能解決/讀取的參數(shù)會被play安全的忽略掉,類型不匹配也會被自動(dòng)忽略。
參數(shù)綁定也可以遞歸的進(jìn)行,只要你列出完整的參數(shù)列表:
?client.name=Zenexity
&client.address.street=64+rue+taitbout
&client.address.zip=75009
&client.address.country=France
有時(shí)候?yàn)榱烁聦ο罅斜恚瑫褂靡粋€(gè)存放了對象id的數(shù)組。For example,假設(shè)我們已經(jīng)有了一個(gè)Customer的對象列表,聲明List Customer customers
,為了更新Customers,我們需要提供如下一串String:
?client.customers[0].id=123
&client.customers[1].id=456
&client.customers[2].id=789
6.JPA object binding
7.File
File upload is easy with Play. Use a multipart/form-data
encoded request to post files to the server, and then use the java.io.File
type to retrieve the file object:
public static void create(String comment, File attachment) { String s3Key = S3.post(attachment); Document doc = new Document(comment, s3Key); doc.save(); show(doc.id); }
The created file has the same name as the original file. It’s stored in a temporary directory and deleted at the end of the request. So you have to copy it in a safe directory or it will be lost.
The uploaded file’s MIME type should normally be specified by the HTTP request’s Content-type
header. However, when uploading files from a web browser, this might not happen for uncommon types. In this case, you can map the file name’s extension to a MIME type, using the play.libs.MimeTypes
class.
String mimeType = MimeTypes.getContentType(attachment.getName());
The play.libs.MimeTypes
class looks up the MIME type for the given file name’s extension in the file $PLAY_HOME/framework/src/play/libs/mime-types.properties
You can also add your own types using the Custom MIME types configuration.
ps:還沒寫完,以后再繼續(xù)。