PlayFramework框架验证Word文档格式.docx

上传人:b****2 文档编号:4949128 上传时间:2023-05-04 格式:DOCX 页数:13 大小:18.07KB
下载 相关 举报
PlayFramework框架验证Word文档格式.docx_第1页
第1页 / 共13页
PlayFramework框架验证Word文档格式.docx_第2页
第2页 / 共13页
PlayFramework框架验证Word文档格式.docx_第3页
第3页 / 共13页
PlayFramework框架验证Word文档格式.docx_第4页
第4页 / 共13页
PlayFramework框架验证Word文档格式.docx_第5页
第5页 / 共13页
PlayFramework框架验证Word文档格式.docx_第6页
第6页 / 共13页
PlayFramework框架验证Word文档格式.docx_第7页
第7页 / 共13页
PlayFramework框架验证Word文档格式.docx_第8页
第8页 / 共13页
PlayFramework框架验证Word文档格式.docx_第9页
第9页 / 共13页
PlayFramework框架验证Word文档格式.docx_第10页
第10页 / 共13页
PlayFramework框架验证Word文档格式.docx_第11页
第11页 / 共13页
PlayFramework框架验证Word文档格式.docx_第12页
第12页 / 共13页
PlayFramework框架验证Word文档格式.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

PlayFramework框架验证Word文档格式.docx

《PlayFramework框架验证Word文档格式.docx》由会员分享,可在线阅读,更多相关《PlayFramework框架验证Word文档格式.docx(13页珍藏版)》请在冰点文库上搜索。

PlayFramework框架验证Word文档格式.docx

下面我们看一下怎样去验证一个简单的HTTP参数。

Let’sseehowtovalidateasimpleHTTPparameter:

publicstaticvoidhello(Stringname){

 

validation.required(name);

...

}

这段代码检查name变量被正确的设置了,如果不是的话,相应的信息会被增加到当前的错误集合中去。

Thiscodechecksthatthenamevariableiscorrectlyset.Ifnot,thecorrespondingerrorisaddedtothecurrenterrorscollection.

你可以重复这个操作去验证每一个你需要的变量。

Youcanrepeatthisoperationforeachvalidationyouneed:

publicstaticvoidhello(Stringname,Integerage){

validation.required(age);

validation.min(age,0);

重新得到错误信息

Retrievingerrormessages

在每一个验证结束,你可以检查是否错误都被创建并显示出来了。

Attheendofthevalidationyoucancheckifanyerrorshavebeencreatedanddisplaythem:

if(validation.hasErrors()){

for(Errorerror:

validation.errors()){

System.out.println(error.message());

}

假设name和age是null,那么将会显示出:

Assumingthatnameandagearenull,thiswoulddisplay:

nameisrequired

ageisrequired

默认的消息是key和message集合中key一致的,所以在conf/messages文件中你可以看到:

Defaultmessagesarekeysthatrefertothemessagebundle.Sointhe**conf/messages**fileyouwillhave:

validation.required=%sisrequired

你可以改变这些默认的消息,然后再每一个项目中覆盖它,%s占位符会被错误的key所替代,你可以使用error.message(Stringfield)方法覆盖它。

Youcanchangethisdefaultmessageandoverrideitforeachapplicationlanguage.The**%s**placeholderwillbereplacedbytheerrorkey.Youcanoverrideusingthe**error.message(Stringfield)**method.

例如:

Forexample:

Errorerror=validation.required(name).error;

if(error!

=null){

System.out.println(error.message("

Thename"

));

你还可以为每一次检查明确指定不同的信息。

Youcanalsospecifyadifferentmessageforeachcheck:

Errorerror=validation.required(name).message("

Fillthename!

"

).error;

System.out.println(error.message());

再模板中显示错误信息

Displayingerrorsinthetemplate

再大多数情况下,你想让错误消息显示在视图模板中,你可以在模板中使用errors对象使用它们,一些tag帮助你显示这些错误。

Inmostcasesyouwanttodisplaytheerrormessagesintheviewtemplate.Youcanaccesstheminthetemplateusingthe**errors**object.Sometagshelpyoutodisplaytheerrors:

让我们看个例子。

Let’sseeasample:

render(name,age);

现在是模板。

andnowthetemplate:

#{ifErrors}

<

h1>

Oops...<

/h1>

#{errors}

li>

${error}<

/li>

#{/errors}

#{/ifErrors}

#{else}

Hello${name},youare${age}.

#{/else}

但是在实际的应用中,你想显示原先的form表单。

所以你将有2个action,显示form表单,还要处理POST。

Butinarealapplicationyouwanttoredisplaytheoriginalform.Soyouwillhavetwoactions:

onetodisplaytheformandanotheronetohandlethePOST. 

当然如果有错误发生的话你需要重新跳转到第一个action,但是验证会发生在第二个action中,这样你需要一些小技巧在跳转之前保持错误信息。

使用validate.keey()方法,它可以为下个action保存错误集合。

Ofcoursethevalidationwilloccurinthesecondactionandifsomeerroroccursyouwillhavetoredirecttothefirstaction.Inthiscaseyouneedaspecialtricktokeepyourerrorsduringtheredirect.Usethe**validation.keep()**method.Thiswillsavetheerrorscollectionforthenextaction.

让我们看一个真实的例子。

Let’sseearealsample:

publicclassApplicationextendsController{

publicstaticvoidindex(){

render();

publicstaticvoidhello(Stringname,Integerage){

validation.required(name);

validation.required(age);

validation.min(age,0);

if(validation.hasErrors()){

params.flash();

//addhttpparameterstotheflashscope

validation.keep();

//keeptheerrorsforthenextrequest

index();

render(name,age);

Andthe**view/Application/index.html**template:

#{form@Application.hello()}

div>

Name:

inputtype="

text"

name="

name"

value="

${flash.name}"

/>

/div>

Age:

age"

${flash.age}"

<

submit"

Sayhello"

#{/form}

Youcancreateabetteruserexperiencebydisplayingeacherrormessagenexttothefieldthatgeneratedtheerror:

spanclass="

error"

>

#{error'

name'

/}<

/span>

age'

使用注解

Usingannotations

你可以使用注解做相同的事。

Youcanuseannotationstodothesamething:

publicstaticvoidhello(@RequiredStringname,@Required@Min(0)Integerage){

params.flash();

validation.keep();

index();

验证对象

Validatingobjects

使用注解你可以轻松的为你的model对象增加约束,让我们重写前一个例子,使用User类。

Usingannotationsyoucaneasilyaddconstraintstoyourmodelobjects.Let’srewritethepreviousexampleusingaUserclass.

Firstthe**User**class:

packagemodels;

publicclassUser{

@Required

publicStringname;

@Min(0)

publicIntegerage;

然后修改helloaction.

Thenthemodified**hello**action:

publicstaticvoidhello(@ValidUseruser){

最后增加一个修改后的form表单

Andfinallythemodifiedform:

user.name"

${flash['

user.name'

]}"

user.age"

user.age'

自定义验证

Customvalidation

如果在play.data.validation没有发现你需要的验证,你可以自己写。

然后使用@CheckWith注解绑定到你自己的Check实现里去。

Can’tfindthevalidatoryouneedinthe**play.data.validation**package?

Writeyourown.Youcanusethegeneric**@CheckWith**annotationtobindyourown**Check**implementation.

例如

@CheckWith(MyPasswordCheck.class)

publicStringpassword;

staticclassMyPasswordCheckextendsCheck{

publicabstractbooleanisSatisfied(Objectuser,Objectpassword){

returnnotMatchPreviousPasswords(password);

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2