newvalidator.java

来自「精通Struts:基于MVC的java web车技与开发源程序(3)」· Java 代码 · 共 45 行

JAVA
45
字号
package validate;
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.validator.Resources;

public class NewValidator implements Serializable {
  /**
   * A validate routine that ensures the value is either true or false.
   */
  public static boolean validateBoolean(Object bean,
                                    ValidatorAction va, Field field,
                                    ActionMessages errors,
                                    HttpServletRequest request) {

         String value = null;
    // The boolean value is stored as a String

    if (field.getProperty() != null && field.getProperty().length(  ) > 0){
      value = ValidatorUtils.getValueAsString(bean, field.getProperty(  ) );
    }

  
    if ( value==null || !(value.equalsIgnoreCase("true")||value.equalsIgnoreCase("false"))){
      errors.add( field.getKey(  ),
                  Resources.getActionMessage(request, va, field));
    }

    // Return true if the value was successfully converted, false otherwise
    return (errors.isEmpty(  ));

    }



}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?