JSF ID的规则是什么? [英] What are the rules for a JSF id?
问题描述
似乎我应该可以在半个小时的网上搜索中找到它,但是由于我不能:
It seems like I should be able to find this with a half hour of searching the webs, but since I cannot:
有效的JSF ID有哪些规则?
我读到一封乱码的电子邮件,提示-
和_
受到限制,但是我收到了IllegalArgumentExceptions,我认为这是由于ID造成的.
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.
编辑
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)
推荐答案
必须为有效的 CSS标识符(ident
此处),并且不应重复.
It has to be a valid CSS identifier (the ident
here) and there should be no duplicates.
在CSS中,选择器中的标识符(包括元素名称,类和ID )只能包含字符
[a-zA-Z0-9]
和ISO 10646字符U+00A1
及更高版本,以及连字符(-
)和下划线(_
);它们不能以数字开头,也不能以连字符后接数字开头.标识符还可以包含转义字符和任何ISO 10646字符作为数字代码(请参阅下一项).例如,标识符"B&W?"
可以写为"B\&W\?"
或"B\26 W\3F"
.
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters
[a-zA-Z0-9]
and ISO 10646 charactersU+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"
.
另请参见:
- CSS标识符允许使用字符
- Allowed characters for CSS identifiers
See also:
更新:对于您感兴趣的情况,这是由UIComponentBase#validateId()
提供的验证器的源代码:
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);
}
}
}
}
但是,它比CSS规则要严格一些.它们也不能以连字符开头.
It's however a little more strict than the CSS rules. They cannot start with a hyphen as well.
这篇关于JSF ID的规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!