参考文献: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:还没写完,以后再继续。


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

japid-controller自动绑定的数据类型相关推荐

  1. 【SSM - SpringMVC篇】03 - SpringMVC的参数绑定 - 参数自动绑定 - javabean对象参数绑 - 嵌套bean参数绑定

    文章目录 SpringMVC的参数绑定 1 传统方式进行参数传递 2 通过SpringMVC的参数绑定实现[***用这个就行] 2.1 SpringMVC的基本数据类型的参数绑定 2.1.1 方法形式 ...

  2. 【邹健】Asp.net MVC Fckeditor的扩展(支持PV3及自动绑定)

    Asp.net MVC Fckeditor的扩展(支持PV3及自动绑定) function StorePage(){d=document;t=d.selection?(d.selection.type ...

  3. Gin 框架学习笔记(02)— 参数自动绑定到结构体

    参数绑定模型可以将请求体自动绑定到结构体中,目前支持绑定的请求类型有 JSON .XML .YAML 和标准表单 form数据 foo=bar&boo=baz 等.换句话说,只要定义好结构体, ...

  4. 使用Ext Form自动绑定Html中的Form元素

    2019独角兽企业重金招聘Python工程师标准>>> Java代码   //把ext 对象绑定在Html Form元素时的ext属性中 Ext.override(Ext.Compo ...

  5. spinbox 上下箭头事件_[React] 3 - 自动绑定 (事件绑定)

    1. 自动绑定 (事件绑定) 绑定this: React为什么要这么做? 是js中this绑定方式决定的,和react工作方式无关. 函数处理, 是作为回调传递的,这样就会丢失了上下文, 所以this ...

  6. JSBing-js自动绑定C++

    什么是JSB JSB是JavaScript binding的简称.Cocos2d-JS中使用的javascript引擎是Mozilla 的spidermonkey,而JSB绑定的目的就是让javasc ...

  7. mvvm 自动绑定_ZK的实际应用:MVVM –表单绑定

    mvvm 自动绑定 这是我们从头开始构建ZK应用程序的第二集. 上一篇文章介绍了使用MVVM将数据加载和呈现到表中. 在本文中,我们将向您介绍ZK MVVM的表单绑定. 目的 我们将构建一个" ...

  8. react回调函数_React中的回调中自动绑定ES6类函数

    在使用ES6类的React组件时,您必须遇到这种现象,必须显式绑定类函数,然后将其传递给诸如onClick.例如,采用以下示例. import React from 'react';class MyC ...

  9. java resultset 赋值_ResultSet数据自动绑定到JavaBean中 | 学步园

    由于需要用jdbc写点程序,不使用Hibernate , mybatis等框架,在设置结果集到bean时相当麻烦,所以写了一个专门完成此功能的类 package com.ehl.dc.authoriz ...

最新文章

  1. Jquery 之Ajax方法$.get() 的运用,扩展链接模型的天地
  2. 4、kubernetes 集群管理工具 kubectl
  3. Docker导入导出镜像两种方式
  4. jquery mobile app案例_讲座预告 | 运动app内的个人成就、运动轨迹和社交分享对体能提升的长期效应研究...
  5. Elasticsearch实战篇——Spring Boot整合ElasticSearch
  6. 新闻网大数据实时分析可视化系统项目——7、Kafka分布式集群部署
  7. java线程实现排序_【多线程实现快速排序】
  8. MySQL CookBook 学习笔记-01
  9. string转为char数组_StringBuilder的区别是什么?String是不可变?一点课堂(多岸学院)...
  10. cad 打开硬件加速卡_CAD卡顿?电脑带不动?几个简单实用的方法,解决CAD运行卡顿问题...
  11. eclipse plugins
  12. Ionic 学习笔记
  13. 剑指Offer_62_二叉搜索树的第k个结点
  14. 走进 JDK 之谈谈字符串拼接
  15. C# 替换Word文档中的书签内容【详细步骤】
  16. 由内而外全面造就自己(三)
  17. 软件工程 3:模块化设计
  18. Android studio中的警告Hardcoded text
  19. 在c语言中用switch编写月份,用switch语句从键盘输入一个月份,自动显示该月份的英文名称!C语言的...
  20. 计算机前进后退灰的无法按,《微机试题2012新题.xls》-支持高清全文免费浏览-max文档...

热门文章

  1. React是如何在后台运行的
  2. php http面向对象编程实例,PHP面向对象编程——PHP对象引用实例代码
  3. 用Git上传删除本地项目到gitlab---图文解析
  4. 工具库 --- Validator (JS正则)
  5. 分布式RPC实践--Dubbo基础篇
  6. unity加载ab后,场景shader不起效问题(物件表现黑色)
  7. dstat用法;利用awk求dstat所有列每列的和;linux系统监控
  8. Android自定义控件NumberCircleProgressBar(圆形进度条)的实现
  9. Podfile 常见语法
  10. 一个基于J2EE的web应用程序运行起来需要什么?