Rational Developer for System z


课程 8:为 ActionValidator 类创建代码

ActionValidator 类将验证至少其中一个字符串参数具有值,但并不是都具有值。您还将使 Value 参数成为必需参数,并且将编写 ActionValidator 类以使用星号来标记此参数。

  1. 包资源管理器视图中,通过执行下列操作来打开 ActionValidator 类:浏览 com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action,并双击此类以在编辑器中将它打开。
  2. 您将从覆盖 isParameterRequired 方法开始。对于此样本,将只对要成为必需参数的 Value 参数编写代码。以下伪码演示了此情况:
    if paramID = paramID of value parameter
    	return true
    else
    	return false
    使用以下示例源代码来覆盖 isParameterRequired 方法:
    /( Mark the value parameter as required.*/
    public boolean isParameterRequired(Action action, Parameter param)
    {
       //The value parameterID is 000
       if(param.getParameterID().equals("000"))
       {
          return true;
       }
    
       return false;
    }
  3. 接下来,您将覆盖 validateAction 方法,以只允许输入字符串参数中有一个值,而不是两个参数都具有值。以下伪码演示了此情况:
    Collect all the Parameter objects associated with the action
    Iterate through the Parameter objects and get the string1 and string2 Parameter objects
    Retrieve the string value of string1, check to see if it's null or has a length of 0
    Retrieve the string value of string2, check to see if it's null or has a length of 0
    Determine if both the parameters have values
    	Return an error message
    Determine if neither of the parameters have values
    	Return an informational message 
    使用以下示例样本代码来覆盖此方法:
    public ValidationResult validateAction(ActionValidationEvent event) 
    {
       Parameter string1Param = null;
       Parameter string2Param = null;
       ValidationResult result = new ValidationResult();
    		
       //Iterate through and retrieve the parameter objects
       for (Object o : event.action.getParameters()) 
       {
          if (((Parameter) o).getParameterId().equals("001"))  
          {
             string1Param = (Parameter) o;
          } 
          else if (((Parameter) o).getParameterId().equals("002")) 
          {
             string2Param = (Parameter) o;
          }
       ) 
    		
       //Retrieve the parameter value for string1, and determine if there was input
       String string1Val = (String) event.parameterValueMap.get(string1Param);
       boolean string1IsThere = !(string1Val == null || string1Val.length() == 0);
    		
       //Retrieve the parameter value for string2, and determine if there was input
       String string2Val = (String) event.parameterValueMap.get(string2Param);
       boolean string2IsThere = !(string2Val == null || string2Val.length() == 0);
    		
       //Determine if both string1 and string2 are provided.
       //Returns an error message.
       if (string1IsThere && string2IsThere)
       {
          result.severity = ValidationResult.ERROR;
          result.message = "Provide either String1 or String2.";
       }
    		
       //Determine if neither string1 nor string2 is provided
       //Returns an info message.
       else if(!string1IsThere && !string2IsThere)
       {
          result.severity = ValidationResult.INFO;
          result.message = "Enter a value for String1 or String2.";
       }
    							
       return result;
    }
  4. 保存源代码,并调试存在的任何错误。

使用条款 | 反馈



本信息中心基于 Eclipse 技术。(http://www.eclipse.org)