熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts validate驗證的具體實現

2013-11-23 20:20:14  來源: Java開源技術 

  Struts validate驗證

   ActionErrors和ActionError都是ActionMessages的子類ActionError存放在ActionErrors 中ActionError對象中的參數為配置文件中配置的相應的值若配置文件中沒有配置或配置文件不適合都不能用< htnl:errors property=/>輸出

  錯誤信息添加用 

  ActionErrors errors=new ActionErrors(); 

  errorsadd(error_keynew ActionError(配置文件中的相應的值)) 

  注解void add(javalangString propertyActionError error)

  在jsp頁面中顯示錯誤

  在Action中使用ActionMessages 他有兩個add方法:

  他有兩個add方法:

  void add(ActionMessages  message)
 Adds the meesage from the given ActionMessage object  to tjis set of meesage
void add(javalangString propertyActionMessage message)
            Add message to the set of message for the specifiled property

  錯誤信息添加使用errorsadd(error_keynew ActionMessage(errorsloginerror))
另外要調用thissaveMessages(requesterrors)方法
這是orgapachestructsactionAction中 的方法要用到的大概有兩個

  saveErrors(少用)

  protected void saveErrors(javaservlethttpHttoServletRequest requestActionErrors errors)
Save the specifiled error message keys into the appropriate request attribute for use by the<html:errors>
tagif any message are requiredOtherwiseensure that the request attribute is not created

  saveMessage(常用)

  protect void saveMessage(javaxServlethttpHttpServletRequest requestActionMessages  messages)
Save the specified message keys into the appropriate request attribute for use by the<html:message>tag{
if messages=true    is set}if any message are requiredOtherwiseensure that the request attribute  is not
created

  ActionErrors actionMessages ActionMessage ActionMessage是繼承關系父類在前子類在後

  

  if (!loadFormgetUsername()equals()) {

    ActionMessages error = new ActionMessages();
  erroradd(username new ActionMessage(errorusername));
  saveErrors(request error);

    return mappingfindForward(S);
 }

   if (!loadFormgetPassword()equals(abc)) {

    ActionMessages error = new ActionMessages();
  erroradd(password new ActionMessage(errorpassword));
  saveErrors(request error);

    return mappingfindForward(S);
 }

   return mappingfindForward(S);

  errorusername=<font color=red>\ue\uf\u</font>
errorpassword=<font color=red>\ubc\u\u</font>
errorup=<font color=red>\ue\uf\u\ubc\u\ued\ufd\uaa</font>

  FORM裡面

  

  public ActionErrors validate(ActionMapping mapping
  HttpServletRequest request) {

   ActionErrors errors = new ActionErrors();
if (null == usernametrim() || null == passwordtrim()||usernamelength()<

  ||passwordlength()<) {

    errorsadd(username new ActionMessage(errorup));
  Systemoutprintln(errors);

    return errors;
 }

   return null;
}

  nativeascii 來處理中文漢字


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28325.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.