JSF ID的规则是什么?

Ada*_*dam 14 jsf

看起来我应该能够在半小时内搜索网页,但是因为我不能:

有效JSF ID的规则是什么?

我读了一个乱码的电子邮件,暗示有限制-_,但我得到IllegalArgumentExceptions,我认为这是由于ID.

编辑

java.lang.IllegalArgumentException: 6a945017207d46fd82b3d3bb7d2795f1
at javax.faces.component.UIComponentBase.validateId(UIComponentBase.java:549)
at javax.faces.component.UIComponentBase.setId(UIComponentBase.java:351)
at com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:151)
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 18

它必须是一个有效的CSS标识符(ident 这里),不应该有重复.

在CSS中,标识符(包括选择器中的元素名称,类和ID )只能包含字符[a-zA-Z0-9]和ISO 10646字符U+00A1及更高字符,以及连字符(-)和下划线(_); 它们不能以数字开头,也不能以数字后跟连字符开头.标识符还可以包含转义字符和任何ISO 10646字符作为数字代码(请参阅下一项).例如,标识符"B&W?"可以写为"B\&W\?""B\26 W\3F".

也可以看看:


更新:对于您感兴趣的情况,这里是验证器的源代码,由UIComponentBase#validateId()以下提供:

private static void validateId(String id) {
    if (id == null) {
        return;
    }
    int n = id.length();
    if (n < 1) {
        throw new IllegalArgumentException("Empty id attribute is not allowed");
    }
    for (int i = 0; i < n; i++) {
        char c = id.charAt(i);
        if (i == 0) {
            if (!Character.isLetter(c) && (c != '_')) {
                throw new IllegalArgumentException(id);
            }
        } else {
            if (!Character.isLetter(c) &&
                    !Character.isDigit(c) &&
                    (c != '-') && (c != '_')) {
                throw new IllegalArgumentException(id);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,它比CSS规则更严格一些.它们也不能以连字符开头.

  • 答对了!不能以数字开头!我很欣赏整个页面,因为我可以花一整天时间来替换字符和附加文本而不是100%的时间都能得到所有规则.谢谢 (2认同)