




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
使用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 errors textual description. The message can be a plain message or refer to a key from a message bundle (typically for internationalization support).下面我们看一下怎样去验证一个简单的HTTP参数。Lets 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.required(age); validation.min(age, 0); .重新得到错误信息 Retrieving error messages在每一个验证结束,你可以检查是否错误都被创建并显示出来了。At the end of the validation you can check if any errors have been created 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,那么将会显示出: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你可以改变这些默认的消息,然后再每一个项目中覆盖它,%s占位符会被错误的key所替代,你可以使用error.message(String field)方法覆盖它。You can change this default message and override it for each application language. The *%s* placeholder will be replaced by the error key. You can override using the *error.message(String field)* method.例如:For example:Error 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) System.out.println(error.message();再模板中显示错误信息 Displaying errors in the template再大多数情况下,你想让错误消息显示在视图模板中,你可以在模板中使用errors对象使用它们,一些tag帮助你显示这些错误。In most cases you want to display the error messages in the view template. You can access them in the template using the *errors* object. Some tags help you to display the errors:让我们看个例子。Lets 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 Oops. #errors $error #/errors#/ifErrors#else Hello $name, you are $age.#/else但是在实际的应用中,你想显示原先的form表单。所以你将有2个action,显示form表单,还要处理POST。But in a real application you want to redisplay the original form. So you will have two actions: one to display the form and another one to handle the POST.当然如果有错误发生的话你需要重新跳转到第一个action,但是验证会发生在第二个action中,这样你需要一些小技巧在跳转之前保持错误信息。使用validate.keey()方法,它可以为下个action保存错误集合。Of course the validation will occur in the second action and if some error occurs you will have to redirect to the first action. In this case you need a special trick to keep your errors during the redirect. Use the *validation.keep()* method. This will save the errors collection for the next action.让我们看一个真实的例子。Lets 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.required(age); validation.min(age, 0); if(validation.hasErrors() params.flash(); / add http parameters to the flash scope validation.keep(); / keep the errors for the next request index(); render(name, age); And the *view/Application/index.html* template:#ifErrors Oops. #errors $error #/errors#/ifErrors#form Application.hello() Name: Age: #/formYou can create a better user experience by displaying each error message next to the field that generated the error:#ifErrors Oops.#/ifErrors#form Application.hello() Name: #error name / Age: #error age / #/form使用注解 Using annotations你可以使用注解做相同的事。You can use annotations to do the same thing:public static void hello(Required String name, Required Min(0) Integer age) if(validation.hasErrors() params.flash(); / add http parameters to the flash scope validation.keep(); / keep the errors for the next request index(); render(name, age);验证对象 Validating objects使用注解你可以轻松的为你的model对象增加约束,让我们重写前一个例子,使用User类。Using annotations you can easily add constraints to your model objects. Lets rewrite the previous example using a User class.First the *User* class:package models;public class User Required public String name; Required Min(0) public Integer age;然后修改hello action.Then the modified *hello* action:public static void hello(Valid User user) if(validation.hasErrors() params.flash(); / add http parameters to the flash scope validation.keep(); / keep the errors for the next request index(); render(name, a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 百色市中石化2025秋招笔试模拟题含答案市场营销与国际贸易岗
- 国家能源莆田市2025秋招面试专业追问及参考计算机与自动化岗位
- 国家能源大连市2025秋招笔试逻辑推理题专练及答案
- 江西地区中石化2025秋招面试半结构化模拟题及答案数智化与信息工程岗
- 运城市中储粮2025秋招笔试题库含答案
- 长治市中储粮2025秋招笔试题库含答案
- 宜宾市中石化2025秋招笔试模拟题含答案油田工程技术岗
- 中国联通黄冈市2025秋招笔试模拟题及答案
- 中国移动吉安市2025秋招网申填写模板含开放题范文
- 民航知识考试试题及答案
- GB/T 6175-20162型六角螺母
- GB/T 3810.4-2016陶瓷砖试验方法第4部分:断裂模数和破坏强度的测定
- 手术室进修护士结业理论考试题附答案
- 组织行为学MBA全套课件
- 光伏施工方案
- 支架预压沉降观测报告
- 医疗风险管理检查记录表
- 全息经络刮痧疗法(内部培训)课件
- 消防安全知识课件PPT(72张)
- 幼儿园绘本故事:《我爸爸》 PPT课件
- 学位英语试题及答案解释
评论
0/150
提交评论