博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
japid-controller自动绑定的数据类型
阅读量:4633 次
发布时间:2019-06-09

本文共 3577 字,大约阅读时间需要 11 分钟。

  参考文献: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 .

 

ps:还没写完,以后再继续。

转载于:https://www.cnblogs.com/timelyxyz/archive/2012/05/18/2323966.html

你可能感兴趣的文章
kali 插耳机没声音
查看>>
Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings
查看>>
如何巧妙使用ZBrush中的Image Plane插件
查看>>
windows安装theano和keras
查看>>
141. Linked List Cycle
查看>>
169. Majority Element
查看>>
51NOD 1087 1 10 100 1000
查看>>
谈谈javascript语法里一些难点问题(一)
查看>>
jQuery 遍历同胞(siblings)
查看>>
小萌库一周电影大合集
查看>>
Linux 之根目录介绍
查看>>
iOS学习之代码块(Block)
查看>>
十三种迹象表明百度取消新闻源势在必行的趋势
查看>>
vue初识(一)
查看>>
jQuery版本升级踩坑大全
查看>>
nginx+tomcat实现Windows系统下的负载均衡搭建的案例
查看>>
checkstyle.xml Code Style for Eclipse
查看>>
Docker2之Service
查看>>
总结技术人生的第一个“五年计划”
查看>>
linux (一)
查看>>