struts2课件4-验证框架.ppt_第1页
struts2课件4-验证框架.ppt_第2页
struts2课件4-验证框架.ppt_第3页
struts2课件4-验证框架.ppt_第4页
struts2课件4-验证框架.ppt_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Struts2第六课 表单数据校验,讲师:李玉明,手工验证,在Action中重写validate方法:public void validate(); 往父类错误栈中添加错误信息addFieldError(a,b) 在验证完毕后,struts会检查错误栈,如果有数据,则调用Input result,如果没数据,表示验证通过,调用业务方法(通常为execute),/Override /public void validate() /String params = A,B; /List params2 = new ArrayList(); /params2.add(c); /params2.add(

2、D); /String value = this.getText(username,params); /String value2 = this.getText(username,params2); /System.out.println(value+value2); /if(null!=user / / Mperties: username=User Name age=Age age.min=age should bigger than 18,转换与校验,转换是校验的基础,只有在数据被正确地转换成其对应的类型后,我们才可以对其取值范围进行校验。,HelloWorld.ja

3、va,package validate; import java.util.Locale; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.LocalizedTextUtil; public class HelloWorld extends ActionSupport private String msg; private Locale loc = Locale.US; public String getMsg() return msg; public Locale getLoc

4、() return loc; public void setLoc(Locale loc) this .loc = loc; ,Override public void validate() System.out.println(Calling validate() ); if (!(loc.equals(Locale.US)|loc.equals(Locale.CHINA) addFieldError(loc,getText(validation.loc); public void validateExecute() System.out.println(Calling validateEx

5、ecute() by reflection ); Override public String execute() System.out.println(Calling execute(); msg = LocalizedTextUtil.findDefaultText(HelloWorld , loc); return SUCCESS; ,Struts.xml, i18nHelloWorld.jsp i18nHelloWorld.jsp ,i18nHelloWorld.jsp, Validate HelloWorld Locale: ,LocaleConverter.java,package

6、 validate; import java.util.Locale; import java.util.Map; public class LocaleConverter extends ognl.DefaultTypeConverter Override public Object convertValue(Map context, Object value, Class toType) if (toType = Locale. class ) System.out.println(Converting String to Locale); String locale = (String)

7、 value) 0 ; return new Locale(locale.substring( 0 , 2 ), locale.substring( 3 ); else if (toType = String. class ) System.out.println( Converting Locale to String ); Locale locale = (Locale) value; return locale.toString(); return null ; ,HelloWperties,loc=validate.LocaleConverter,

8、globalMessages_zh_CN.properties,HelloWorld=u4F60u597DuFF0Cu4E16u754CuFF01 invalid.fieldvalue.loc=Localeu5FC5u987Bu4E3Axx_XXu7684u683Cu5F0F validation.loc=u533Au57DFu5FC5u987Bu4E3Au4E2Du56FDu6216u7F8Eu56FD,globalMessages_en_US.properties,HelloWorld=Hello World! invalid.fieldvalue.loc=Locale must like

9、 xx_XX validation.loc=Locale must be China or USA,测试,在Locale输入框中输入“zh_CN”,按“Submit”提交:,数据校验工作方式,通过转换器将请求参数转换成相应的Bean属性; 判断转换过程是否出现异常。如果有,则将其保存到ActionContext中,conversionError拦截器再封装为fieldError;如果没有,进行下一步; 通过反射(Reflection)来调用validateXxx()方法(其中,Xxx表示Action的方法名); 调用validate()方法; 如果经过上述步骤没有出现fieldError,则调

10、用Action方法;如果有,则会跳过Action方法,通过国际化将fieldError输出到页面。,校验顺序图,校验选择,如果需要转换的数据,通常做法在转换的时候做格式的校验,在Action中的校验方法中校验取值。假如用户填错了格式,我们可以通过在资源文件配置invalid.fieldvalue.xxx(xxx为属性名)来提示用户正确的格式,不同的阶段出错显示不同的信息。 用validate()还是validateXxx()?推荐使用validate()。validateXxx()使用了反射,相对来说性能稍差,而validate()则是通过接口com.opensymphony.xwork2.V

11、alidateable调用。当然如果你的表单数据取值是取决于特定Action方法,则应该使用validateXxx()。,校验测试,Struts2框架校验,package validate; import com.opensymphony.xwork2.ActionSupport; public class ValidationAction extends ActionSupport private String reqiuredString; public String getReqiuredString() return reqiuredString; public void setReq

12、iuredString(String reqiuredString) this .reqiuredString = reqiuredString; Override public String execute() return SUCCESS; ,struts.xml, Output.jsp Input.jsp ,Input.jsp, Input ,校验配置文件 ValidationAction-validation.xml, This string is required ,Output.jsp, Output Required String: ,测试,示例-register.jsp, re

13、gister ,public class RegisterAction extends ActionSupport private String name; private String pass; private int age; private Date birthday; public void setName(String name) = name; public String getName() return ; 。 Override public String execute()throws Exception return SUCCESS;

14、,客户端验证, $getText(username.required) $getText(username.regex) true $getText(pass.required) $getText(pass.regex) , 1 100 $getText(age.range) 1900-01-01 2050-01-01 $getText(birthday.range) ,Struts.xml, /1.jsp show.jsp register.jsp ,globalMessage_zh_CN.properties,username.required=u4f60u5fc5u987bu8f93u5

15、165u7528u6237u540d username.regex=u4f60u8f93u5165u7684u7528u6237u540du53eau80fdu662fu5b57u6bcdu4e0eu6570u5b57uff0cu4e14u957fu5ea6u5fc5u987bu57284u81f325u4e4bu9593 pass.required=u4f60u5fc5u987bu8f93u5165u5bc6u7801 pass.regex=u4f60u8f93u5165u7684u5bc6u7801u53eau80fdu662fu5b57u6bcdu4e0eu6570u5b57uff0cu4e14u957fu5ea6u5fc5u987bu57284u81f325u4e4bu9593 age.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论