Play Framework 框架 验证分析和总结.docx

Play Framework 框架 验证分析和总结.docx

  1. 1、本文档共15页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
使用 Play 验证 HTTP 数据 Validating HTTP data with Play 验证确保数据有确定的值,或者符合某种特殊的需求,你可以在模型被保存进数据库之前使用验证去核实你的模型,或者直接在 HTTP 参数中使用它们去验证一 个简单的 form 表单。 Validations ensure that the data has certain values or meets specific requirements. You can use validation to verify that your models are correct before saving them to the database, or use them directly on HTTP parameters to validate a simple form. 它们怎样工作? How does it work? 每一次的请求使用它自己的验证去收集错误。在控制器里,你可以直接使用 Validation 变量,你也可以直接访问 play.data.validation.Validation 类下的 API 中的静态方法。 Each request has it own **Validation** object which collects errors. From a controller, you access it directly using the **validation** variable. You can still access a subset of the API using the **play.data.validation.Validation** class’ static methods. 验证对象包含一个集合 play.data.validation.Error 对象,每一个错误有 2 个属性。 The validation object maintains a collection of **play.data.validation.Error** objects. Each error has two properties: key,它帮助你决定哪一个数据项引发的错误,key 的值可以被定义但是当 Play 产生错误时,它使用默认的约定,遵循 Java 变量的名称。 The **key**. This helps you to determine which data element caused the error. The key value can be set arbitrarily but when Play generates errors, it uses default conventions that follow the Java variables’ names. message,它包含了错误的文字描述,message 可以是文本信息,或者从错误集合里(典型的是为了国际化支持)参考一个 key。 The **message**. This contains the error’s textual description. The message can be a plain message or refer to a key from a message bundle (typically for internationalization support). 下面我们看一下怎样去验证一个简单的 HTTP 参数。 Let’s see how to validate a simple HTTP parameter: public static void hello(String name) { validation.required(name); ... } 这段代码检查 name 变量被正确的设置了,如果不是的话,相应的信息会被增加到当前的错误集合中去。 This code checks that the name variable is correctly set. If not, the corresponding error is added to the current errors collection. 你可以重复这个操作去验证每一个你需要的变量。 You can repeat this operation for each validation you need: public static void hello(String name, Integer age) { validation.required(name); validation.requir

文档评论(0)

tianya189 + 关注
官方认证
内容提供者

该用户很懒,什么也没介绍

认证主体阳新县融易互联网技术工作室
IP属地上海
统一社会信用代码/组织机构代码
92420222MA4ELHM75D

1亿VIP精品文档

相关文档