0

我将 TomCat 5.5 与 MyFaces 1.1 一起使用,并尝试实现自定义正则表达式验证标签。

我的 RegExValidator 类如下所示:

public class RegExValidator implements Validator, StateHolder {
  private String regex;
  private boolean transientValue = false;

  public RegExValidator() {
    super();
  }

  public RegExValidator(String regex) {
    this();
    this.regex = regex;
  }

  public void validate(FacesContext context, UIComponent component, Object toValidate) throws ValidatorException {

    if ((context == null) || (component == null)) {
throw new NullPointerException();
    }
    if (!(component instanceof UIInput)) {
        return;
    }
    if (null == regex || null == toValidate) {
        return;
    }

    String val = (String) toValidate;

    if (!val.matches(regex)) {
        FacesMessage errMsg = MessageFactory.createFacesMessage(context, Constants.FORMAT_INVALID_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, (new Object[]{regex}));
        throw new ValidatorException(errMsg);
    }
}

public Object saveState(FacesContext context) {
    Object[] values = new Object[1];
    values[0] = regex;

    return (values);
}

public void restoreState(FacesContext context, Object state) {
    Object[] values = (Object[]) state;
    regex = (String) values[0];
}

public String getRegex() {
    return regex;
}

public void setRegex(String regex) {
    this.regex = regex;
}

public boolean isTransient() {
    return transientValue;
}

public void setTransient(boolean transientValue) {
    this.transientValue = transientValue;
}
}

我的 RegExValidatorTag 类如下所示:

@SuppressWarnings("serial")
public class RegExValidatorTag extends ValidatorELTag {

private static String validatorID = null;
protected ValueExpression regex = null;

public RegExValidatorTag() {
    super();

    if (validatorID == null) {
        validatorID = "RegExValidator";
    }
}

public Validator createValidator() throws JspException {

    FacesContext facesContext = FacesContext.getCurrentInstance();
    RegExValidator result = null;

    if (validatorID != null) {
        result = (RegExValidator) facesContext.getApplication().createValidator(validatorID);
    }

    String patterns = null;

    if (regex != null) {
        if (!regex.isLiteralText()) {
            patterns = (String) regex.getValue(facesContext.getELContext());
        } else {
            patterns = regex.getExpressionString();
        }
    }

    result.setRegex(patterns);

    return result;
}

public void setValidatorID(String validatorID) {
    RegExValidatorTag.validatorID = validatorID;
}

/**
 * @param regex
 *            the regex to set
 */
public void setRegex(ValueExpression regex) {
    this.regex = regex;
}
}

我的标签库描述符如下所示:

<tag>
    <name>regexValidator</name>
    <tag-class>com.company.components.taglib.RegExValidatorTag</tag-class>
    
    <attribute>
        <name>regex</name>
        <required>true</required>       
        <rtexprvalue>false</rtexprvalue>    
    </attribute>    
</tag>

我的 face-common-config.xml 有一个 Validator 标签,如下所示:

<validator>
    <description>
        Validate an input string value against a regular
        expression specified by the "regex" attribute.
    </description>
    <validator-id>RegExValidator</validator-id>
    <validator-class>com.company.components.validators.RegExValidator</validator-class>
    <attribute>
        <description>
            The regular expression to test the value against
        </description>
        <attribute-name>regex</attribute-name>
        <attribute-class>java.lang.String</attribute-class>
    </attribute>        
</validator>

后来它应该在这样的jsp文件中使用:

<tc:in value="${dataBean.currentBean.field}">
  <a:regexValidator regex="${dataBean.currentBean.validationRegEx}" />
</tc:in>

调用页面时出现如下错误:

javax.servlet.ServletException:javax.servlet.jsp.JspException:org.apache.jasper.JasperException:无法将字符串“[\d{4}]”转换为属性“regex”的类“javax.el.ValueExpression”:未向 PropertyEditorManager 注册属性编辑器

原因:org.apache.jasper.JasperException - 无法将字符串“[\d{4}]”转换为属性“regex”的类“javax.el.ValueExpression”:属性编辑器未向 PropertyEditorManager 注册

我希望我提供了足够的细节来帮助我解决这个问题......

4

1 回答 1

1

我似乎遇到了类似的问题,我正在尝试找到解决方案,但似乎问题出在使用 Tomcat 或应用程序服务器(WebSphere Application Server 7.0)JSF 库时,我的问题是新的应用程序服务器有新的JSF 库 (1.2) 而不是我的旧应用程序服务器拥有的 1.1 库。(6.1 版)

更加具体。我的问题在这里描述。http://www-01.ibm.com/support/docview.wss?uid=swg21318801

于 2010-12-20T16:14:08.633 回答