strutsä¸æä¾ä¸¤ç§æ ¡éªæ¹æ³ï¼ç¡¬ç¼ç åæ ¡éªæ¡æ¶ï¼
硬ç¼ç å°±æ¯ç´æ¥åvalidate()è¿è¡éªè¯ï¼validate()æ¹æ³å¯¹åºexecute()æ¹æ³ã
å¨actionéä¹å¯ä»¥éæ°å®ä¹methodæ¹æ³ï¼è¿æ ·å¨å¯¹åºçé»è¾å¤çç±»ä¸ç´æ¥å®ä¹è¯¥æ¹æ³å¯¹åºçvalidate()æ¹æ³ã
å¦å®ä¹äºæ¹æ³abcï¼ï¼
ããpublic String abc()
ãã {
ããã System.out.print("hello");
ããã return SUCCESS;
ãã }
public void validateAbc()
{//æ¹æ³å为validate+æ¹æ³åï¼ç¬¬ä¸ä¸ªåæ¯å¤§åï¼
System.out.println("vailidate abc");
//é¦å
æ§è¡validateAbc()ï¼åæ§è¡validate()æ¹æ³ï¼åå¨æ§è¡abc()ï¼
//解å³æ¹æ³æ两个ï¼ä¸æ¯å»ævalidate()æ¹æ³ï¼äºæ¯å°validate()æ¹æ³éå½å为validateExecute()ï¼
}
public void validate() {
if(null==username||username.length()<6||username.length()>10)
{
//表åä¸åªæ¾ç¤ºFILED级å«çé误信æ¯ï¼ä¸æ¾ç¤ºACTION级å«çé误信æ¯
//两ç§éªè¯ï¼FILEDERRORåæ¾å¨MAPä¸ä¼å¨æ ç¾åºä¸æ¾ç¤ºï¼
//èactionerroræ¯åæ¾å¨LISTéï¼ä¸ä¼å¨æ ç¾åºé»è®¤æ¾ç¤ºï¼æ以页é¢ä¸ä¸ä¼æé误æ示
this.addFieldError("username","username invalid");
//this.addActionError("username invalid");
}
if(null==password||password.length()<6||password.length()>10)
{
this.addFieldError("password","å¯ç ä¸åæ³");
}
}
æ ¡éªæ¡æ¶å°±æ¯è¦å对åºè¯¥ACIONççxmlé
ç½®æ件ï¼
å¦é对RegisterAction建ç«RegisterAction-validation.xmlæ件è¿è¡æ°æ®æ ¡éªã
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator1.0.2//EN" "
http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<validator type="requiredstring">
<param name="fieldName">username</param>
<message>å§åä¸è½ä¸ºç©º</message>
</validator>
<validator type="stringlength">
<param name="fieldName">username</param>
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>å§åé¿åº¦åºå¨${minLength}å${maxLength}ä¹é´</message>
</validator>
<!--ä¸é¢æ¯å段æ£éªå¨ï¼ä¸é¢æ¯éåæ®µæ ¡éªå¨
<field name="username">
<field-validator type="requiredstring" short-circuit="true">
<param name="trim">true</param>
<message>å§åä¸è½ä¸ºç©º</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>å§åé¿åº¦åºå¨${minLength}å${maxLength}ä¹é´</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>å¯ç ä¸è½ä¸ºç©º</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>å¯ç é¿åº¦åºå¨${minLength}å${maxLength}ä¹é´</message>
</field-validator>
</field>
<field name="repassword">
<field-validator type="requiredstring">
<message>å次å¯ç ä¸è½ä¸ºç©º</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>å次é¿åº¦åºå¨${minLength}å${maxLength}ä¹é´</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>å¹´é¾åºå¨${min}å${max}ä¹é´</message>
</field-validator>
</field>
<field name="birthday">
<field-validator type="required">
<param name="trim">true</param>
<message>åºçæ¥æä¸è½ä¸ºç©º</message>
</field-validator>
<field-validator type="date">
<param name="min">1988-1-1</param>
<param name="max">2001-1-1</param>
<message>åºçæ¥æåºå¨${min}å${max}ä¹é´</message>
</field-validator>
</field>
<field name="graduation">
<field-validator type="required">
<param name="trim">true</param>
<message>æ¯ä¸æ¥æä¸è½ä¸ºç©º</message>
</field-validator>
<field-validator type="date">
<param name="min">2010-1-1</param>
<param name="max">2012-1-1</param>
<message>æ¯ä¸æ¥æåºå¨${min}å${max}ä¹é´</message>
</field-validator>
</field>-->
</validators>
ããå¦å¤ä¾ï¼RegisterAction-test-validation.xml
ãã(ç±»å-æ¹æ³å-validation.xmlä¸é¨å¯¹è¯¥ç±»çtest()æ¹æ³è¿è¡æ ¡éªï¼ä½æ¯é¦å
è¿è¡RegisterAction-validation.xmlæ ¡éª)
ããæ以建议ï¼å¦æä¸ä¸ªaction对åºå¤ä¸ªå¤çé»è¾çæ¹æ³ï¼ä¸è¦åå
¨å±ç.xmlæ ¡éªï¼èåºè¯¥é对æ¯ä¸ªé»è¾æ¹æ³åä¸é¨ç.xmlæ ¡éªã
ãã注æï¼
ããa. çæ£åæ¾field级å«é误信æ¯ç对象æ¯LinkedHashMap
(èä¸å
å¤ççvalidation.xmléé¢çï¼ç¶åå¤çvalidate()æ¹æ³éç)
ããb. 该LinkedHashMapçkeyæ¯Stringç±»åçï¼valueæ¯ArrayListç±»åç
ããc. 对äºAction级å«çé误信æ¯ï¼å®é
ä¸æ¯æ¾ç½®å¨ArrayListä¸çã
ããstruts2åæ ·æ¯æ客æ·ç«¯æ ¡éªï¼
ããa.formçthemeä¸å®ä¸è½è®¾ä¸ºsimple
ããb.å°FORMçvalidateå±æ§è®¾ä¸ºTRUE
ããä½æ¯æ好ä¸ä½¿ç¨struts2æä¾å®¢æ·ç«¯æ ¡éªï¼åè½æ¯è¾å¼±.