15

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)
Adam
  • 3,675
  • 8
  • 45
  • 77
  • Why don't you just post the exception stacktrace and the code snippet that caused it? – BalusC May 12 '11 at 01:37
  • I don't think I can par down the example to anything reasonably simple that would demonstrate the issue, but I'll attach the stack trace. – Adam May 12 '11 at 01:39

1 Answers1

18

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:


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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    BINGO! can't start with a digit! I appreciate the whole page because I could spend all day replaceing characters and appending text and not get all the rules right 100% of the time. Thanks – Adam May 12 '11 at 01:40
  • @BalusC is there any reason, to be more restrictive than [HTML rules](http://stackoverflow.com/a/79022/1099452)? Only to allow easier CSS selection? – lucasvc Oct 07 '15 at 15:12
  • @lucasvc: HTML rules allow `:`, which conflicts naming container separator character. – BalusC Oct 07 '15 at 15:15
  • @BalusC But container separtor, can be configured, right? And in my case, the problem is with period / dot / `.`. – lucasvc Oct 08 '15 at 05:26
  • 1
    @lucasvc: it was only since JSF 2.0 configurable. – BalusC Oct 08 '15 at 07:32
  • Still, `update=":form-list-users:table-list-users"` cannot be solved. And I was about to rename my ids again. Almost all of them have `-` and still some have `_` in their names. Else only `[a-z]` is included, no numbers at all. `table-list-users` is a `p:dataTable`. – Roland Sep 28 '17 at 20:02
  • 1
    @Roland You have actually a different question than the one asked here. Move on to https://stackoverflow.com/q/8634156 – BalusC Sep 29 '17 at 07:55
  • Yes, maybe it is. I found already the fix for it: I used `p:page` which adds another id fragment to the final id `update` requires. So I set `p:page id="master"` and I can use `update=":master:what-ever-here"` successfully. Thank you replying anyway. – Roland Sep 29 '17 at 19:15