



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精品使用 Play 验证 HTTP 数据Validating HTTP data with Play验证确保数据有确定的值, 或者符合某种特殊的需求, 你可以在模型被保存进数据库之前使用验证去核实你的模型, 或者直接在 HTTP 参数中使用它们去验证一个简单的 form 表单。Validations ensure that the data has certain values or meets specificrequirements. You can use validation to verify that your models arecorrect before saving the
2、m to the database, or use them directly onHTTP 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. Froma controller, you acce
3、ss it directly using the *validation* variable. You感谢下载载精品can still access a subset of the API using the*play.data.validation.Validation* classstatic methods.验证对象包含一个集合play.data.validation.Error对象,每一个错误有2 个属性。The validation object maintains a collection of*play.data.validation.Error* objects. Each e
4、rror 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 generateserrors, it uses default conventions that follow the Java variablesnam
5、es.message ,它包含了错误的文字描述,message 可以是文本信息,或者从错误集合里 (典型的是为了国际化支持)参考一个 key 。* The *message*. This contains the errors textual description. Themessage can be a plain message or refer to a key from a message bundle(typically for internationalization support).感谢下载载精品下面我们看一下怎样去验证一个简单的HTTP 参数。Let s see how t
6、o 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, thecorresponding error is added to the current errors collection.你可以重复这个操作去验证每一个你需要的变
7、量。You can repeat this operation for each validation you need:public static void hello(String name, Integer age) 感谢下载载精品validation.required(name);validation.required(age);validation.min(age, 0);.重新得到错误信息Retrieving error messages在每一个验证结束,你可以检查是否错误都被创建并显示出来了。At the end of the validation you can check i
8、f any errors have beencreated and display them:public static void hello(String name, Integer age) validation.required(name);validation.required(age);validation.min(age, 0);感谢下载载精品if(validation.hasErrors() for(Error error : validation.errors() System.out.println(error.message();假设 name 和 age 是 null ,
9、那么将会显示出:Assuming that name and age are null, this would display:name is requiredage is required默认的消息是 key 和 message 集合中 key 一致的,所以在conf/messages文件中你可以看到:感谢下载载精品Default messages are keys that refer to the message bundle. So in the*conf/messages* file you will have:validation.required=%s is required你可
10、以改变这些默认的消息,然后再每一个项目中覆盖它,%s 占位符会被错误的 key 所替代,你可以使用error.message(String field)方法覆盖它。You can change this default message and override it for each applicationlanguage. The *%s* placeholder will be replaced by the error key. Youcan override using the *error.message(String field)* method.例如:For example:Er
11、ror error = validation.required(name).error;if(error != null) System.out.println(error.message("The name");感谢下载载精品你还可以为每一次检查明确指定不同的信息。You can also specify a different message for each check:Error error = validation.required(name).message("Fill the name!").error;if(error != null)
12、System.out.println(error.message();再模板中显示错误信息Displaying errors in the template再大多数情况下,你想让错误消息显示在视图模板中,你可以在模板中使用errors 对象使用它们,一些tag 帮助你显示这些错误。In most cases you want to display the error messages in the viewtemplate. You can access them in the template using the *errors*object. Some tags help you to d
13、isplay the errors:感谢下载载精品让我们看个例子。Let s see a sample:public static void hello(String name, Integer age) validation.required(name);validation.required(age);validation.min(age, 0);render(name, age);现在是模板。and now the template:#ifErrors<h1>Oops.</h1>感谢下载载精品#errors<li>$error</li>#/
14、errors#/ifErrors#elseHello $name, you are $age.#/else但是在实际的应用中, 你想显示原先的form 表单。所以你将有 2 个 action ,显示 form 表单,还要处理 POST。But in a real application you want to redisplay the original form. So youwill have two actions: one to display the form and another one to handlethe POST.感谢下载载精品当然如果有错误发生的话你需要重新跳转到第一
15、个action ,但是验证会发生在第二个 action中,这样你需要一些小技巧在跳转之前保持错误信息。使用validate.keey()方法,它可以为下个action保存错误集合。Of course the validation will occur in the second action and if some erroroccurs you will have to redirect to the first action. In this case you need aspecial trick to keep your errors during the redirect. Use
16、the*validation.keep()* method. This will save the errors collection for thenext action.让我们看一个真实的例子。Let s see a real sample:public class Application extends Controller public static void index() render();public static void hello(String name, Integer age) 感谢下载载精品validation.required(name);validation.re
17、quired(age);validation.min(age, 0);if(validation.hasErrors() params.flash(); / add http parameters to the flash scopevalidation.keep(); / keep the errors for the next requestindex();render(name, age);And the *view/Application/index.html* template:#ifErrors感谢下载载精品<h1>Oops.</h1>#errors<
18、li>$error</li>#/errors#/ifErrors#form Application.hello()<div>Name: <input type="text" name="name" value="$"/></div><div>Age: <input type="text" name="age" value="$flash.age" /></div>&l
19、t;div>感谢下载载精品<input type="submit" value="Say hello" /></div>#/formYou can create a better user experience by displaying each errormessage next to the field that generated the error:#ifErrors<h1>Oops.</h1>#/ifErrors#form Application.hello()<div>Nam
20、e: <input type="text" name="name" value="$"/><span class="error">#error 'name' /</span></div>感谢下载载精品<div>Age: <input type="text" name="age" value="$flash.age" /><span class=
21、"error">#error 'age' /</span></div><div><input type="submit" value="Say hello" /></div>#/form使用注解Using annotations你可以使用注解做相同的事。You can use annotations to do the same thing:感谢下载载精品public static void hello(Required String name, Req
22、uired Min(0)Integer age) if(validation.hasErrors() params.flash(); / add http parameters to the flash scopevalidation.keep(); / keep the errors for the next requestindex();render(name, age);验证对象Validating objects使用注解你可以轻松的为你的model对象增加约束,让我们重写前一个例子,使用 User 类。Using annotations you can easily add const
23、raints to your model objects.Let s rewrite the previous example using a User class.感谢下载载精品First the *User* class:package models;public class User Requiredpublic String name;RequiredMin(0)public Integer age;然后修改 hello action.Then the modified *hello* action:感谢下载载精品public static void hello(Valid User
24、user) if(validation.hasErrors() params.flash(); / add http parameters to the flash scopevalidation.keep(); / keep the errors for the next requestindex();render(name, age);最后增加一个修改后的form 表单And finally the modified form:#ifErrors<h1>Oops.</h1>#/ifErrors感谢下载载精品#form Application.hello()<div>Name: <input type="text" name=""value="$flash''" /><span class="error">#error '' /</span></div><div>Age: <input type="tex
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度文化旅游地产项目房屋及土地所有权转让协议
- 二零二五年度高校毕业生就业安置与就业服务保障合同
- 二零二五年度车库购置与车位共享运营协议
- 二零二五年度玉米种植补贴收购合同
- 二零二五年度廉洁合作协议:公共资源交易项目监管合同
- 二零二五年度饲料行业风险评估与保险合同
- 二零二五年度旅游度假区招商代理专项协议
- 二零二五年度少儿教育讲师聘用合同
- 二零二五年度高校离退休教师兼职返聘协议
- 二零二五年度瑜伽教练职业培训聘用协议
- 2024版2024年《汽车文化》全套教案
- 建筑垃圾清理运输服务方案
- 商业街委托运营合同范本
- 2024年部编版六年级语文上册第六单元 语文园地六(教案)
- 诺如病毒的护理
- 三年级下册语文核心素养教案电子版
- 中考英语688高频词大纲词频表
- 计算机基础教程电子版
- 财务管理学(第10版)课件 第3章 财务分析
- 急性脑卒中知识考核试题及答案
- 关于如何做好清单招标控制价的几点建议
评论
0/150
提交评论