2011-04-17 53 views
0

我在製作註冊表格。如何驗證Struts 1中的密碼字段?

我需要檢查兩個密碼字段是否相同。

<validator name="twofields" 
      classname="com.mysite.StrutsValidator" 
      method="validateTwoFields" 
      msg="errors.twofields"/> 

<field property="password" 
     depends="required,twofields"> 

    <arg position="0" key="typeForm.password.displayname"/> 
    <var> 
     <var-name>secondProperty</var-name> 
     <var-value>password2</var-value> 
    </var> 
</field> 

方法

public class StrutsValidator { 

    public static boolean validateTwoFields(
      Object bean, 
      ValidatorAction va, 
      Field field, 
      ActionErrors errors, 
      HttpServletRequest request, 
      ServletContext application) { 

     String value = ValidatorUtils.getValueAsString(
      bean, 
      field.getProperty()); 
     String sProperty2 = field.getVarValue("secondProperty"); 
     String value2 = ValidatorUtils.getValueAsString(
      bean, 
      sProperty2); 

     if (!GenericValidator.isBlankOrNull(value)) { 
      try { 
       if (!value.equals(value2)) { 
        errors.add(field.getKey(), 
           Resources.getActionError(
            application, 
            request, 
            va, 
            field)); 

        return false; 
       } 
      } 
      catch (Exception e) { 
       errors.add(field.getKey(), 
          Resources.getActionError( //This line is giving an error. 
           application, 
           request, 
           va, 
           field)); 
       return false; 
      } 
     } 
     return true; 
    } 
} 

我嘗試這樣做,但它給了一個錯誤。方法getActionError(ServletContext, HttpServletRequest, ValidatorAction, Field)未定義類型資源。

回答

0

你確切的Struts版本是什麼?同時根據Struts 1.1 API documentation最少,Resources.getActionError方法簽名如下:

getActionError( 
    javax.servlet.http.HttpServletRequest request, 
    org.apache.commons.validator.ValidatorAction va, 
    org.apache.commons.validator.Field field 
) 

這表明你需要從方法調用中刪除應用程序參數:

errors.add(
    field.getKey(), 
    Resources.getActionError(
     request, 
     va, 
     field));