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

Struts2教程4:使用validate方法驗證數(shù)據(jù)

開發(fā) 開發(fā)工具 后端
Struts是Apache基金會Jakarta項目組的一個Open Source項目,它采用MVC模式,能夠很好地幫助Java開發(fā)者利用J2EE開發(fā)Web應(yīng)用。和其他的Java架構(gòu)一樣,Struts也是面向?qū)ο笤O(shè)計,將MVC模式"分離顯示邏輯和業(yè)務(wù)邏輯"的能力發(fā)揮得淋漓盡致。Struts的目的是為了減少在運用MVC設(shè)計模型來開發(fā)Web應(yīng)用的時間。你仍然需要學(xué)習(xí)和應(yīng)用該架構(gòu),不過它將可以完成其中一些繁重的工作。在本系列教程中我們將學(xué)習(xí)到Struts2的各種技術(shù)。

【相關(guān)文章】

  1. Struts2教程1:***個Struts2程序
  2. Struts2教程2:處理一個form多個submit
  3. Struts2教程3:struts.xml常用配置解析
  4. Struts2教程5:使用Validation框架驗證數(shù)據(jù)
  5. Struts2教程6:在Action類中獲得HttpServletResponse對象
  6. Struts2教程7:上傳任意多個文件
  7. Struts2教程8:攔截器概述
  8. Struts2教程9:實現(xiàn)自已的攔截器
  9. Struts2教程10:國際化

在Struts2中最簡單的驗證數(shù)據(jù)的方法是使用validate。我們從ActionSupport類的源代碼中可以看到,ActionSupport類實現(xiàn)了一個Validateable接口。這個接口只有一個validate方法。如果Action類實現(xiàn)了這個接口,Struts2在調(diào)用execute方法之前首先會調(diào)用這個方法,我們可以在validate方法中驗證,如果發(fā)生錯誤,可以根據(jù)錯誤的level選擇字段級錯誤,還是動作級錯誤。并且可使用addFieldError或addActionError加入相應(yīng)的錯誤信息,如果存在Action或Field錯誤,Struts2會返回“input”(這個并不用開發(fā)人員寫,由Struts2自動返回),如果返回了“input”,Struts2就不會再調(diào)用execute方法了。如果不存在錯誤信息,Struts2在***會調(diào)用execute方法。

這兩個add方法和ActionErrors類中的add方法類似,只是add方法的錯誤信息需要一個ActionMessage對象,比較麻煩。除了加入錯誤信息外,還可以使用addActionMessage方法加入成功提交后的信息。當(dāng)提交成功后,可以顯示這些信息。

以上三個add方法都在ValidationAware接口中定義,并且在ActionSupport類中有一個默認(rèn)的實現(xiàn)。其實,在ActionSupport類中的實現(xiàn)實際上是調(diào)用了ValidationAwareSupport中的相應(yīng)的方法,也就是這三個add方法是在ValidationAwareSupport類中實現(xiàn)的,代碼如下:

privatefinalValidationAwareSupportvalidationAware=newValidationAwareSupport();
publicvoidaddActionError(StringanErrorMessage)
{   validationAware.addActionError(anErrorMessage);
}
publicvoidaddActionMessage(StringaMessage)
{
  validationAware.addActionMessage(aMessage);
}
publicvoidaddFieldError(StringfieldName,StringerrorMessage)
{
  validationAware.addFieldError(fieldName,errorMessage);
}

下面我們來實現(xiàn)一個簡單的驗證程序,來體驗一個validate方法的使用。

先來在Web根目錄建立一個主頁面(validate.jsp),代碼如下:

<%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<html>
 <head>
 ?。紅itle>驗證數(shù)據(jù)</title>
 </head>
 
?。糱ody>
 ?。約:actionerror/>
  <s:actionmessage/>
 ?。約:formaction="validate.action" theme="simple">
    輸入內(nèi)容:<s:textfieldname="msg"/>
   ?。約:fielderrorkey="msg.hello"/>
   ?。糱r/>
    <s:submit/>
 ?。?s:form>
?。?body>
</html>

在上面的代碼中,使用了Struts2的tag:<s:actionerror>、<s:fielderror>和<s:actionmessage>,分別用來顯示動作錯誤信息,字段錯誤信息,和動作信息。如果信息為空,則不顯示。

