自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Struts validate驗證的具體實現(xiàn)

開發(fā) 后端
本文將對Struts validate驗證進行講解,Struts validate驗證通過XML文件進行配置,希望本文對大家有所幫助。
Struts validate驗證

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

2.錯誤信息添加用 

ActionErrors errors=new ActionErrors(); 

errors.add("error_key",new ActionError("配置文件中的相應的值")) 

注解:void add(java.lang.String property,ActionError error)

在jsp頁面中顯示錯誤:

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

他有兩個add方法:

void add(ActionMessages  message)
  Adds the meesage from the given ActionMessage object  to tjis set of meesage
void add(java.lang.String property,ActionMessage message)
             Add message to the set of message for the specifiled property.

錯誤信息添加使用errors.add("error_key",new ActionMessage("errors.loginerror"))
另外要調(diào)用this.saveMessages(request,errors)方法;
這是org.apache.structs.action.Action中 的方法,要用到的大概有兩個:

1.saveErrors(少用)

protected void saveErrors(java.servlet,http.HttoServletRequest request,ActionErrors errors)
 Save the specifiled error message keys into the appropriate request attribute for use by the<html:errors>
tag,if any message are required.Otherwise,ensure that the request attribute is not created.

2.saveMessage(常用)

protect void saveMessage(javax.Servlet.http.HttpServletRequest request,ActionMessages  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 required,Otherwise,ensure that the request attribute  is not
created.

ActionErrors actionMessages ,ActionMessage ActionMessage是繼承關(guān)系,父類在前,子類在后

if (!loadForm.getUsername().equals("123")) {

   ActionMessages error = new ActionMessages();
   error.add("username", new ActionMessage("error.username"));
   saveErrors(request, error);

   return mapping.findForward("S1");
  }

  if (!loadForm.getPassword().equals("abc")) {

   ActionMessages error = new ActionMessages();
   error.add("password", new ActionMessage("error.password"));
   saveErrors(request, error);

   return mapping.findForward("S1");
  }

  return mapping.findForward("S1");

error.username=<font color="red">\u5e10\u53f7\u9519</font>
error.password=<font color="red">\u5bc6\u7801\u9519</font>
error.up=<font color="red">\u5e10\u53f7\u6216\u5bc6\u7801\u4e0d\u80fd\u7a7a</font>

FORM里面

public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {

  ActionErrors errors = new ActionErrors();
if (null == username.trim() || null == password.trim()||username.length()<3

||password.length()<2) {

   errors.add("username", new ActionMessage("error.up"));
   System.out.println(errors);

   return errors;
  }

  return null;
 }

native2ascii 來處理中文漢字

【編輯推薦】

  1. 在Eclipse中開發(fā)struts應用程序
  2. 手把手教你在Eclipse中配置開發(fā)Struts
  3. Eclipse下開發(fā)struts完整解決亂碼問題
  4. Struts相關(guān)背景介紹
  5. 使用Easy Struts for Eclipse開發(fā)Struts
責任編輯:張燕妮 來源: javaeye.com
相關(guān)推薦

2009-06-25 15:33:12

Struts2教程使用validate驗證數(shù)據(jù)

2009-02-04 12:00:08

2009-11-25 11:33:26

PHP驗證表單

2009-11-23 16:59:23

PHP圖形驗證碼

2009-06-04 10:33:10

struts vali框架

2011-03-30 09:03:57

struts2

2009-06-05 09:58:20

struts2驗證用戶注冊

2009-06-25 15:37:12

Struts2教程Validation框

2011-04-28 09:52:04

Struts2

2009-06-04 10:54:59

實戰(zhàn)strutsMenu

2009-06-05 09:52:25

struts分頁Hibernate

2009-02-04 13:13:03

2010-06-08 14:28:16

Opensuse中文

2010-07-22 13:23:46

telnet SMTP

2009-07-20 16:51:59

Struts2.0+i

2009-06-03 15:38:37

Struts框架RBAC

2009-12-02 17:22:22

PHP抓取圖片

2010-07-13 10:38:54

2009-08-26 13:41:58

C#打印源碼

2009-11-23 18:39:17

PHP重定向
點贊
收藏

51CTO技術(shù)棧公眾號