參考文獻:http://www.playframework.org/documentation/1.2.3/controllers
當參數名和HTTP請求中的參數名(即界面中的name)相同時,后臺Controller可以直接獲取該變量的值。變量分兩大類:
1. Simple types
所有的基本數據類型和一些常用的Java類型可以被自動綁定
int, long, boolean, char, byte, float, double, Integer, Long, Boolean, Char, Byte, Float, Double, String
以上數據類型可以被自動綁定,當參數為丟失時,默認會將變量的值賦為:null或0。
2. Date
當時間對象以一下格式展現時,可以被自動綁定到后臺:
- 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 注釋,可以直接定義時間格式。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);
}
另外也可以根據語言來格式化時間。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);
}
在這個例子中,我們將French和German語言對應的時間格式設置為dd-MM-yyyy和MM-dd-yyyy。需要注意的是,語言必須用都好分隔開,value和lang的個數要匹配。
如果@As注釋沒寫的話,時間會按照默認格式來轉化。
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){...}
在上面的這個例子中,傳入的語句如下:
?client.name=John&client.phone=111-1111&client.phone=222-222
此時后臺獲取到一個map元素,第一個元素的key為name,值為John,第二個元素的key為phone,值為111-1111,222-2222.
5.POJO object binding
play同樣支持自動綁定任何model,只要該對象遵守相同的命名規則。for example:
public static void create(Client client){
client.save();
show(client);
}
在頁面端,一個保存client的action需要規定如下:
?client.name=Zenexity&client.email=contact&zenexity.fr
play可以創建一個Client對象,并且從HTTP請求中讀取相關的屬性值賦到Client對象上。一些不能解決/讀取的參數會被play安全的忽略掉,類型不匹配也會被自動忽略。
參數綁定也可以遞歸的進行,只要你列出完整的參數列表:
?client.name=Zenexity
&client.address.street=64+rue+taitbout
&client.address.zip=75009
&client.address.country=France
有時候為了更新對象列表,會使用一個存放了對象id的數組。For example,假設我們已經有了一個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:還沒寫完,以后再繼續。