这是我的xhtml页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Index</title>
<style>
.error {
background-color: #fdd;
}
</style>
</h:head>
<h:body>
<h:messages style="color: orange" />
<h:form>
<h:inputText value="#{myBacking.um}" required="true" />
<h:commandButton value="Submit" action="#{myBacking.acme}" />
</h:form>
</h:body>
</html>
我使用mojarra 2.0.9和mojarra 2.1.8进行了测试,两者都没有为h:inputText
组件生成id。
这是JSF中的错误吗?
答案 0 :(得分:2)
Mojarra只会在最终用户自己指定ID或组件具有指定的客户端行为(<f:ajax>
)时设置ID。
以下是HtmlBasicRenderer
源代码的相关性摘录:
protected String writeIdAttributeIfNecessary(FacesContext context, ResponseWriter writer, UIComponent component) {
String id = null;
if (shouldWriteIdAttribute(component)) {
try {
writer.writeAttribute("id", id = component.getClientId(context), "id");
} catch (IOException e) {
if (logger.isLoggable(Level.WARNING)) {
String message = MessageUtils.getExceptionMessageString (MessageUtils.CANT_WRITE_ID_ATTRIBUTE_ERROR_MESSAGE_ID, e.getMessage());
logger.warning(message);
}
}
}
return id;
}
并注意评论
protected boolean shouldWriteIdAttribute(UIComponent component) {
// By default we only write the id attribute if:
//
// - We have a non-auto-generated id, or...
// - We have client behaviors.
//
// We assume that if client behaviors are present, they
// may need access to the id (AjaxBehavior certainly does).
String id;
return (null != (id = component.getId()) &&
(!id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX) ||
((component instanceof ClientBehaviorHolder) &&
!((ClientBehaviorHolder)component).getClientBehaviors().isEmpty())));
}
所以,这是预期的行为。