現(xiàn)在我們來實現(xiàn)一個動作類,代碼如下:

packageaction;

importjavax.servlet.http.*;
importcom.opensymphony.xwork2.ActionSupport;
importorg.apache.struts2.interceptor.*;
publicclassValidateActionextendsActionSupport
{
  privateStringmsg;
  publicStringexecute()
  {
    System.out.println(SUCCESS);
    returnSUCCESS;
  }
  publicvoidvalidate()
  {
    if(!msg.equalsIgnoreCase("hello"))
    {
      System.out.println(INPUT);
      this.addFieldError("msg.hello","必須輸入hello!");
      this.addActionError("處理動作失敗!");
    }
    else
    {
      this.addActionMessage("提交成功");
    }
  }
  publicStringgetMsg()
  {
    returnmsg;
  }
  publicvoidsetMsg(Stringmsg)
  {
    this.msg=msg;
  }
}

大家從上面的代碼可以看出,F(xiàn)ield錯誤需要一個key(一般用來表示是哪一個屬性出的錯誤),而Action錯誤和Action消息只要提供一個信息字符串就可以了。

***來配置一下這個Action,代碼如下:

<packagename="demo"extends="struts-default">
 ?。糰ctionname="validate"class="action.ValidateAction">
    <resultname="success">/error/validate.jsp</result>
   ?。紃esultname="input">/error/validate.jsp</result>
 ?。?action>
</package>

假設(shè)應(yīng)用程序的上下文路徑為demo,則可通過如下的URL來測試程序:

http://localhost:8080/demo/validate.jsp

我們還可以使用ValidationAware接口的其他方法(由ValidationAwareSupport類實現(xiàn))獲得或設(shè)置字段錯誤信息、動作錯誤信息以及動作消息。如hasActionErrors方法判斷是否存在動作層的錯誤,getFieldErrors獲得字段錯誤信息(一個Map對象)。下面是ValidationAware接口提供的所有的方法:

packagecom.opensymphony.xwork2;

importjava.util.Collection;
importjava.util.Map;
publicinterfaceValidationAware
{
  voidsetActionErrors(CollectionerrorMessages);
  CollectiongetActionErrors();
  voidsetActionMessages(Collectionmessages);
  CollectiongetActionMessages();
  voidsetFieldErrors(MaperrorMap);
  MapgetFieldErrors();
  voidaddActionError(StringanErrorMessage);
  voidaddActionMessage(StringaMessage);
  voidaddFieldError(StringfieldName,StringerrorMessage);
  booleanhasActionErrors();
  booleanhasActionMessages();
  booleanhasErrors();
  booleanhasFieldErrors();
}

【編輯推薦】

  1. Struts2教程1:***個Struts2程序
  2. Struts2教程2:處理一個form多個submit
  3. Struts2教程3:struts.xml常用配置解析
  4. Struts2教程5:使用Validation框架驗證數(shù)據(jù)
  5. Struts2教程6:在Action類中獲得HttpServletResponse對象
  6. Struts2教程7:上傳任意多個文件
  7. Struts2教程8:攔截器概述
  8. Struts2教程9:實現(xiàn)自已的攔截器
  9. Struts2教程10:國際化
責(zé)任編輯:楊鵬飛 來源: BlogJava
相關(guān)推薦

2009-06-25 15:33:12

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

2009-06-25 15:37:12

Struts2教程Validation框

2009-02-04 13:13:03

2011-03-30 09:03:57

struts2

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 10:51:07

2009-06-03 14:19:34

Struts2Guice

2009-06-25 16:04:30

2009-06-05 10:17:34

struts vali驗證

2009-06-25 15:26:25

Struts2教程struts.xml常

2009-02-04 15:04:13

2009-06-25 15:54:42

Struts2教程攔截器

2009-06-25 15:50:03

Struts2教程上傳任意多個文件

2009-02-04 11:37:15

2009-06-05 09:58:20

struts2驗證用戶注冊

2009-06-04 09:20:19

struts2 if標(biāo)使用

2012-05-10 14:00:06

StrutsjsonJava

2009-07-29 09:54:34

struts2和str

2009-02-04 14:19:38

2009-02-04 14:00:59

點贊
收藏

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