参考文献:http://www.playframework.org/documentation/1.2.3/controllers
当参数名和HTTP请求中的参数名(即界面中的name)相同时,后台Controller可以直接获取该变量的值。变量分两大类:
所有的基本数据类型和一些常用的Java类型可以被自动绑定
int, long, boolean, char, byte, float, double, Integer, Long, Boolean, Char, Byte, Float, Double, String
以上数据类型可以被自动绑定,当参数为丢失时,默认会将变量的值赋为:null或0。
当时间对象以一下格式展现时,可以被自动绑定到后台:
- 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注释没写的话,时间会按照默认格式来转化。
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.
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
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:还没写完,以后再继续。