我需要从JSF标记的呈现属性中获取调用getter方法的组件的组件ID。
我的目的是建立一个" globalGetter"并根据id或name字段,从哈希映射中检索该对象的权限。
注意:我不想通过EL表达式从JSF实现传递字符串id。
<p:commandButton style="float:right;"
value="New Export"
id="viewPageId"
name="viewPageName"
rendered="#{ExportBean.globalPermission }"
action="#{ExportBean.resetCreateUI}">
</p:commandButton>
以下是我的获取者:
注意:显示的代码用于测试我正在检索哪些ID,并且可以将服务器作为我已经尝试过的方法的示例。
public boolean isGlobalPermission(){
boolean allowed = false;
UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
for(UIComponent compc :component.getChildren()){
System.err.println(compc.getId());
}
String help = component.getContainerClientId(FacesContext.getCurrentInstance());
System.err.println(help + "jaj");
String id = component.getClientId();
String contId = component.getNamingContainer().getId();
String name = (String) component.getAttributes().get("name");
String parent = component.getParent().getId();
System.err.println("testing id--- :" + id);
System.err.println("testing name --- :" + name);
System.err.println("testing container id --- :" + contId);
System.err.println("testing parent id --- :" + parent);
return false;
}
在每种情况下,我都检索了父元素的id。 &#34; for循环&#34;迭代通过子节点确实返回所需的子ID,但我需要确定调用getter的组件。我需要做出猜测工作&#34;超出等式。
答案 0 :(得分:0)
这段代码有效:
public boolean isGlobalGetter(){
UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
String name = (String) component.getAttributes().get("name");
if (name != null) {
name = StringUtils.removeStart(name, ":");
try {
return this.getGlobalPermissionsMap().get(PAGE_NAME + "." + name).isAllowed();
} catch (Exception e) {
return false;
}
}
return true;
}
在遍历此代码之后,它一直返回正确的id。我想这与制定文档对象有关。这就是为什么我必须回归真实的&#39;第一次出现。我可能需要重新考虑这个解决方案,因为我不知道它有多可靠。