Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the rules for a JSF id?

Tags:

jsf

It seems like I should be able to find this with a half hour of searching the webs, but since I cannot:

What are the rules for valid JSF ids?

I read a garbled e-mail message that suggested there were limitations on - and _, but I'm getting IllegalArgumentExceptions and I think it's due to the ids.

EDIT

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)
like image 832
Adam Avatar asked May 12 '11 01:05

Adam


1 Answers

It has to be a valid CSS identifier (the ident here) and there should be no duplicates.

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

See also:

  • Allowed characters for CSS identifiers

Update: for the case you're interested, here's the source code of the validator as provided by 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);
            }
        }
    }
}

It's however a little more strict than the CSS rules. They cannot start with a hyphen as well.

like image 108
BalusC Avatar answered Sep 22 '22 15:09

BalusC