如何获取数据表中组件的jsf clientid?
我正在尝试获取数据表中组件的客户端 ID。问题是 jsf 自动将行索引放在组件 id 之前,即
<a id="mappedidentifier_table:1:mappedidentifier_Update" href="#">Update</a>
第二行中的链接 (index=1)。
我正在使用以下方法来获取 clientId,
public static String findClientId(String id) {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot view = context.getViewRoot();
if (view == null) {
throw new IllegalStateException("view == null");
}
UIComponent component = findComponent(view, id);
if (component == null) {
throw new IllegalStateException("no component has id='" + id + "'");
}
return component.getClientId(context);
}
private static UIComponent findComponent(UIComponent component, String id) {
if (id.equals(component.getId())) {
return component;
}
Iterator<UIComponent> kids = component.getFacetsAndChildren();
while (kids.hasNext()) {
UIComponent kid = kids.next();
UIComponent found = findComponent(kid, id);
if (found != null) {
return found;
}
}
return null;
}
但这会返回
mappedidentifier_table:mappedidentifier_Update
,
而不是
mappedidentifier_table:1:mappedidentifier_Update
,
因此它与导致行索引的任何元素都不匹配id 丢失。
我读过 http:// invalidargumentexception.blogspot.com/2009/05/jsf-using-component-ids-in-data-table.html
但是我打算有一个更简单的实现,而不是像作者那样的 TLD 函数或facelets。
有人有什么想法吗?
谢谢,
dzh
i am trying to get the client id of a component in a datatable. the problem is that jsf puts row index before the component id automatically, i.e.
<a id="mappedidentifier_table:1:mappedidentifier_Update" href="#">Update</a>
for a link in the second row (index=1).
i am using the following methods to get the clientId
public static String findClientId(String id) {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot view = context.getViewRoot();
if (view == null) {
throw new IllegalStateException("view == null");
}
UIComponent component = findComponent(view, id);
if (component == null) {
throw new IllegalStateException("no component has id='" + id + "'");
}
return component.getClientId(context);
}
private static UIComponent findComponent(UIComponent component, String id) {
if (id.equals(component.getId())) {
return component;
}
Iterator<UIComponent> kids = component.getFacetsAndChildren();
while (kids.hasNext()) {
UIComponent kid = kids.next();
UIComponent found = findComponent(kid, id);
if (found != null) {
return found;
}
}
return null;
}
however this returns
mappedidentifier_table:mappedidentifier_Update
,
instead of
mappedidentifier_table:1:mappedidentifier_Update
,
therefore it does not match any element cause the row index in id is missing.
i've read http://illegalargumentexception.blogspot.com/2009/05/jsf-using-component-ids-in-data-table.html
however i intend to have a simpler implementation, rather than TLD function or facelets like the author did.
does anyone have any thoughts ?
thanks,
dzh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在行上下文之外解析
clientId
,就会发生这种情况。行上下文由 UIData 控件在生命周期的每个阶段设置。如果您在 EL 表达式中使用立即计算而不是延迟计算 -
${}
而不是#{}
,也可能会发生这种情况。顺便说一句,正如文章中所指出的,该查找算法仅在组件标识符对于视图是唯一的情况下才有效;规范规定它只需要对 NamingContainer 是唯一的;如果您在页面上谨慎使用唯一的组件 ID,这不一定是问题。
This will happen if you resolve the
clientId
outside the context of a row. The row context is set up at each stage of the lifecycle by theUIData
control.It might also happen if you're using immediate evaluation instead of deferred evaluation in your EL expressions -
${}
instead of#{}
.As an aside, and as noted in the article, that lookup algorithm will only work if the component identifier is unique to the view; the spec says it need only be unique to the
NamingContainer
; this need not be a problem if you are careful about using unique component IDs on your page.