ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:18.07KB ,
资源ID:11511612      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11511612.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(PlayFramework框架验证.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

PlayFramework框架验证.docx

1、PlayFramework框架验证使用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 sav

2、ing 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,

3、 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. E

4、ach 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

5、 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 valida

6、te 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 ca

7、n 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 error

8、s 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 th

9、at 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占位符会被

10、错误的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(

11、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

12、 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

13、 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。Bu

14、t 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 i

15、n 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:p

16、ublic 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 validatio

17、n.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

18、 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.has

19、Errors() 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 rewri

20、te 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(); / a

21、dd http parameters to the flash scope validation.keep(); / keep the errors for the next request index(); render(name, age);最后增加一个修改后的form表单And finally the modified form:#ifErrors Oops.#/ifErrors#form Application.hello() Name: #error user.name / Age: #error user.age / #/form自定义验证 Custom validation如果在

22、play.data.validation没有发现你需要的验证,你可以自己写。然后使用CheckWith注解绑定到你自己的Check实现里去。Cant find the validator you need in the *play.data.validation* package? Write your own. You can use the generic *CheckWith* annotation to bind your own *Check* implementation.例如For example:public class User Required CheckWith(MyPasswordCheck.class) public String password; static class MyPasswordCheck extends Check public abstract boolean isSatisfied(Object user, Object password) return notMatchPreviousPasswords(password);

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1