Struts2 验证框架使用基本步骤

基本验证框架:

1. 所写Action需要继承ActionSupport,若未继承,则需自己实现validationAware。

2. 在Action所在类创建ActionClass-validations.xml,如你的Action名字为Login,则需要创建Login-validations.xml。

3. 该xml格式为:

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
       "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
  <field name="password">
      <field-validator type="requiredstring">
         <message >Password is required.</message>
      </field-validator>
      <field-validator type="stringlength">
         <param name="maxLength">10</param>
         <param name="minLength">6</param>
         <message>Your password should be 6-10 characters.</message>
      </field-validator>
      <field-validator type="passwordintegrity">
          <param name="specialCharacters">$!@#?</param>
          <message>Your password must contain one letter, one number, and one of the following "${specialCharacters}".</message>
      </field-validator>
  </field>
  <field name="username">
      <field-validator type="requiredstring">
           <message >Username is required.</message>
      </field-validator>
      <field-validator type="stringlength">
         <param name="maxLength">8</param>
         <param name="minLength">5</param>
         <message>While ${username} is a nice name, a valid username must be between ${minLength} and ${maxLength} characters long. </message>
     </field-validator>
  </field>
  <field name="portfolioName">
      <field-validator type="requiredstring">
         <message key="portfolioName.required"/>
     </field-validator>
  </field>
   <field name="email">
      <field-validator type="requiredstring">
          <message>You must enter a value for email.</message>
      </field-validator>
       <field-validator type="email">
         <message key="email.invalid"/>
      </field-validator>
   </field>
  <validator type="expression">
      <param name="expression">username != password</param>
      <message>Username and password can't be the same.</message>
  </validator>
</validators>

基本格式为:

<validators>
	<field name="">
		<field-validator type=""> <!-- Struts2验证器名称,可以自己创建验证器 -->
			<message>错误信息</message>
			<param name="">参数</param> <!-- 可选 -->
		</field-validator>
	</field>
</validators>

自定义验证器:


你可能感兴趣的:(Struts2 验证框架使用基本步骤)