使用 findComponent 方法获取 Null?
Getting Null with findComponent method?
这是我添加的 Java 代码
public void collapseTreeById(ActionEvent event) throws IOException {
String treeId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("treeId");
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
UITree tree = (UITree)viewRoot.findComponent(treeId);
tree.setToggleType(SwitchType.server);;
}
和jsf页面变化
<a4j:commandLink actionListener="#{inventoriesBean.collapseTreeById}"
value="collapse" render="treeServType">
<f:param value="treeServType" name="treeId" />
</a4j:commandLink>
但是我在这一行中得到 Null UITree tree = (UITree)viewRoot.findComponent(treeId);
这里treeServType
是树
的id
那么简单的意思就是给定findComponent
search expression does not exist in the component tree. You seem to have passed the sole component ID and you are searching inside UIViewRoot
(the root of the component tree). This will fail if the target component is by itself actually inside a NamingContainer
分量的分量,比如<h:form>
.
如果您不能或不想更改 findComponent
搜索表达式来表示像 formId:treeId
这样的绝对组件 ID,并且它具有与 NamingContainer
相同的父级该命令 link,那么您应该通过该命名容器父项进行搜索。
String treeId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("treeId");
UIComponent namingContainerParent = event.getComponent().getNamingContainer();
UITree tree = (UITree) namingContainerParent.findComponent(treeId);
// ...
这是我添加的 Java 代码
public void collapseTreeById(ActionEvent event) throws IOException {
String treeId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("treeId");
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
UITree tree = (UITree)viewRoot.findComponent(treeId);
tree.setToggleType(SwitchType.server);;
}
和jsf页面变化
<a4j:commandLink actionListener="#{inventoriesBean.collapseTreeById}"
value="collapse" render="treeServType">
<f:param value="treeServType" name="treeId" />
</a4j:commandLink>
但是我在这一行中得到 Null UITree tree = (UITree)viewRoot.findComponent(treeId);
这里treeServType
是树
那么简单的意思就是给定findComponent
search expression does not exist in the component tree. You seem to have passed the sole component ID and you are searching inside UIViewRoot
(the root of the component tree). This will fail if the target component is by itself actually inside a NamingContainer
分量的分量,比如<h:form>
.
如果您不能或不想更改 findComponent
搜索表达式来表示像 formId:treeId
这样的绝对组件 ID,并且它具有与 NamingContainer
相同的父级该命令 link,那么您应该通过该命名容器父项进行搜索。
String treeId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("treeId");
UIComponent namingContainerParent = event.getComponent().getNamingContainer();
UITree tree = (UITree) namingContainerParent.findComponent(treeId);
// ...